UVa 1210 - Sum of Consecutive Prime Numbers(素数+连续和)
来源:互联网 发布:网络优化工程师 知乎 编辑:程序博客网 时间:2024/05/13 19:21
求所给的数能用多少种连续素数的和表示。
类似于求最大连续和优化的方法。使用前缀和减少运算。然后输入范围是2到10000,貌似可以打表交。
#include<cstdio>const int maxn=10010;int isp[1250],pre_p,sum[1250];bool np[maxn]={true,true};void prepare(){ for(int i=2;i<maxn;++i){ if(!np[i]) isp[pre_p++]=i; for(int j=0;j<pre_p&&i*isp[j]<maxn;++j){ np[i*isp[j]]=true; if(!(i%isp[j])) break; } } for(int i=1;i<=pre_p;++i) sum[i]=sum[i-1]+isp[i-1]; return;}int main(){ prepare(); int n; while(~scanf("%d",&n)&&n){ int cnt=0; for(int i=0;i<pre_p;++i){ if(isp[i]>n) break; for(int j=i+1;j<=pre_p;++j) if(n==sum[j]-sum[i]) ++cnt; } printf("%d\n",cnt); } return 0;}
0 0
- UVa 1210 - Sum of Consecutive Prime Numbers(素数+连续和)
- 习题10-6 UVA - 1210 Sum of Consecutive Prime Numbers 连续素数之和(滑动窗口)
- UVA 1210 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(连续素数和)
- 【UVA】1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- Uva-1210 Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers
- UVA - 1210 - Sum of Consecutive Prime Numbers
- UVa 1210 - Sum of Consecutive Prime Numbers(滑动窗口)
- UVA 1210 Sum of Consecutive Prime Numbers(数论)
- POJ2739,Sum of Consecutive Prime Numbers,连续素数相加和,水过
- POJ 2739: Sum of Consecutive Prime Numbers - 素数和
- POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)
- poj2739 Sum of Consecutive Prime Numbers (素数打表)
- RIL层源码分析
- 题目1517:链表中倒数第k个结点
- MySQL聚簇索引的使用介绍
- C++ 顺序容器的操作(2)push_back、push_front、insert
- USERADD命令详解
- UVa 1210 - Sum of Consecutive Prime Numbers(素数+连续和)
- ARM启动代码研究
- 一天一段scala代码(八)
- Libgdx 1.5.4发布,跨平台游戏开发框架
- 分支-08. 高速公路超速处罚(15)
- JDK 7 Fork and Join: Java也可以轻松地编写并发程序
- 分支-09. 分段计算居民水费(10)
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- CentOS Pureftpd 使用Mysql数据库进行管理用户