POJ 2739 Sum of Consecutive Prime Numbers
来源:互联网 发布:java 开源项目源码 编辑:程序博客网 时间:2024/06/05 06:08
题意:
给你一个数<=10000,问你该数等于连续的质数相加的方案数有多少种。
思路:
既然是连续的质数相加而且数最大才一万,那么我们可以先打张10000以内的质数表,然后从枚举第一个相加的质数进行搜索,累加结果。
#include<cstdio>#include<cstring>const int MAX=10005;int prime[MAX],num[MAX],cnt,ans,n;bool vis[MAX];void sieve(){memset(vis,false,sizeof(vis));for(int i=2;i<MAX;i++){if(!vis[i]){for(int j=i*i;j<MAX;j+=i){vis[j]=true;}}}} void gen_primes(){sieve();cnt=0;for(int i=2;i<MAX;i++){if(!vis[i]){prime[cnt++]=i;}}} void DFS(int i,int sum){if(sum==n){ans++;return;}if(sum+prime[i]<=n&&i<cnt){DFS(i+1,sum+prime[i]);}}int main(){gen_primes();while(~scanf("%d",&n),n){ans=0;for(int i=0;prime[i]<=n;i++){DFS(i,0);}printf("%d\n",ans);}return 0;}
0 0
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ-2739-Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- poj - 2739 - Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- poj 2739 Sum of Consecutive Prime Numbers
- 【POJ-2739】Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- POJ-2739-Sum of Consecutive Prime Numbers
- POJ 2739 Sum of Consecutive Prime Numbers
- JVM简介
- Android Toolbar设置向上箭头,标题等
- sdut oj2054 数据结构实验之链表九:双向链表
- Android 一些异常
- 转:Bootstrap的常用类(CSS和布局组件)的总结
- POJ 2739 Sum of Consecutive Prime Numbers
- Leetcode 162. Find Peak Element (Medium) (cpp)
- 分析优酷/土豆/pptv/乐视 HTML5、m3u8地址
- JHTP自测题_第十二章_GUI组件(上篇)
- python资料
- C++语法 : 内存池技术的原理与实现
- nodejs--http
- FILL bfs
- Java垃圾回收(一)对象存活状态判断---深入理解Java虚拟机