题目大意:
题目链接:https://www.luogu.org/problemnew/show/CF735D
某人要交税,交的税钱是收入
n
n
n的最大因子(除了
n
n
n本身)。但是现在这人为了避税,把钱拆成几份,使交税最少,输出税钱。
思路:
当时看到是黑题,但是一翻讨论发现有人说是恶意评分,所以就想了一下。
结果发现很简单。
先分类讨论。
- 当 n = 1 n=1 n=1时,答案显然为1。
- 当 n n n是质数时,答案显然为1。
- 当 n n n是除了2以外的偶数时,根据哥德巴赫猜想,可以拆分成两个质数,答案为2。
- 当 n n n是除了质数以外的奇数时,只有 ( n − 2 ) + 2 (n-2)+2 (n−2)+2才有可能是两个质数,所以如果 n − 2 n-2 n−2是质数,那么答案就是2,否则可以拆分成 ( n − 3 ) + 3 (n-3)+3 (n−3)+3,其中 n − 3 n-3 n−3是偶数,3是质数,所以答案为3。
还是很简单的。
代码:
#include <cstdio>
using namespace std;
int n;
bool isprime(int x) //O(sqrt(n))判断质数
{
for (int i=2;i*i<=x;i++)
if (!(x%i)) return 0;
return 1;
}
int main()
{
scanf("%d",&n);
if (n==1||isprime(n)) return !printf("1");
if ((!(n&1))||isprime(n-2)) return !printf("2");
return !printf("3");
}