码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5104 Primes Problem (素数+递推)

时间:2015-08-25 20:53:06      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

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(n10000).
 

 

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 }

 

hdu 5104 Primes Problem (素数+递推)

标签:

原文地址:http://www.cnblogs.com/pblr/p/4758343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!