http://acm.hdu.edu.cn/showproblem.php?pid=2098
把一个偶数拆成两个不同素数的和,有几种拆法呢?
输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
30
26
0
3
2
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<map> 8 #include<set> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::sort; 14 using std::set; 15 using std::map; 16 using std::pair; 17 using std::vector; 18 using std::lower_bound; 19 #define sz(c) (int)(c).size() 20 #define all(c) (c).begin(), (c).end() 21 #define iter(c) __typeof((c).begin()) 22 #define cls(arr,val) memset(arr,val,sizeof(arr)) 23 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 24 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 26 #define pb(e) push_back(e) 27 #define mp(a, b) make_pair(a, b) 28 const int Max_N = 10010; 29 typedef unsigned long long ull; 30 struct PrimeSpilt { 31 int tot, prime[Max_N]; 32 inline bool isPrime(int n) { 33 for(int i = 2; i * i <= n; i++) { 34 if(!(n % i)) return false; 35 } 36 return n != 1; 37 } 38 inline void init() { 39 tot = 0; 40 for(int i = 1;i < Max_N ;i++) { 41 if(isPrime(i)) prime[tot++] = i; 42 } 43 } 44 inline void work(int n) { 45 int ans = 0; 46 for(int i = 0;i < tot && prime[i] < n; i++) { 47 int p = lower_bound(prime + i, prime + tot, n - prime[i]) - prime; 48 if(prime[i] + prime[p] == n && i != p) ans++; 49 } 50 printf("%d\n", ans); 51 } 52 }go; 53 int main() { 54 #ifdef LOCAL 55 freopen("in.txt","r",stdin); 56 freopen("out.txt","w+",stdout); 57 #endif 58 int n; 59 go.init(); 60 while(~scanf("%d", &n) && n) go.work(n); 61 return 0; 62 }
原文:http://www.cnblogs.com/GadyPu/p/4592395.html