Time Limit: 1000MS   Memory Limit: 65536K

Description

Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations does a given positive integer have? For example, the integer 53 has two representations 5 + 7 + 11 + 13 + 17 and 53. The integer 41 has three representations 2+3+5+7+11+13, 11+13+17, and 41. The integer 3 has only one representation, which is 3. The integer 20 has no such representations. Note that summands must be consecutive prime 
numbers, so neither 7 + 13 nor 3 + 5 + 5 + 7 is a valid representation for the integer 20. 
Your mission is to write a program that reports the number of representations for the given positive integer.

Input

The input is a sequence of positive integers each in a separate line. The integers are between 2 and 10 000, inclusive. The end of the input is indicated by a zero.

Output

The output should be composed of lines each corresponding to an input line except the last zero. An output line includes the number of representations for the input integer as the sum of one or more consecutive prime numbers. No other characters should be inserted in the output.

Sample Input

2
3
17
41
20
666
12
53
0

Sample Output

1
1
2
3
0
0
1
2
题意:输入一个数字(<=1e5)求该数可由几种在素数表中连续的素数之和组成
思路:用尺取法,注意退出循环的情况
 #include <iostream>
#include <cstdio>
using namespace std;
#define N 10010 int prime[N];//素数表 int quickmod(int a,int b,int c)//快速幂模
{
int ans=; a=a%c; while (b)
{
if (b&)
{
ans=ans*a%c;
}
a=a*a%c;
b>>=;
} return ans;
} bool miller(int n)//米勒求素数法
{
int i,s[]={,,,,}; for (i=;i<;i++)
{
if (n==s[i])
{
return true;
} if (quickmod(s[i],n-,n)!=)
{
return false;
}
}
return true;
} void init()
{
int i,j; for (i=,j=;i<N;i++)//坑点:注意是i<N,而不是j<N
{
if (miller(i))
{
prime[j]=i;
j++;
}
}
} void test()
{
int i;
for (i=;i<N;i++)
{
printf("%6d",prime[i]);
}
} int main()
{
int n,l,r,ans,sum;//l为尺取法的左端点,r为右端点,ans为答案,sum为该段素数和 init();
// test(); while (scanf("%d",&n)&&n)
{
l=r=ans=;
sum=; for (;;)
{
while (sum<n&&prime[r+]<=n)//prime[r+1]<=n表示该数是可加的,意即右端点还可以继续右移
{
sum+=prime[++r];
} if (sum<n)//右端点无法继续右移,而左端点的右移只能使sum减小,意即sum数组无法再大于等于n,就可以退出循环
{
break;
} else if (sum>n)
{
sum-=prime[l++];
} else if (sum==n)
{
ans++;
sum=sum-prime[l];
l++;
}
} printf("%d\n",ans);
} return ;
}

最新文章

  1. 四道简单DP
  2. 光盘刻录 CD刻录软件 Ashampoo Burning Studio特别版 刻录CD就这么简单
  3. Android APK的安装
  4. 解决pageControl页面设置无效问题
  5. POJ 2431 Expedition(探险)
  6. PDF内容不允许复制的解决方法!
  7. JAVA(int...i)问题
  8. Hadoop学习之--Capaycity Scheduler源码分析
  9. JVM并发机制的探讨——内存模型、内存可见性和指令重排序
  10. ember.js
  11. PowerDesigner15的安装和破解
  12. React的学习(上)
  13. mssql server for docker on MacOs
  14. LeetCode Weekly Contest 117
  15. Hyperledger Fabric 建立一个简单网络
  16. 适用于nodercms的打包构建脚本
  17. Ubuntu 启动服务 失败 的 可能解决办法
  18. linux下安装redis组件报错-gcc报错
  19. jmeter oracle 多机 jdbc url配置
  20. vue spn如何做seo优化

热门文章

  1. ZT 匿名内存映射
  2. 软工团队 - 预则立&amp;&amp;他山之石
  3. codeforces 809C Find a car
  4. #npm install# MSBUILD : error MSB4132: 无法识别工具版本“2.0”。可用的工具版本为 &quot;4.0&quot;。
  5. hdu 5521 Meeting(最短路)
  6. 远程执行newLISP代码
  7. 点击键盘上的“Next”button实现文本框焦点跳转
  8. swift 第一个IOS应用程序
  9. BZOJ4653:[NOI2016]区间(线段树)
  10. 算法——(4)哈希、hashmap、hashtable