Greedy:Sum of Consecutive Prime Numbers(POJ 2739)

题目大意:一些整数可以表示成一个连续素数之和,给定一个整数要你找出可以表示这一个整数的连续整数序列的个数
方法:打表,然后用游标卡尺法即可
#include <iostream>
#include <functional>
#include <algorithm>
#define MAX_N 10010 using namespace std; static int primes_set[MAX_N], flag[MAX_N], p_sum; void inivilize(void);
void solve(const int); int main(void)
{
inivilize();
int n; while (~scanf("%d", &n))
{
if (n == )
break;
solve(n);
}
return EXIT_SUCCESS;
} void solve(const int n)
{
int s, t, sum, ans = ;
s = t = sum = ; while ()
{
while (primes_set[t] <= n && sum < n)
sum += primes_set[t++];
if (sum == n)
ans++;
if (sum < n)
break;
sum -= primes_set[s++];
}
printf("%d\n", ans);
} void inivilize(void)
{
int i = , j; memset(flag, , sizeof(flag)); for (; i < MAX_N; i++)
{
if (!flag[i])
primes_set[p_sum++] = i;
for (j = ; j < p_sum && primes_set[j] * i < MAX_N; j++)
flag[primes_set[j] * i] = ;
}
}

Greedy:Sum of Consecutive Prime Numbers(POJ 2739)的更多相关文章
- Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)
题意:给一个数 可以写出多少种 连续素数的合 思路:直接线性筛 筛素数 暴力找就行 (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ.2739 Sum of Consecutive Prime Numbers(水)
POJ.2739 Sum of Consecutive Prime Numbers(水) 代码总览 #include <cstdio> #include <cstring> # ...
- POJ 2739 Sum of Consecutive Prime Numbers(素数)
POJ 2739 Sum of Consecutive Prime Numbers(素数) http://poj.org/problem? id=2739 题意: 给你一个10000以内的自然数X.然 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
- POJ 2739 Sum of Consecutive Prime Numbers( *【素数存表】+暴力枚举 )
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19895 ...
- POJ 2739 Sum of Consecutive Prime Numbers【素数打表】
解题思路:给定一个数,判定它由几个连续的素数构成,输出这样的种数 用的筛法素数打表 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memo ...
- poj 2379 Sum of Consecutive Prime Numbers
...
随机推荐
- Python程序的常见错误(收集篇)
关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...
- Python学习笔记(迭代、模块扩展、GUI 、编码处理等)
PythonIDLE中的编码处理 http://www.tuicool.com/articles/NbyEBr 原文标题:Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来 ...
- 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解
序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...
- 【Tomcat】tomcat报错 removeGeneratedClassFiles failed
程序放到测试环境一点问题没有,放到正式环境都是问题.总感觉是环境的问题,环境能带来问题,但是不是所有问题都能说是环境带来的. 这点,要改正.还要淡定对待问题.看错误. 程序是不会骗你的.这个问题折磨了 ...
- apache2 多站点虚拟主机配置
<VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot /var/www/ ServerN ...
- XML理解
XML:页面之间传递数据,跨平台传递,核心标签 HTML:超文本标记语言,核心标签 <xml version='1.0'>版本1.0<Nation> <one> & ...
- Swift-打开其它Storyboard中的自定义模态窗口
本文的方法针对OS X应用开发. 如果想在某个ViewController中,用模态窗口的方式,打开某个Storyboard中定义的WindowController.可用以下方式. let story ...
- ajax读取XML文本(如读取城市)
//加载城市 function loadArea_pep() { $.ajax({ url: "/xmlFile/crty.xml", success: function (res ...
- sass兼容IE8透明度方法
你可以轻松的利用 {Sass::Script::Functions#ie_hex_str ie_hex_str} 函数对其做转换.$translucent-red: rgba(, , , 0.5); ...
- linux 下开放端口问题
Linux安装Tomcat后本地可以正常访问,可是这时Tomcat还不能被外界访问需要在Linux默认防护墙上打开8080端口 打开 /etc/sysconfig/iptables [root@l ...