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. iOS开发之如何应对苹果app的ipv6时代?

    WWDC2015苹果宣布在ios9支持纯IPv6的网络服务,并且要求2016年提交到app store的应用必须兼容纯IPv6的网络,要求适配的系统版本是ios9以上(包括ios9). 一 背景介绍 ...

  2. tomcat的安装和配置

    本人java开发的菜鸟工程师,这几天学习了tomcat的安装和使用,终于在今天运行成功. 一.tomcat的安装 1.tomcat下载网址:http://tomcat.apache.org/ 2.打开 ...

  3. 原 the app referencesnon-public selectors in payload

    摘要 当我们上传验证的时候,出现了the app referencesnon-public selectors in payload/项目名.app/项目:字符 的警告的解决办法 当我们上传验证的时候 ...

  4. freeradius连接mysql数据库慢

    [环境说明] 服务器版本         redHat5.3 mysql版本         MySQL5.6.22 freeradius版本   2.1.12 [问题描述] 配置好freeradiu ...

  5. HDU3652 B-number 数位DP第二题

    A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- ...

  6. 使用 MSBuild 响应文件 (rsp) 来指定 dotnet build 命令行编译时的大量参数

    在为开源项目 easiwin/MSTestEnhancer 进行持续集成编译时,需要在编译命令中传入较多的参数.这对于新接手此项目的人来说,成本还是高了一点儿.本文将介绍 MSBuild 响应文件 ( ...

  7. 将 async/await 异步代码转换为安全的不会死锁的同步代码

    在 async/await 异步模型(即 TAP Task-based Asynchronous Pattern)出现以前,有大量的同步代码存在于代码库中,以至于这些代码全部迁移到 async/awa ...

  8. 《selenium2 python 自动化测试实战》(21)——unittest单元测试框架解析

    unittest是展开自动化测试的基础——这个框架很重要! 我们先自己写一个测试类: 1.被测试类 Widthget.py: # coding: utf-8 class Widthget:    de ...

  9. Android源代码分析之拍照、图片、录音、视频和音频功能

    Android源代码分析之拍照.图片.录音.视频和音频功能   //选择图片 requestCode 返回的标识 Intent innerIntent = new Intent(Intent.ACTI ...

  10. Mysql中五级权限小结

    mysql的权限控制主要是通过mysql库下的db,user,host,table_priv,column_priv表控制. 由于权限信息数据量比较小,所以mysql在启动时会将所有的权限消息加载到内 ...