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

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
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 1200
bool hash[maxn];
void inithash()
{
int i,j;
for(j=; j<maxn; j+=)
hash[j]=;
for(i=; i<; i+=)
if(!hash[i])
for(j=i*i; j<maxn; j+=i)
hash[j]=;
}
int primenum(int N)
{
int num=;
for(int i=; i<=N; i++)
if(!hash[i])
num++;
return num;
}
int main()
{
inithash();
int N,len,num,tmp;
while(scanf("%d%d",&N,&len)!=EOF)
{
printf("%d %d:",N,len);
num=primenum(N);
if(num%==)
{
if(num-(*len)<=)
{
for(int i=; i<=N; i++)
if(!hash[i])
{
printf(" %d",i);
}
printf("\n\n");
}
else
{
tmp=;
for(int i=; i<=N; i++)
if(!hash[i])
{
if(tmp>=(num-(*len))/&&tmp<*len+(num-(*len))/)
printf(" %d",i);
tmp++;
}
printf("\n\n");
}
}
else
{
if(num-(*len-)<=)
{
for(int i=; i<=N; i++)
if(!hash[i])
{
printf(" %d",i);
}
printf("\n\n");
}
else
{
tmp=;
for(int i=; i<=N; i++)
if(!hash[i])
{
if(tmp>=(num-(*len-))/&&tmp<*len-+(num-(*len-))/)
printf(" %d",i);
tmp++;
}
printf("\n\n");
}
}
}
return ;
}

PoJ 1595 PrimeCuts的更多相关文章

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

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

  2. poj 1595

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

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

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

  4. POJ 1595 素数打表水题

    [题意简述]:给出N和C,让我们求出N以内的包含N的素数,然后依据若N以内的素数为奇数个,就将中间2*c-1个素数输出:若为偶数个.就将中间2*c个素数输出. [分析]:仅仅要题意理解就简单了. 详见 ...

  5. poj 1595 Prime Cuts

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

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

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

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  9. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

随机推荐

  1. eclipsec常用快捷键

    Eclipse常用快捷键 1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索 ...

  2. spring之httpclient doget请求

    /**     * @param url        请求地址     * @param jsonString 加密后的字符串     * @return     * @throws ClientP ...

  3. ROS机器人操作系统在线练习

    废话不说,先看图吧: 1. ROS in 5 Days Entering ROS 2. ROS Navigation in 5 Days Mastering ROS 3. ROS Autonomous ...

  4. JAVA怎么在函数内改变传入的值

    public class TestInt { public int aa(int i) { return i+4; } public static void main(String [] args) ...

  5. mongodb 使用

    一.下载 MongoDB的官网是:http://www.mongodb.org/ MongoDB最新版本下载在官网的DownLoad菜单下:http://www.mongodb.org/downloa ...

  6. OPEN(SAP) UI5 学习入门系列之三:MVC (下) - 视图与控制器

    继续来学习UI5的MVC模型吧,这次我们来探讨视图与控制器. 1 视图 在MVC中,视图用来定义和渲染UI.在UI5中,视图的类型是可以自定义的,除了以下预定义的四种视图类型之外,你也可以定制自己的视 ...

  7. ubuntu 终端命令颜色的修改

    http://blog.chinaunix.net/uid-13954789-id-3137184.html http://blog.chinaunix.net/uid-26021340-id-348 ...

  8. PHP convet class to json data

    /********************************************************************* * PHP convet class to json da ...

  9. 新手向——关于Python3.5在Windows 10 系统下发布模块的终极讲解

    博主自己在发布Python模块的时候也是摸索了好久啊,因为跟着书上写的步骤一步一步来终究会跪的节奏有木有啊!!!几经波折终于搞出来了,贴下来与诸君共勉.之前的步骤相信大家都已经知道了,那我们就直接跳过 ...

  10. BZOJ4831: [Lydsy1704月赛]序列操作(非常nice的DP& 贪心)

    4831: [Lydsy1704月赛]序列操作 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 250  Solved: 93[Submit][Statu ...