CodeForces - 735D Taxes 数论 哥德巴赫猜想和弱哥德巴赫猜想
2017-08-09 16:55
429 查看
http://codeforces.com/problemset/problem/735/D
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分成若干份,价值是每份的它的最大因子和(不是本身),求最小价值。如27分成3 11 13,价值就是3。
题解:最优的方法就是把这个数尽可能的分成素数。如果对于素数那么价值就是1,那么对于合数呢。根据哥德巴赫猜想有:任何大于2的偶数都可以分成两个质数和。所以偶数除2之外的价值就是2。根据弱哥德巴赫猜想有:任何大于7的奇数都可以分成三个质数和。但是对于奇数这么分不一定是最优的,如13,可以分成2 11和3 3 5。其实可以发现因为质数中只有2这个偶数,所以如果n-2是素数的话,那么价值就是2了。反之就是3。
代码:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100000 + 5;
const int mod = 1000000000 + 7;
bool judge(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0){
return 0;
}
}
return 1;
}
int main(){
int n;
cin>>n;
if(judge(n)){
puts("1");
}
else if(n%2==0){
puts("2");
}
else{
if(judge(n-2)){
puts("2");
}
else{
puts("3");
}
}
return 0;
}
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分成若干份,价值是每份的它的最大因子和(不是本身),求最小价值。如27分成3 11 13,价值就是3。
题解:最优的方法就是把这个数尽可能的分成素数。如果对于素数那么价值就是1,那么对于合数呢。根据哥德巴赫猜想有:任何大于2的偶数都可以分成两个质数和。所以偶数除2之外的价值就是2。根据弱哥德巴赫猜想有:任何大于7的奇数都可以分成三个质数和。但是对于奇数这么分不一定是最优的,如13,可以分成2 11和3 3 5。其实可以发现因为质数中只有2这个偶数,所以如果n-2是素数的话,那么价值就是2了。反之就是3。
代码:
#include<bits/stdc++.h>
#define debug cout<<"aaa"<<endl
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define MIN_INT (-2147483647-1)
#define MAX_INT 2147483647
#define MAX_LL 9223372036854775807i64#define MIN_LL (-9223372036854775807i64-1)
using namespace std;
const int N = 100000 + 5;
const int mod = 1000000000 + 7;
bool judge(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0){
return 0;
}
}
return 1;
}
int main(){
int n;
cin>>n;
if(judge(n)){
puts("1");
}
else if(n%2==0){
puts("2");
}
else{
if(judge(n-2)){
puts("2");
}
else{
puts("3");
}
}
return 0;
}
相关文章推荐
- codeforces 735D Taxes 哥德巴赫猜想
- codeforces 735D Taxes 哥德巴赫猜想
- codeforces 735D Taxes(数论)
- codeforces 数论分析题
- Codeforces 236C LCM Challenge 数论(基础题)
- Codeforces 382B Number Busters(数论)
- CodeForces 630 J. Divisibility(数论)
- [数论] Codeforces 516E. Drazil and His Happy Friends
- Codeforces 622F 「数学数论」「数学规律」
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
- codeforces 55D beautiful number [数学+数位DP]【动态规划+数论】
- CodeForces - 376C Divisible by Seven(数论:同余定理)(找规律)
- CodeForces - 359C Prime Number(数论)
- CodeForces - 58B Coins(数论)
- codeforces 401D D. Roman and Numbers(状态压缩dp+数论)
- codeforces 474F F. Ant colony(线段树+数论)
- Codeforces 396B On Sum of Fractions 数论
- codeforces 165E DP 数论 二进制
- Codeforces 893E 数论
- CodeForces 735D - Taxes(数论)