Knowledge is Power(思维构造)

本文探讨了如何计算将一个整数n拆分成两两互质数的最小差值,通过分类讨论奇数、偶数及特定余数情况,揭示了解决此类问题的关键策略。

Knowledge is Power

[Link](Problem - K - Codeforces)

题意

给你一个n,问你把n拆分成至少两个数的和,且拆分后的数两两互质,问你所有的拆分方案中最大值减最小值的最小值是多少。

题解

分类来看:

如果 n n n是奇数就一定是拆成 n / 2 , n / 2 + 1 n/2,n/2 + 1 n/2,n/2+1,res = 1。

如果 n n n偶数且被 4 4 4整除,那么可以拆成 n / 2 − 1 , n / 2 + 1 n/2 - 1,n/2+1 n/21,n/2+1 , res = 2

否则按照 n % 3 n\%3 n%3的情况分类

  1. n % 3 = = 0 n\% 3==0 n%3==0,可以拆成 n / 3 − 1 、 n / 3 、 n / 3 + 1 n/3 - 1、n/3 、 n/3 + 1 n/31n/3n/3+1。 res = 2
  2. n % 3 = = 1 n\% 3==1 n%3==1,可以拆成 n / 3 − 1 , n / 3 , n / 3 + 2 n/3 - 1, n/3, n/3 + 2 n/31,n/3,n/3+2。 res = 3
  3. n % 3 = = 2 n\% 3 == 2 n%3==2,则可以拆成 n / 3 − 1 , n / 3 + 1 , n / 3 + 2 n/3 - 1, n/3 + 1, n/3 + 2 n/31,n/3+1,n/3+2。 res = 3

否则偶数一定可以拆分成 n / 2 − 2 , n / 2 + 2 n / 2 - 2, n/2 + 2 n/22,n/2+2。 res = 4

Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath> 
#include <stack>
#include <iomanip>
#include <deque> 
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
    e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int gcd(int a, int b) {
    return b ? gcd(b, a % b) : a;
}
int n, m, k;
int a[N];

int main() {
    int T, C = 0;
    cin >> T;
    while (T -- ) {
        int res = 4; cin >> n;
        if (n == 6) res = -1;
        else if (n & 1) res = 1;
        else if (n % 4 == 0) res = 2;
        else {
            if (n % 3== 0) res = 2;
            else if (n % 3 == 1) {
                int a = n / 3 - 1, b = n / 3, c = n / 3 + 2;
                if (gcd(a, b) == 1 && gcd(a, c) == 1 && gcd(b, c) == 1)  res = 3;
            }
            else {
                int a = n / 3 - 1, b = n / 3 + 1, c = n / 3 + 2;
                if (gcd(a, b) == 1 && gcd(a, c) == 1 && gcd(b, c) == 1)  res = 3;
            }
        }        
        printf("Case #%d: %d\n", ++ C, res);
    }
    return 0;
}
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值