Codefroces 735D Taxes(哥德巴赫猜想)

题目链接:http://codeforces.com/problemset/problem/735/D

题目大意:给一个n,n可以被分解成n1+n2+n3+....nk(1=<k<=n)。要求出分解后n1~nk的最大因子(不包括本身)之和的最小值。

解题思路:看了别人的题解才知道是哥德巴赫猜想。

哥德巴赫猜想内容:

①所有大于2的偶数可以被分解成两个素数。

②所有大于7的奇数可以被分解成三个素数。(n-3)为偶数,3是一个素数,所以是三个。

现在来看这题,我们可以分两种情况:

①若n为偶数,则可分解为两个素数,答案为2

②若n为奇数,则至少可分解为3个素数;还有两种情况,一、n为素数,则答案为1,二、n-2是素数,则答案为2(偶数中只有2是素数)。其他时候答案为3。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 
 7 bool prime(int n){
 8     if(n<2)
 9         return false;
10     for(int i=2;i*i<=n;i++){
11         if(n%i==0)
12             return false;
13     }
14     return true;
15 }
16 
17 int main(){
18     int n;
19     while(cin>>n){
20         if(n%2==0)
21             cout<<2<<endl;
22         else{
23             if(prime(n))
24                 cout<<1<<endl;
25             else if(prime(n-2))
26                 cout<<2<<endl;
27             else
28                 cout<<3<<endl;
29         }
30     }
31     return 0;
32 }

  

原文地址:https://www.cnblogs.com/fu3638/p/8483110.html