標籤:
題目串連
http://acm.hdu.edu.cn/showproblem.php?pid=2098
分拆素數和Description
把一個偶數拆成兩個不同素數的和,有幾種拆法呢?
Input
輸入包含一些正的偶數,其值不會超過10000,個數不會超過500,若遇0,則結束。
Output
對應每個偶數,輸出其拆成不同素數的個數,每個結果佔一行。
SampleInput
30
26
0
SampleOutput
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 LOCAL55 freopen("in.txt","r",stdin);56 freopen("out.txt","w+",stdout);57 #endif58 int n;59 go.init();60 while(~scanf("%d", &n) && n) go.work(n);61 return 0;62 }View Code
hdu 2098 分拆素數和