【POJ2739】Sum of Consecutive Prime Numbers
简单的素数打表,然后枚举。开始没注意n读到0结束,TLE了回。。下次再认真点。A过后讨论里面有个暴力打表过的,给跪了!
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <complex.h>
#include <cmath>
using namespace std; const int N = ; bool is[N]; int prm[N];
int getprm (int n) {
int i, j, k = ;
int s, e = (int)(sqrt(0.0 + n) + );
memset (is, , sizeof (is));
prm[k ++] = ; is[] = is[] = ;
for (i = ; i < n; i += ) is[i] = ;
for (i = ; i < e; i += )
if (is[i]){
prm[k ++] = i;
for (s = i * , j = i * i; j < n; j += s) {
is[j] = ;
}
}
for (; i < n; i += ) {
if (is[i]) prm[k ++] = i;
}
return k;
} int main () {
getprm();
/*for (int i = 0 ; i < 100; ++ i) {
cout << i << ": ";
cout << prm[i] << endl;
}*/
// 1229个素数 0-1w
ios::sync_with_stdio(false);
cin.tie();
int n;
while (~scanf ("%d", &n)) {
if (n == ) break;
int sum, cnt = ;
for (int i = ; prm[i] <= n; ++ i) {
sum = ;
for (int j = i; prm[j] <= n; ++ j) {
sum += prm[j];
if (sum < n) {
continue;
} else if (sum == n) {
cnt ++;
break;
} else {
break;
}
}
}
printf ("%d\n", cnt);
}
return ;
}
【POJ2739】Sum of Consecutive Prime Numbers的更多相关文章
- 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 ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- POJ2739 Sum of Consecutive Prime Numbers 2017-05-31 09:33 47人阅读 评论(0) 收藏
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25225 ...
- Sum of Consecutive Prime Numbers(poj2739)
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22019 Ac ...
- 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(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers http://poj.org/problem?id=2739 Time Limit: 1000MS Memory Limit: 6 ...
- poj 2739 Sum of Consecutive Prime Numbers 素数 读题 难度:0
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19697 ...
随机推荐
- Maven--多模块依赖实例解析(五)
<Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> <Maven—生命周期和插件(四)&g ...
- hsql使用架构包启动数据库
一.通常我们平时启动就是直接通过hsql.jar来进行启动 java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing java -cp hsql ...
- PHP常用魔术方法(__invoke魔术方法)
<?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload_register('\\ ...
- 对easyui datagrid进行扩展,当滚动条拉直最下面就异步加载数据。
以下方法是通用的,只要把datagrid定义为全局的即可,其他部分的代码不用进行修改! 可以把以下代码放入到一个单独的js文件,然后再需要的页面引入即可! $(function(){ try{ $(& ...
- sdut2623--The number of steps(概率dp第一弹,求期望)
The number of steps Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Mary stands in a st ...
- getAttribute()与getParameter的区别
当两个Web组件之间为转发关系时,转发源会将要共享 request范围内的数据先用setAttribute将数据放入到HttpServletRequest对象中,然后转发目标通过 getParamet ...
- js字母大小写转换
function a(){ document.getElementById("test").value = document.getElementById("test&q ...
- 《第一行代码》学习笔记3-活动Activity(1)
1.活动-一种可以包含用户界面的组件,用于和用户进行交互. <Button android:id="@+id/button_1" android:layout_width=& ...
- sql 2000 分页
create PROCEDURE [dbo].[Proc_GetPageList] ( @Tables varchar(1000), --表名 @PK varchar(100 ...
- 多种下载文件方式 Response.BinaryWrite(byte[] DocContent);Response.WriteFile(System.IO.FileInfo DownloadFile .FullName);Response.Write(string html2Excel);
通过html给xls赋值,并下载xls文件 一.this.Response.Write(sw.ToString());System.IO.StringWriter sw = new System.IO ...