Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2016 Accepted Submission(s): 928
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
Source
给出n,计算三个素数相加等于n的方案个数,素数打表就行
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int num[10020]; void prim() { for(int i=2;i<10020;i++) { if(num[i]==0) { for(int j=i+i;j<10020;j+=i) num[j]=1; } } } int main() { int n; memset(num,0,sizeof(num)); num[1]=1; prim(); while(scanf("%d",&n)!=EOF) { int ans=0; for(int i=2;i<=n;i++) { for(int j=i;j<=(n-i)/2;j++) { if(!num[i]&&!num[j]&&!num[n-i-j]) ans++; } } printf("%d ",ans); } return 0; }