hdu 5104 Primes Problem (素数+递推)
2015-08-25 18:52
330 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000).
[align=left]Output[/align]
For each test case, print the number of ways.
[align=left]Sample Input[/align]
3
9
[align=left]Sample Output[/align]
0
2
先找出所有素数,再递推。
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.
[align=left]Input[/align]
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000).
[align=left]Output[/align]
For each test case, print the number of ways.
[align=left]Sample Input[/align]
3
9
[align=left]Sample Output[/align]
0
2
先找出所有素数,再递推。
#include<cstdio> #include<cstring> using namespace std; const int N=10005; int zj ,ssb ,pn,ans,n; void f() { int i,j; zj[0]=zj[1]=1; pn=0; for (i=2;i<N;i++) { if (!zj[i]) ssb[pn++]=i; for (j=0;j<pn;j++) { if (i*ssb[j]>N) break; zj[i*ssb[j]]=1; if (i%ssb[j]==0) break; } } } void dfs(int sum,int x,int p) { if (p==2) { if (zj[n-sum]==0&&n-sum>=ssb[x]) ans++; return ; } int i; for (i=x;i<pn;i++) { if (sum+ssb[i]>=n) return ; dfs(sum+ssb[i],i,p+1); } } int main() { f(); while (~scanf("%d",&n)) { ans=0; dfs(0,0,0); printf("%d\n",ans); } }
相关文章推荐
- [poj3984]
- HTML访问CGI
- Android 中textview显示富文本信息
- 45.UIAlertController和常见传值
- Android 中textview显示富文本信息
- 输入一个int型数据,计算出它在内存中存储时含1的个数
- Application Data简介
- 制作好看又非常简单CSS样式的颜色块
- flash视频舞台全屏和视频全屏分开
- AIZU 2560 [想法题]
- 一个图搞定窗口机制
- 【学习总结】IOS程序运行过程 、UIWindow 、controller 、 UIView 创建过程的总结
- hdu 5327 Olympiad 2015 Multi-University Training Contest 4
- Buildings
- Solrj操作Solr4.0 中CloudSolrServer
- opencv3.0&win7专业版Severice Pack1&visual studio 2013错误解决办法
- 开发中的小坑
- Spring的学习之路(一)Bean
- HDU - 4277(暴力加+hash)
- Linux Ubuntu server安装Tomcat-7.0.63