[UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3651
题意:问一个数n能拆成多少种连续的素数和。
筛出所有素数发现只有1200个,维护素数的前缀和,再打出所有区间的前缀和的表,统计出来,直接查询。
#include <bits/stdc++.h>
using namespace std; const int maxn = ;
bool isprime[maxn];
int prime[maxn];
int sum[maxn];
int pcnt;
int n;
int mp[];
void init() {
memset(isprime, true, sizeof(isprime));
memset(prime, , sizeof(prime));
pcnt = ;
}
void getPrime() {
init();
prime[] = prime[] = ;
for(int i = ; i <= maxn; i++) {
if(isprime[i]) prime[++pcnt] = i;
for(int j = ; j <= pcnt; j++) {
if(i * prime[j] > maxn) break;
isprime[i*prime[j]] = ;
if(i % prime[j] == ) break;
}
}
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
getPrime();
memset(sum, , sizeof(sum));
sum[] = prime[];
for(int i = ; i < pcnt; i++) sum[i] = sum[i-] + prime[i];
memset(mp, , sizeof(mp));
for(int i = ; i < pcnt; i++) {
for(int j = i; j < pcnt; j++) {
mp[sum[j]-sum[i]]++;
}
}
while(~scanf("%d", &n) && n) {
printf("%d\n", mp[n]);
}
return ;
}
[UVa1210]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 ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
- 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 ...
- 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
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 ...
随机推荐
- Openstack的镜像属性
先来看张图: 容易理解的地方我们就不介绍了,我们这里介绍'公有'和'受保护'的 在shell命令中,公有用is-public=True表示,而受保护的用is-protected表示,公有的反面是is- ...
- Ceph的状态错误
使用命令检查ceph集群的监控状态,得到 [root@node1 ~]# ceph -s cluster c4898b1c-7ac1-406d-bb5d-d3c7980de438 health HEA ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Linux TTY框架【转】
本文转载自:http://ju.outofmemory.cn/entry/281168 1. 前言 由于串口的缘故,TTY是Linux系统中最普遍的一类设备,稍微了解Linux系统的同学,对它都不陌生 ...
- 【python cookbook】【数据结构与算法】15.根据字段将记录分组
问题:想根据字典或者对象实例的某个特定的字典(比如日期)来分组迭代数据 解决方案:itertools.groupby()函数在对数据进行分组时特别有用(前提是先以目标字典进行排序) rows = [ ...
- iOS 警告窗口
- (IBAction)buttonPressed:(id)sender { NSDate *date=self.datePicker.date; NSString *messag ...
- linux下的module_param()解释【转】
转自:http://blog.csdn.net/wavemcu/article/details/7762133 版权声明:本文为博主原创文章,未经博主允许不得转载. ***************** ...
- webapi获取请求地址的IP
References required: HttpContextWrapper - System.Web.dll RemoteEndpointMessageProperty - System.Serv ...
- HDU 2067:小兔的棋盘
小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 使用VisualSVN建立SVN Server
首先去官网下载安装包.http://subversion.apache.org/packages.html找到windows的,选择VisualSVN->VISUALSVN SERVER 双击开 ...