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. 使用扩展方法重写.NET底层架构

    我们在开发B/S架构的项目时,用到的都是.NET底层各种命名空间提供的操作类,利用扩展类可以重新写出一套真正属于你自己的框架. PS:扫描下方二维码或点击链接,加入QQ群

  2. sharepoint用户信息同步出错

    首先使用工具定位问题,然后权限不够授予权限 C:\Program Files\Microsoft Office Servers\15.0\Synchronization Service\UIShell

  3. Win7系统下搭建FTP

    一.创建FTP站点  1.打开:控制面板---系统和安全---管理工具---Internet 信息服务 2. 建站:右键点击网站---添加FTP站点 3. 输入FTP 站点名称---选择你的 FTP ...

  4. AutoCAD.Net圆弧半径标注延长线

    #region 注册RegApp public static void CheckRegApp(string regapptablename) { Database db = HostApplicat ...

  5. [ActionScript 3.0] 动态链接库

    很多时候,我们为了项目和程序结构更加清晰,需要将发布好的swf放到一个固定的地方供主文件引用,这时就会出现发布好的swf所用的as类路径发生改变,为避免这个问题,需要用到动态链接库,以下做一个简单例子 ...

  6. css中设置background属性

    属性解释 background属性是css中应用比较多,且比较重要的一个属性,它是负责给盒子设置背景图片和背景颜色的,background是一个复合属性,它可以分解成如下几个设置项: backgrou ...

  7. bootstrap-table教程演示

    Bootstrap Admin 效果展示 Table of contents Create Remove Update Export Tree Create 相关插件 bootstrap-valida ...

  8. 解决mysql最大允许传输包不足的问题

    一.报错提示内容和原因 在执行“数据传输”或者“运行SQL文件”时报错误:Got a packet bigger than 'max_allowed_packet' bytes With,表明当前所传 ...

  9. Navigator导航器

    import React, { Component } from 'react';import { Platform, StyleSheet, Text, View, Navigator, Touch ...

  10. linux入门 配置网络

    我因为有自己的ssr,因此想让虚拟机共享我的ssr,并且不想搞的太麻烦,特此记录一下 1. 获取本机的ip 在命令行里输入ipconfig /all 获取本机IP,如下图所示,我本机ip为10.3.7 ...