Prime Cuts
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10610   Accepted: 4046

Description

A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.

Input

Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.

Output

For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one blank. Each line of output should be followed by a blank line. Hence, your output should follow the exact format shown in the sample output.

Sample Input

21 2
18 2
18 18
100 7

Sample Output

21 2: 5 7 11

18 2: 3 5 7 11

18 18: 1 2 3 5 7 11 13 17

100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67

Source

 

题意:给定你一个数n,让你求出1-n内有多少个素数,再给你一个数d,如果2*d大于素数的个数则全部输出;

否则,如果个数为单数,输出2*d - 1个并且以中间那个素数为中心分别向两边输出d-1个;

如果偶数个,输出2*d个,也是以中间那个素数为中心向两边扩展;————网上查的

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define max 1000
int prime[max+];
void get_prime(int n){
int i;
prime[]=;
prime[]=;
int index=;
for(i=;prime[index]<=n;i++){//得到第一个大于n的素数
int j=;//j不能==0!!
for(;j<=index&&prime[j]*prime[j]<=i;j++){
if(!(i%prime[j])){
break;
}
}
if(prime[j]*prime[j]>i){
prime[++index]=i;
}
}
//cout<<prime[index]<<endl;
}
int main()
{
get_prime(max);
int n,c;
while(cin>>n>>c){
int i=,j,s,e;
while(prime[i]<=n){
i++;
}
if(i%){
if(i<*c-){
s=;
e=i-;
}
else{
s=i/-(c-);
e=i/+(c-);
}
}
else{
if(i<*c){
s=;
e=i-;
}
else{
s=i/--(c-);
e=i/+(c-);
}
}
cout<<n<<" "<<c<<":";
for(j=s;j<=e;j++){
cout<<" "<<prime[j];
}
cout<<endl;
cout<<endl;//格式注意
}
return ;
}

poj 1595 Prime Cuts的更多相关文章

  1. POJ 1595 Prime Cuts (ZOJ 1312) 素数打表

    ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...

  2. ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)

    何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...

  3. poj 1595

    #include <iostream> #define N 10010 using namespace std; int a[N],b[N]; int prime(int a) { int ...

  4. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  5. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  6. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  7. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  8. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  9. PoJ 1595 PrimeCuts

    Prime Cuts Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9339   Accepted: 3562 Descri ...

随机推荐

  1. 菜鸟的Xamarin.Forms前行之路——从新建项目到APP上架各种报错问题解决方法合集(不定时更新)

    出自:博客园-半路独行 原文地址:http://www.cnblogs.com/banluduxing/p/7425791.html 本文出自于http://www.cnblogs.com/banlu ...

  2. Unit Vector Compression

    Recently, I compared a few methods for compressing generic unit vectors. The method used in Cry Engi ...

  3. 「HNOI 2015」落忆枫音

    题目链接 戳我 \(Description\) 给一张\(n\)割点\(m\)条边的\(DAG\),保证点\(1\)不存在入边,现在需要在\(DAG\)中加入一条不在原图中的边\((x,y)\),求这 ...

  4. 一、SecureCRT 8.0 客户端连接服务器

    1.通过远程连接服务器linux,连接的是ssh服务: 如图:ssh2协议是ssh的升级版. 连接模式: 2. Ctrl+d 快速退出==exit/quit/logout 3.SecureCRT 改变 ...

  5. “全栈2019”Java第四十八章:重写方法Override

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. 【bzoj4887】:[Tjoi2017]可乐 矩阵乘法,快速幂

    [bzoj4887]:[Tjoi2017]可乐 题目大意:一张无相连通图(n<=30),从1号点开始走,每秒可以走到相邻的点也可以自爆,求第t秒(t<=1e6)后所有的方案数是多少对201 ...

  7. Linux 下四条高大命令(计划360检测脚本)

    查看进程,按内存从大到小 ps -e -o "%C : %p : %z : %a"|sort -k5 -nr 查看进程,按CPU利用率从大到小排序 ps -e -o "% ...

  8. 深度学习(tensorflow) —— 自己数据集读取opencv

    先来看一下我们的目录: dataset1 和creat_dataset.py 属于同一目录 mergeImg1 和mergeImg2 为Dataset1的两子目录(两类为例子)目录中存储图像等文件 核 ...

  9. js 平均分割

    let alllist=res.data; var result = []; for (var i = 0; i < alllist.length; i += 3) { result.push( ...

  10. [Ruby]Unzipping a file using rubyzip

    link: http://www.markhneedham.com/blog/2008/10/02/ruby-unzipping-a-file-using-rubyzip/ require 'ruby ...