题意:

设一个等差数列,首元素为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. iOS开发之iOS界面UI

    1.UILabel NSString *str = @"字符串大小"; UIFont *font = [UIFont fontWithName:@"Arial" ...

  2. scala简介

    1.什么是Scala scala官方网址: http://www.scala-lang.org Scala是一种多范式的编程语言,其设计的初衷是要集成面向对象编程和函数式编程的各种特性.Scala运行 ...

  3. metasploit 渗透测试笔记(meterpreter篇)

    0x01 背景 meterpreter作为后渗透模块有多种类型,并且命令由核心命令和扩展库命令组成,极大的丰富了攻击方式. 需要说明的是meterpreter在漏洞利用成功后会发送第二阶段的代码和me ...

  4. Apache Jemeter 开发插件

    为什么选择使用JMeter 当被问到这个问题的时候,也许你会在脑海里产生很多的理由,比如: Apache基金会下的开源项目,没有版权问题: 为数不多的还在持续更新的开源性能自动化测试工具: 支持协议丰 ...

  5. Vue-Router基础使用

    作为Vue生态系统里面的一大成员,Vue-Router主要负责vue中的页面路由及其传值问题. 1.基本使用–添加路由 基本使用主要包括四个部分,页面引入.配置路由数组.实例化路由.把实例化的路由加入 ...

  6. TensorFlow学习之 图像预处理

    import tensorflow as tf import matplotlib.pyplot as plt image_raw_data = tf.gfile.GFile("./pict ...

  7. 随手练——S(n)=O(1),判断一个链表是否为“回文”

    方法一:T(n)=O(n),S(n)=O(n) 走完一遍链表,每个值入栈,之后再走一遍链表,和每次弹出的栈顶进行比较. 核心: LNode *p = l->next; while (p) { s ...

  8. jQuery.mobile.changePage的参数

    选项 类型:对象 属性: allowSamePageTransition(默认值:假的) 类型:布尔 默认情况下,changePage()忽略请求更改为当前活动页面.将此选项设置为true,则允许该请 ...

  9. 谷歌浏览器linux,windows下载

    https://www.chromedownloads.net/ 提取码自己行提取rpm安装包

  10. Shell笔记-04

    如果表达式中包含特殊字符,Shell 将会进行替换.例如,在双引号中使用变量就是一种替换,转义字符也是一种替换. 举个例子: #!/bin/bash a=10 echo -e "Value ...