Date:2022.01.06
题意:n块钱,交的税是n最大的约数,求你让n分若干份后交的总税最少是多少。
思路:首先,如果n是质数,不需要分则答案直接为1;否则由哥德巴赫猜想:任何大于2的偶数都是两个素数之和,任意大于7的奇数都是三个素数之和。因此若n不是质数且为偶数,输出2;若n不是质数且为奇数,先判断一个奇数能不能分成两个素数之和,因为奇数=奇数+偶数,若能分出两个素数之和则偶素数只有2,因此判断2和n-2是否都为素数,若为素数则输出2,否则输出3。
代码如下:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
LL n,t,q[N],cnt;
bool isprime(LL n)
{
if(n==1) return false;
for(int i=2;i<=n/i;i++)
if(n%i==0) return false;
return true;
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
if(isprime(n)) {cout<<'1'<<endl;return 0;}
if(n%2==0) cout<<'2'<<endl;
else
{
if(n>7)
{
if(isprime(n-2)) cout<<'2'<<endl;
else cout<<'3'<<endl;
}
else cout<<'2'<<endl;
}
return 0;
}