给一个数 写成连续质数的和的形式,能写出多少种
*解法:先筛质数 然后尺取法
**尺取法:固定区间左、右端点为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. IOS 的调试模式

    iOS调试模式分为: 断点单步调试: 全局断点调试: 僵尸调试: 暴力调试: 这里主要说一下什么是僵尸调试模式? .为什么会使用NSZombieEnabled? 应用调试可能会收到类似 Thread ...

  2. Java调用外部类定义的方法(Static与无Static两种)

    首先定义方法 public class Dy { public int Add(int x,int y){ //定义Add(),该方法没有被static修饰 return x+y; } public ...

  3. Know the Core Objects of Your App---了解应用程序的内核对象

    Back to App Design You develop apps using the Cocoa application environment. Cocoa presents the app’ ...

  4. python学习笔记2-条件语句

    #条件语句 ''' if 判断条件: 执行语句…… else: 执行语句…… ''' flag = False name = 'python' if name == 'python': # 判断变量否 ...

  5. Get back Typing Break in Ubuntu 12.04 & 11.10(转载)

    转自:http://ubuntuguide.net/get-back-typing-break-in-ubuntu-12-04-11-10 Since Ubuntu 11.10 Oneiric, th ...

  6. 由mysql分区想到的分表分库的方案

    在分区分库分表前一定要了解分区分库分表的动机. 对实时性要求比较高的场景,使用数据库的分区分表分库. 对实时性要求不高的场景,可以考虑使用索引库(es/solr)或者大数据hadoop平台来解决(如数 ...

  7. Ruby主要方法

         方法定义        def hello(name) ...  end                                                函数名 参数 作用 备 ...

  8. UITableViewCell添加点击时改变字体的颜色、背景、图标

    改变字体颜色:cell.textLabel.highlightedTextColor 改变背景颜色:cell.selectedBackgroundView (必须是图片哦) 改变图标颜色:cell.i ...

  9. HTML5移动端手机网站开发流程

    基本上开发手机网站,可大致分为两大类.一类是用框架开发手机网站.一类是自己手写手机网站. 一.框架开发手机网站 一般用现在常用的开发框架有:目前Web前端最火的框架(BootStrap).jQuery ...

  10. MVC:html动态追加行及取值

    先一个button   id=addRow 点击事件进行添加 $("#addRow").bind("click", function () { var addH ...