Codeforces Round #382D. Taxes(哥德巴赫猜想)

来源:互联网 发布:淘宝红包判定套现 编辑:程序博客网 时间:2024/05/17 06:52

D. Taxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 partsn1 + 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拆分为若干个数的和,怎样拆分才能使得这些数的每个数的最大除数之和最小。

分析:有想过把20亿以内的素数全部筛出来,存起来之后,用DAG上的最短路解决。也就相当于,有n种硬币,面值分别为V1,V2,V3,.....Vn,每种都有无限多。

给定非负整数S,最少可以选用多少个硬币,使得面值之和恰好为S。这个dp一下就可以了。但是数据范围这么大,也就想想而已。。

正解还是哥德巴赫猜想:

任何一个大于2的偶数都能分解成两个质数的和
所以,如果输入的n为2,就输出1
如果大于n,则判断是否为偶数;为偶数就输出2
如果为奇数: 
①先判断是不是质数,是质数就输出1 
②不是质数的话就找离它最近的质数now(now要小于等于n-2),然后用这个数n减去now,得到差,因为n为奇数、且质数肯定是奇数,所以n-now必然为偶数。这个时候如果n-now==2,则总的答案为1+1==2,如果n-now!=2,则n-now是大于2的偶数,则n-now可以分解成两个质数的和,则答案为1+2==3

#include<bits/stdc++.h>using namespace std;bool Is_prime(int number){    for(int i = 2; i <= sqrt(number); i++)        if(number % i == 0)            return false;    return true;}int main(){    int n;    while(cin >> n)    {        if(n & 1)        {            if(Is_prime(n))                cout << 1 << endl;            else if(Is_prime(n-2))                cout << 2 << endl;            else                cout << 3 << endl;        }        else        {            if(n == 2)                cout << 1 << endl;            else                cout << 2 << endl;        }    }    return 0;}




0 0
原创粉丝点击