题目大意:把一个数n拆分为若干个数的和,怎样拆分才能使得这些数的每个数的最大除数(非自身)之和最小。
题解:
直观可以想到将这个数拆为个数最少的质数之和。
此处需要用到哥德巴赫猜想。
任何一个大于2的偶数都能分解成两个质数的和
所以,如果输入的n为2,就输出1
如果大于n,且为偶数就输出2
如果为奇数:
①先判断n自身是否为质数,若是质数就输出1
②不是质数的话就找离它最近的质数x(x要小于等于n-2,因为质数肯定为奇数),然后用这个数n减去x,得到差,因为n为奇数、且质数肯定是奇数,所以n-x必然为偶数。这个时候如果n-x==2,则总的答案为1+1=2,如果n-x!=2,则n-x是大于2的偶数,则n-x可以分解成两个质数的和,则答案为1+2=3
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
bool isprim(int x)
{
int m=sqrt(x);
for(int i=2;i<=m;i++)
if(x%i==0) return false;
return true;
}
int main(int argc, const char * argv[]) {
int n;
scanf("%d",&n);
if(n&1)
{
if(isprim(n)) cout << 1 << endl;
else if(isprim(n-2)) cout << 2 << endl;
else cout << 3 << endl;
}
else
{
if(n==2) cout << 1 << endl;
else cout << 2 << endl;
}
return 0;
}