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/2−1,n/2+1 , res = 2
否则按照 n % 3 n\%3 n%3的情况分类
- 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/3−1、n/3、n/3+1。 res = 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/3−1,n/3,n/3+2。 res = 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/3−1,n/3+1,n/3+2。 res = 3
否则偶数一定可以拆分成 n / 2 − 2 , n / 2 + 2 n / 2 - 2, n/2 + 2 n/2−2,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;
}
本文探讨了如何计算将一个整数n拆分成两两互质数的最小差值,通过分类讨论奇数、偶数及特定余数情况,揭示了解决此类问题的关键策略。
5855





