交税
Description
阿強愉快的拿到了自己工資,可是摳門的他發(fā)現(xiàn)自己還要沒有交稅。X國采用一種奇怪的稅收方式。對于工資
num,需要收的稅是p,p為num的最大的除數(shù)并且p不等于num。
(ps:6的最大除數(shù)是3,25的最大除數(shù)是5,2的最大除數(shù)是1)
為了減少人們要交的稅,X國又規(guī)定可以把錢分開算稅,分開的個數(shù)不限,即n1?+?n2?+?...?+?nk?=?n,要交的
稅就是每個部分的總和。
阿強現(xiàn)在向你尋求幫助,給定X最少要交多少錢的稅?
Input
數(shù)據(jù)的組數(shù)?T;(T<=2000)
接下來T行,每行一個數(shù)字X(2 <= X <= 2e9)
Output
?對于每行X,輸出最少的稅Y
?
Sample Input
3 11 27 8Sample Output
1 3 2HINT
?
?對于第三個例子8,可以分為3+5,3和5的最大除數(shù)都是1,所以答案是2
C++版本一
題解:
任意一個大于2的偶數(shù)都能被拆分成兩個素數(shù)的和。同時任意一個大于5的奇數(shù)都能被拆分成3個素數(shù)的和,一個為3,剩下的是偶數(shù),按照前面的拆分即可。同時對于奇數(shù)還要考慮n-2為素數(shù)時,個數(shù)為2而不是3.
驗證大素數(shù)則可以用米勒羅賓素數(shù)測試。
#include<bits/stdc++.h> using namespace std; #define ll long longll mod_mul(ll a, ll b, ll n) {ll res = 0;while(b) {if(b&1) res = (res + a) % n;a = (a + a) % n;b >>= 1;}return res; } ll mod_exp(ll a, ll b, ll n) {ll res = 1;while(b) {if(b&1) res = mod_mul(res, a, n);a = mod_mul(a, a, n);b >>= 1;}return res; } bool miller_rabin(ll n) {if(n == 2 || n == 3 || n == 5 || n == 7 || n == 11) return true;if(n == 1 || !(n%2) || !(n%3) || !(n%5) || !(n%7) || !(n%11)) return false;ll x, pre, u;int i, j, k = 0;u = n - 1; while(!(u&1)) { k++; u >>= 1;}srand((ll)time(0));for(i = 0; i < 20; ++i) { x = rand()%(n-2) + 2; if((x%n) == 0) continue;x = mod_exp(x, u, n); pre = x;for(j = 0; j < k; ++j) {x = mod_mul(x, x, n);if(x == 1 && pre != 1 && pre != n-1) return false; pre = x;}if(x != 1) return false; }return true; }int main() {//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);int t;scanf("%d",&t);ll n;while(t--) {scanf("%lld",&n);if(n%2 == 0) {if(n == 2) printf("1\n");else printf("2\n");}else {if(miller_rabin(n)) printf("1\n");else if(miller_rabin(n-2)) printf("2\n");else printf("3\n");}} }C++版本二
哥德巴赫猜想
大于二的偶數(shù)可以分解為兩個素數(shù)之和;
大于七的奇數(shù)可以分解為三個素數(shù)之和;(是一定可以分解成三個素數(shù)之和,也有可能分解成兩個)分解成兩個必然有一個是2,其他就是至少三個。
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> #include <string.h> #include <cmath> #include <queue> using namespace std; typedef long long ll; const int N=10000; ll t,n,m; int a[N]; bool prime(int x){for(int i=2;i<=sqrt(x);i++)if(x%i==0)return 0;return 1;} int main() {scanf("%lld",&t);while(t--){scanf("%lld",&n);if(prime(n)||n==1||n==2||n==3)cout << 1 << endl;else if(n%2==0||prime(n-2))cout << 2 << endl;elsecout << 3 << endl;}//cout << "Hello world!" << endl;return 0; }?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結(jié)
- 上一篇: 你面对以希望为名的绝望微笑
- 下一篇: LLL和小猫