Codeforces#382(Div. 2) D.Taxes【哥德巴赫猜想】
2016-11-28 12:56
483 查看
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
Output
Input
Output
题目大意:
给你一个数N,然你找几个数,使得其加和值为N。使得使用的数字量最少。
思路:
1、我们分类讨论,首先讨论偶数的情况:
根据强哥德巴赫猜想,我们能够知道,任何大于2的偶数能够拆成两个素数相加得到,那么明显,如果N==2,那么其本身也是素数,那么输出1,否则输出2.
2、接下来讨论奇数的情况:
①如果这个数本身就是素数,那么很肯定,输出1即可。
②如果这个数是两个数相加得到的,然而这个数是奇数,很明显只能通过奇数+偶数==奇数得来的,然而偶数中素数只有2,那么判定一下n-2是不是素数即可,如果是,那么输出2,否则输出3。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll __int64int isprime(ll a)
{
for(int i=2;i<=sqrt(a);i++)
{
ll tmp=i;
if(a%tmp==0)return 0;
}
return 1;
}
int main()
{
ll n;
while(~scanf("%I64d",&n))
{
if(isprime(n)==1)
{
printf("1\n");
continue;
}
if(n%2==1)
{
if(isprime(n-2)==1)printf("2\n");
else printf("3\n");
}
if(n%2==0)
{
if(n==2)
{
printf("1\n");
}
else printf("2\n");
}
}
}
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,然你找几个数,使得其加和值为N。使得使用的数字量最少。
思路:
1、我们分类讨论,首先讨论偶数的情况:
根据强哥德巴赫猜想,我们能够知道,任何大于2的偶数能够拆成两个素数相加得到,那么明显,如果N==2,那么其本身也是素数,那么输出1,否则输出2.
2、接下来讨论奇数的情况:
①如果这个数本身就是素数,那么很肯定,输出1即可。
②如果这个数是两个数相加得到的,然而这个数是奇数,很明显只能通过奇数+偶数==奇数得来的,然而偶数中素数只有2,那么判定一下n-2是不是素数即可,如果是,那么输出2,否则输出3。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
using namespace std;
#define ll __int64int isprime(ll a)
{
for(int i=2;i<=sqrt(a);i++)
{
ll tmp=i;
if(a%tmp==0)return 0;
}
return 1;
}
int main()
{
ll n;
while(~scanf("%I64d",&n))
{
if(isprime(n)==1)
{
printf("1\n");
continue;
}
if(n%2==1)
{
if(isprime(n-2)==1)printf("2\n");
else printf("3\n");
}
if(n%2==0)
{
if(n==2)
{
printf("1\n");
}
else printf("2\n");
}
}
}
相关文章推荐
- Codeforces Round #382 (Div. 2) D. Taxes
- Codeforces Round #382 (Div. 1) B. Taxes
- Codeforces Round #382 (Div. 2) D. Taxes 歌德巴赫猜想
- Codeforces Round #382 (Div. 2)D. Taxes(数论知识)
- Codeforces Round #382 (Div. 1) B. Taxes
- Codeforces Round #324 (Div. 2) (584A,584B(排列组合),584C(构造),584D(哥德巴赫猜想),584E(置换群))
- Codeforces Round #382 (Div. 2) C. Tennis Championship 递推、斐波拉契数列
- Codeforces Round #324 (Div. 2) D. Dima and Lisa(哥德巴赫猜想)
- Codeforces Round #382 (Div. 2) 735B - Urbanization
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心
- Codeforces Round #382 (Div. 1) 736B Taxes
- Codeforces Round #382 (Div. 2)-735C. Tennis Championship(递推)
- Codeforces Round #382 (Div. 2)
- Codeforces Round #324 (Div. 2) D. Dima and Lisa 哥德巴赫猜想
- Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想
- Codeforces Round #382 (Div. 2) B. Urbanization
- Codeforces Round #324 (Div. 2) D.Dima and Lisa(哥德巴赫猜想)
- Codeforces #324 Div2 D.Dima and Lisa(三素数构造、哥德巴赫猜想)
- Codeforces Round #382 (Div. 1) C. Ostap and Tree(树形DP)
- Codeforces Round #382 (Div. 2)B. Urbanization 贪心