1. 程式人生 > >Codeforces Round #382 (Div. 2) D. Taxes (哥德巴赫猜想)

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

D. Taxes

題目連結

題面

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.

輸入

The first line of the input contains a single integer n (2 ≤ n ≤ 2·109) — the total year income of mr. Funt.

輸出

Print one integer — minimum possible number of burles that mr. Funt has to pay as a tax.

樣例輸入

4

樣例輸出

2

【題意】:某個人有n元的工資,但是他要交稅,交稅的數目就是n的最大因子。比如說6,他的因子有1,2,3;最大的是3,所以要交3元的稅,但是這個人想偷稅漏稅(作為共產主義接班人的我們不能向他學習)。偷稅漏稅的方法就是他把這n元分成幾部分,比如把6分成 3 和 3,那麼他就只交2元稅就可以了。但是不能這幾部分都不能為1, 否則會被發現的。 

思想:儘量都拆成素數,這樣每部分只要交1,但是炒成的素數還儘量少。這就是哥德巴赫猜想

如果一個數是偶數(2除外)那麼他能分解為兩個質數的和; 
如果一個數是奇數那麼它有三種情況: 
(1)本身是質數 
(2)這個數減二是質數,那麼他就能分解為兩個質數(很顯然就是n-2 和 2) 
(3)可以分解為一個質數和一個偶數,就是三個質數 

#include<iostream>
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int f = 0, ff = 0;
        for(int i = 2; i * i <= n; i++){
            if(n  % i == 0) f = 1;
            if((n - 2) % i == 0) ff = 1; 
        }
        if(f == 1 && n % 2 == 0) {
            cout << 2 << endl;
            continue;
        }
        if(f == 0) cout << 1 << endl;
        else if(ff == 0) cout << 2 << endl;
        else cout << 3 << endl;
    }
    return 0;
}