poj2739
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18427 | Accepted: 10122 |
Description
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
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
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
Source
//memory:744K time:0MS
#include <iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int MAXN = ;
int main()
{
bool vis[MAXN];
int isprime[MAXN],k=;
memset(vis,,sizeof(vis));
for(int i=;i<(int)sqrt((double)MAXN);i++) //筛选法挑选素数
{
if(!vis[i])
{
for(int j=i*i;j<MAXN;j+=i)
vis[j]=;
}
}
for(int i=;i<MAXN;i++)
{
if(!vis[i])
isprime[k++]=i;
}
int n;
while(cin>>n)
{
if(n==)
break;
//因为不一定是从头开始,所以需要两层循环,
//每一个i都需要从i开始往后累加
int num=;
for(int i=;isprime[i]<=n;i++) //isprime循环
{
int ans = ; //累加器
for(int j=i ; j<k&& ans<n ; j++) //从每一个i开始往后循环
{
ans += isprime[j];
}
if(ans==n)
num++;
}
cout<<num<<endl;
}
return ;
}
以下给出一个较好的代码,挑选素数的想法很好,素数只能被素数整除,不会被偶数整除。但是这种方法很明显慢一些
//memory :736K time :32MS
#include<iostream>
using namespace std;
const int MAXN = ;
int prime[MAXN],prime_num = ; bool isprime(int n)
{
for(int i=;i<prime_num;i++)
{
if(n%prime[i]==)
return false;
}
return true;
} int main()
{
int n; for(int i=;i<MAXN;i++)
{
if(isprime(i))
{
prime[prime_num++]=i;
}
}
while(cin>>n)
{
if(n==)
break;
int num=;
for(int i=;prime[i]<=n;i++)
{
int ans = ;
for(int j=i;j<prime_num&&ans<n;j++)
{
ans += prime[j];
}
if(ans == n)
num++;
}
cout<<num<<endl;
}
return ;
}
poj2739的更多相关文章
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- poj2739 poj2100 尺取法基础(二)
都是很简单的题目 poj2739素数打表+单点推移 #include<iostream> #include<cstring> #include<cstdio> us ...
- POJ2739解题报告
2017-09-01 17:04:45 writer:pprp 一开始读错题了,总是想不到,其实不是很难,但是就是心理太着急了,反而浪费了很长时间 /* @param:poj2739 @writer: ...
- POJ2739 - Sum of Consecutive Prime Numbers(素数问题)
题目大意 给定N,要求你计算用连续的素数的和能够组成N的种数 题解 先筛选出素数,然后暴力判断即可... 代码: #include<iostream> #include<cstrin ...
- 【POJ2739】Sum of Consecutive Prime Numbers
简单的素数打表,然后枚举.开始没注意n读到0结束,TLE了回..下次再认真点.A过后讨论里面有个暴力打表过的,给跪了! #include <iostream> #include <c ...
- poj2739尺取法+素数筛
Some positive integers can be represented by a sum of one or more consecutive prime numbers. How man ...
- poj2739(尺取法+质数筛)
题意:给你一个数,问这个数能否等于一系列连续的质数的和: 解题思路:质数筛打出质数表:然后就是尺取法解决: 代码: #include<iostream> #include<algor ...
- 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 ...
- POJ2739(尺取法)
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23931 ...
随机推荐
- Android 百度地图API(01)_开发环境 HelloBaiduMap
转载于:http://blog.csdn.net/lmj623565791/article/details/37729091 转载于:http://blog.csdn.net/crazy1235/ar ...
- C# 约瑟夫环算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- pyqt lineedit右边显示按钮效果
from PyQt4 import QtGui, QtCore class ButtonLineEdit(QtGui.QLineEdit): buttonClicked = QtCore.pyqtSi ...
- python ctypes小例子
import time import ctypes import ctypes.wintypes SEE_MASK_NOCLOSEPROCESS = 0x00000040 SEE_MASK_INVOK ...
- wpf新增记录时用多线程的问题
多线程虽然可以增加用户操作体验,但是有时候会出现意想不到的错误. 如果采用分布式,数据库在另外服务器上,当网络出现问题,或者数据库繁忙,那么新增数据就会等待,这时候用户如果以为没有操作,而多次点击新增 ...
- Windows7 32位机上,OpenCV中配置GPU操作步骤
1. 查看本机配置,查看显卡类型是否支持NVIDIA GPU: 2. 从http://www.nvidia.cn/Download/index.aspx?lang=cn下载最新驱动并安装: 3. ...
- Swift 2.0 封装图片折叠效果
文/猫爪(简书作者)原文链接:http://www.jianshu.com/p/688c491580e3著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 用Swift封装图片折叠效果 b ...
- Codeforces 468B Two Sets 并查集
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...
- bit、byte、位、字节、字符串等概念
原始文章:http://djt.qq.com/article/view/658 1.古代送信:马车,烽火,信鸽 2.1837年,世界第一条电报诞生, 美国科学家莫尔斯尝试用一些“点”和“划”来表示不同 ...
- Ztree中simpleData是怎么转换成标准ztree数组对象的
今天遇到一个自己构造树的情况,树是动态的,预先不知道根节点,用的是easyUI中的tree,于是参考了下Ztree中的实现,恍然大悟,遂记之: transformTozTreeFormat: func ...