您的位置:首页 > 其它

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

2016-12-08 13:10 507 查看
题目链接

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 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拆分为若干个数的和,怎样拆分才能使得这些数的每个数的最大除数(非自身)之和最小。

参考博客链接

题解:

直观可以想到将这个数拆为个数最少的质数之和。

此处需要用到哥德巴赫猜想。

任何一个大于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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: