Problem Description
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
Input
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000).
Output
For each test case, print the number of ways.
Sample Input
3
9
Sample Output
0
2
先找出所有素数,再递推。
1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 const int N=10005; 5 int zj[N],ssb[N],pn,ans,n; 6 void f() 7 { 8 int i,j; 9 zj[0]=zj[1]=1; 10 pn=0; 11 for (i=2;i<N;i++) 12 { 13 if (!zj[i]) ssb[pn++]=i; 14 for (j=0;j<pn;j++) 15 { 16 if (i*ssb[j]>N) break; 17 zj[i*ssb[j]]=1; 18 if (i%ssb[j]==0) break; 19 } 20 } 21 } 22 void dfs(int sum,int x,int p) 23 { 24 if (p==2) 25 { 26 if (zj[n-sum]==0&&n-sum>=ssb[x]) ans++; 27 return ; 28 } 29 int i; 30 for (i=x;i<pn;i++) 31 { 32 if (sum+ssb[i]>=n) return ; 33 dfs(sum+ssb[i],i,p+1); 34 } 35 } 36 int main() 37 { 38 f(); 39 while (~scanf("%d",&n)) 40 { 41 ans=0; 42 dfs(0,0,0); 43 printf("%d\n",ans); 44 } 45 }
时间: 2024-11-05 19:40:32