Codeforces Round #382 (Div. 1) B. Taxes

B. Taxes
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Mr. Funt now lives in a country with a very specific tax laws. The total income of mr. Funt during this year is equal to n (n ≥ 2) burles and the amount of tax he has to pay is calculated as the maximum divisor of n (not equal to n, of course). For example, if n = 6 then Funt has to pay 3 burles, while for n = 25 he needs to pay 5 and if n = 2 he pays only 1 burle.

As mr. Funt is a very opportunistic person he wants to cheat a bit. In particular, he wants to split the initial n in several parts n1 + n2 + … + nk = n (here k is arbitrary, even k = 1 is allowed) and pay the taxes for each part separately. He can’t make some part equal to 1 because it will reveal him. So, the condition ni ≥ 2 should hold for all i from 1 to k.

Ostap Bender wonders, how many money Funt has to pay (i.e. minimal) if he chooses and optimal way to split n in parts.

Input
The first line of the input contains a single integer n (2 ≤ n ≤ 2·109) — the total year income of mr. Funt.

Output
Print one integer — minimum possible number of burles that mr. Funt has to pay as a tax.

Examples
input
4
output
2
input
27
output
3

题意:要根据自己的收入交税,税额是收入n除本身外的最大因数,可以将收入n分成k份,每份不能为1,分成k份后取每份的因数加和作为税额。

题解:根据素数的因子是除本身外就是1,因此素数收入的税额是最小的,根据哥德巴赫猜想,任何偶数都可拆分成两个质数,因此偶数收入的税额必定是2,素数税额必定是1,奇数分为3中情况,一种作为素数税额为1,一种作为奇数,但减2后得到一个素数,因此可以看成是两个素数之和,税额也为2。第三种是奇数减2之后仍是一个普通奇数,但再减2之后可以得到一个素数,因此是2+2+素数,三个素数之和税额为3。

#include<stdio.h>///判断素数的方法,就是从2一直到sqrt(n)判断取摸是否有为0
#include<string.h>///可以不用素数筛,单个数判断素数
#include<math.h>
bool judge(long long n)///分3种情况,根据哥德巴赫猜想,一个不为2的偶数必能拆成两个素数之和,所以偶数n纳税都是2
{
    int x=n,i;
    for(i=2; i<=sqrt(n); i++)///奇数分两种情况,一种就是素数,另一种是,奇数-2后是一个素数,此时可分成2+一个素数,纳税为2
    {
        while(x%i==0)
        {
            return false;///第三种情况,奇数-2不为素数,此时奇数被分为3个素数相加,纳税为3
        }
    }
    return true;
}
int main()///题目要纳的税是收入n的最大因子(除去n),并且可以把n分成不为1的很多份
{///求纳税最小值
    int i,n;
    while(scanf("%d",&n)!=EOF)///也就是当分成很多个素数时,纳税都是1
    {
        if(judge(n))///若是质数。最大因子为1
        {
            printf("1
");
            continue;
        }
        if(n%2==0)///若是偶数,可分解为两个质数之和
            printf("2
");
        else///非素数的奇数,有两种情况
        {
            if(judge(n-2))
                printf("2
");
            else
                printf("3
");
        }
    }
}
///以下是分解质因数的方法
//void zhiyinshu(int n)
//{
//    int x=n,i;
//    printf("%d=",n);
//    for(i=2;i<=sqrt(n);i++)
//    {
//        while(x%i==0)
//        {
//            if(x==i)
//                break;
//            x/=i;
//            printf("%d*",i);
//        }
//    }
//    printf("%d
",x);
//}
原文地址:https://www.cnblogs.com/kuronekonano/p/11794343.html