给一个数 写成连续质数的和的形式,能写出多少种
*解法:先筛质数 然后尺取法
**尺取法:固定区间左、右端点为0,如果区间和比目标值大则右移左端点,比目标值小则右移右端点
#include <iostream>
#include <cstdio>
using namespace std;
#define SZ 11000
bool isprime[SZ];
int prime[SZ], num[SZ];
int cnt;
void prim(int n)
{
memset(isprime, , sizeof(isprime));//1->是素数,0->不是素数
memset(prime, , sizeof(prime));
isprime[] = ;
isprime[] = ;
cnt = ;
for(int i = ; i <= n; i++)
{
if(isprime[i]) prime[cnt++] = i;
for(int j = ; j <= cnt && i * prime[j] <= n; j++)
{
isprime[i * prime[j]] = ;
if(i % prime[j] == ) break;
}
}
return;
}
int main()
{
while()
{
int n;
scanf("%d", &n);
if(n == ) break;
prim(n);
num[] = ;
for(int i = ; i < cnt; i++)
num[i] = num[i - ] + prime[i];
int l = , r = , ans = ;
while(r < cnt)
{
if(num[r] - num[l - ] < n) r++;
else if(num[r] - num[l - ] > n) l++;
else if(num[r] - num[l - ] == n) ans++, l++, r++;
}
printf("%d\n", ans);
}
return ;
}

尺取法 || POJ 2739 Sum of Consecutive Prime Numbers的更多相关文章

  1. POJ.2739 Sum of Consecutive Prime Numbers(水)

    POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...

  2. POJ 2739 Sum of Consecutive Prime Numbers(素数)

    POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...

  3. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19697 ...

  6. POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19895 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers【素数打表】

    解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memo ...

  8. poj 2739 Sum of Consecutive Prime Numbers 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K Description Some positive integers can be represented by a ...

  9. poj 2739 Sum of Consecutive Prime Numbers 小结

     Description Some positive integers can be represented by a sum of one or more consecutive prime num ...

随机推荐

  1. 用文件作为Swap分区

    用文件作为Swap分区 1.创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小).# dd if=/dev/zero ...

  2. 1856: [Scoi2010]字符串(Catalan数)

    1856: [Scoi2010]字符串 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 2117  Solved: 1211[Submit][Status] ...

  3. spring mvc No mapping found for HTTP request with URI [/web/test.do] in DispatcherServlet with name 'spring'

    原因: spring-servlet.xml 中 <context:component-scan base-package="com.test.controller" /&g ...

  4. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D

    Description A tree is an undirected connected graph without cycles. The distance between two vertice ...

  5. 修改dns访问android.com

    1.几个常用dns服务器 8.8.8.8 美国 加利福尼亚州圣克拉拉县山景市谷歌公司DNS服务器 8.8.4.4 美国 加利福尼亚州圣克拉拉县山景市谷歌公司DNS服务器 8.8.4.3 美国 加利福尼 ...

  6. vue项目node升级后,node-saas报错解决办法

    ERROR in ./node_modules/_extract-text-webpack-plugin@3.0.2@extract-text-webpack-plugin/dist/loader.j ...

  7. 479 Largest Palindrome Product 最大回文数乘积

    你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...

  8. KEIL软件仿真死在等待外部晶振起振

    这是由于是Debug里面的设置有问题 主要是下面2项设置 Dialog DLL默认是DCM3.DLL Parameter默认是-pCM3 应改为 Dialog DLL默认是DARMSTM.DLL Pa ...

  9. RHEL 6.5----SCSI存储

    主机名 IP master 192.168.30.130 node-1 192.168.30.131 node-2 192.168.30.132 安装并启动 [root@master ~]# ll / ...

  10. 如何正确从他人机器MySQL数据库下拷贝出.sql,再导入到自己windows下MySQL数据库(图文详解)

    不多说,直接上干货! 我这里,是放在桌面上. 登陆数据库 然后, mysql -uroot -p 默认是回车. 创建数据库 CREATE DATABASE securityonion_db; 目的,就 ...