题意:

设一个等差数列,首元素为a,公差为d

现在要求输入a,d,n ,要求找出属于该等差数列中的第n个素数并输出

思路:空间换时间是个主旋律。素数表的生成用素数筛选法。方法是从2开始,对每个目前还标记为素数的数(初始情况下每个数都标记为素数),把它的所有倍数都标记为非素数。这些扫描过去后,一直没被标记的(即保持为素数的)就是所有的素数。

之后的事情就比较简单了,对等差序列中的每个数一个个去查预先生成的素数表,一直数到第n个素数输出即可。

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 1000000 int prime[N];//装素数
bool vis[N];//记录该值是否已被访问 void dabiao()//欧拉筛法打表
{
int i,j,cnt=;//cnt为素数个数 memset(prime,,sizeof(prime));
memset(vis,true,sizeof(vis));//先全部假设为素数
vis[]=vis[]=false; for (i=;i<=N;i++)
{
if (vis[i])//找到一个素数
{
prime[cnt++]=i;
} for (j=;j<cnt&&i*prime[j]<=N;j++)
{
vis[i*prime[j]]=false;//打上标记 if (i%prime[j]==)//停止筛选,因为在以后的合数倍筛选中会筛到
{
break;
}
}
}
} void test()
{
int i;
for (i=;i<;i++)
{
printf("%2d",vis[i]);
}
} int main()
{
int i,cnt;
dabiao();
// test(); while ()
{
int a,d,n; scanf("%d %d %d",&a,&d,&n);
cnt=; if (a==&&d==&&n==)
{
break;
} for (i=a;;i+=d)
{
if (vis[i]==true)
{
cnt++;
} if (cnt==n)//找到第n个素数
{
printf("%d\n",i);
break;
}
}
} return ;
}

POJ3006-Dirichlet's Theorem on Arithmetic Progressions的更多相关文章

  1. Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  4. 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)

    简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...

  5. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 素数 难度:0

    http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool u ...

  6. poj 3006 Dirichlet's Theorem on Arithmetic Progressions

    题目大意:a和d是两个互质的数,则序列a,a+d,a+2d,a+3d,a+4d ...... a+nd 中有无穷多个素数,给出a和d,找出序列中的第n个素数 #include <cstdio&g ...

  7. Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

    题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...

  8. Dirichlet's Theorem on Arithmetic Progressions

    http://poj.org/problem?id=3006 #include<stdio.h> #include<math.h> int is_prime(int n) { ...

  9. Dirichlet's Theorem on Arithmetic Progression

    poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...

  10. (素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

随机推荐

  1. 沉淀再出发:使用python进行机器学习

    沉淀再出发:使用python进行机器学习 一.前言 使用python进行学习运算和机器学习是非常方便的,因为其中有很多的库函数可以使用,同样的python自身语言的特点也非常利于程序的编写和使用. 二 ...

  2. CentOS 6 系统启动流程

    第一阶段: BIOS启动引导 主板加电,系统自动载入BIOS(Basic Input Output System)系统 BIOS载入CMOS,读取CMOS中设定的硬件工作参数 BIOS进行POST自检 ...

  3. Who are you, What is the science

    Please read:  地球月球有多大? 我们乃至我们赖以生存的地球, 甚至是我们硕大的银河系放到茫茫大宇中真的不过是一粒尘埃, 我们司空见惯的事物,我们习以为常的生活,我们笃定信奉的科学, 是不 ...

  4. Linux中安装Nginx

    1.安装编译文件及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel 2.安装PCRE,Ngi ...

  5. Angular2 Router路由相关

    路由设置 Angular中路由的配置应该按照先具体路由到通用路由的设置,因为Angular使用先匹配者优先的原则. 示例: 路由设置如下: export const reportRoute: Rout ...

  6. python中的装饰函数

    在面向对象(OOP)的设计模式中,decorator被称为装饰模式.OOP的装饰模式需要通过继承和组合来实现,而Python除了能支持OOP的decorator外,直接从语法层次支持decorator ...

  7. gluoncv训练faster rcnn的一点小问题

    gt数目超过上限. https://github.com/dmlc/gluon-cv/pull/335/files

  8. Python中的类(一)

    Python中的类(一) 一. 应用场景 如果多个函数中有一些相同的参数时,转换成面向对象. 二. 如何创建类 类是用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法 ...

  9. POJ3690 Constellations

    嘟嘟嘟 哈希 刚开始我一直在想二维哈希,但发现如果还是按行列枚举的话会破坏子矩阵的性质.也就是说,这个哈希只能维护一维的子区间的哈希值. 所以我就开了个二维数组\(has_{i, j}\)表示原矩阵\ ...

  10. NSOJ 4621 posters (离散化+线段树)

    posters 时间限制: 1000ms 内存限制: 128000KB 64位整型:      Java 类名: 上一题 提交 运行结果 统计 讨论版 下一题 题目描述 The citizens of ...