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. ffmpeg+EasyDSS流媒体服务器实现稳定的rtmp推流直播

    本文转自EasyDarwin团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/74783269 需求 在做EasyDSS开发时,总 ...

  2. $timeout

    $timeout 会在执行后刷新页面上 与angular 相关的变量,在于jQuery共用修改页面变量时,这很可能会导致刷新跳动的现象:

  3. 关于Instruments-Leaks工具的归纳总结

    前言: 本篇文章,在于学习,我把别人的一些感觉好的文章汇总成了一篇,亲自实现了一下,留用于今后学习资料. 文章脉络: 文章脉络: 一.内存优化 简介:Objective_C 有3种内存管理方法, 它们 ...

  4. iOS LZMA类压缩算法使用

    LZMA(Lempel-Ziv-Markov chain-Algorithm的缩写)是2001年以来得到发展的一个数据压缩算法,它用于7-Zip归档工具中的7z格式和 Unix-like 下的 xz  ...

  5. Swift 菊花、UIPageControl和UIProgressView

    // Make: 加载 菊花 func _initUIActivityIndicatorView() { let activity = UIActivityIndicatorView(activity ...

  6. python 帮助和导入路径

    1 help('**'),查看函数用法,其中**为内置的函数 help(**.**),模块.函数,查看非内置函数 2 先添加路径到sys,再导入 import sys;sys.path.append( ...

  7. U-Boot 不能识别FAT32 SD分区

    /********************************************************************* * U-Boot 不能识别FAT32 SD分区 * 说明: ...

  8. apue 2ed 和 unp 3ed 环境配置

    网上虽然有很多配置攻略,但是依然会一头雾水,下面记录我的配置过程. OS. Ubuntu 10.04 LTS 5 首先下载APUE源代码(http://www.apuebook.com/src.tar ...

  9. Luogu3387 缩点 【tarjan】【DP】

    Luogu3387 缩点 题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点, ...

  10. [LeetCode系列]N皇后问题递归解法 -- 位操作方式

    N皇后问题: 给定8*8棋盘, 放置n个皇后, 使其互相不能攻击(即2个皇后不能放在同一行/列/正反对角线上), 求解共有多少种放置方式? 这个问题的解答网上有不少, 但是位操作解法的我看到的不多. ...