Dirichlet's Theorem on Arithmetic Progressions
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15398   Accepted: 7714

Description

If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., aa + da + 2da + 3da + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theorem on Arithmetic Progressions, which had been conjectured by Johann Carl Friedrich Gauss (1777 - 1855) and was proved by Johann Peter Gustav Lejeune Dirichlet (1805 - 1859) in 1837.

For example, the arithmetic sequence beginning with 2 and increasing by 3, i.e.,

2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, 77, 80, 83, 86, 89, 92, 95, 98, ... ,

contains infinitely many prime numbers

2, 5, 11, 17, 23, 29, 41, 47, 53, 59, 71, 83, 89, ... .

Your mission, should you decide to accept it, is to write a program to find the nth prime number in this arithmetic sequence for given positive integers ad, and n.

Input

The input is a sequence of datasets. A dataset is a line containing three positive integers ad, and n separated by a space. a and d are relatively prime. You may assume a <= 9307, d <= 346, and n <= 210.

The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

Output

The output should be composed of as many lines as the number of the input datasets. Each line should contain a single integer and should never contain extra characters.

The output integer corresponding to a dataset adn should be the nth prime number among those contained in the arithmetic sequence beginning with a and increasing by d.

FYI, it is known that the result is always less than 106 (one million) under this input condition.

Sample Input

367 186 151
179 10 203
271 37 39
103 230 1
27 104 185
253 50 85
1 1 1
9075 337 210
307 24 79
331 221 177
259 170 40
269 58 102
0 0 0

Sample Output

92809
6709
12037
103
93523
14503
2
899429
5107
412717
22699
25673

Source

 
 #include <stdio.h>
#include <string.h>
#define MAX 1000000
int s[MAX];
int main()
{
int a,d,n,i,j,k;
memset(s,,sizeof(s));
s[]=;
for(i=;i<MAX/;i++)
{
if(!s[i])
{
for(j=i+i;j<MAX;j+=i)
s[j]=;
}
}
while(scanf("%d%d%d",&a,&d,&n),a||d||n)
{
int num=,t;
for(i=;;i++)
{
if(s[a+i*d]==)
{
num++;
if(num==n)
{
t=a+i*d;
break;
}
}
}
printf("%d\n",t);
}
return ;
}

//本想着会超时,没想到竟然没有超时,100万以内的素数282MS

poj_3006_Dirichlet's Theorem on Arithmetic Progressions_201407041030的更多相关文章

  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. Dirichlet's Theorem on Arithmetic Progression

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

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

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

  4. Fundamental theorem of arithmetic 为什么1不是质数

    https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic In number theory, the fundamental th ...

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

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

  6. (素数求解)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 ...

  7. 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 ...

  8. 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 ...

  9. POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 快筛质数

    题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考怎样筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <c ...

随机推荐

  1. 升级Python2.7

    习惯于centos6.5的系统,却习惯使用Python2.7,每次都会升级Python版本,安装pip.于是总结了升级过程中遇到问题(原创) 升级Python2.7 注意的是 1.编译2.7时依赖问题 ...

  2. ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第五天(非原创)

    文章大纲 一.课程介绍二.前台系统(门户系统)搭建介绍三.前台系统(门户系统)搭建实战四.js请求跨域解决五.项目源码与资料下载六.参考文章   一.课程介绍 一共14天课程(1)第一天:电商行业的背 ...

  3. [BZOJ1798][AHOI2009]Seq维护序列 线段树

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 一眼看过去线段树,事实上就是线段树.对于乘和加的两个标记,我们可以规定一个顺序,比如 ...

  4. absolute元素水平居中

    原始(未居中): .con{ width:200px; height:200px; background:#ccc; position:relative; } .abs{ width:40px; he ...

  5. 使用Glide加载Android图片

    一.概述 Glide是一个在Android端非常好的图片缓冲工具,总体上来说,他有以下优点 使用简单 自适应程度高 支持常见的图片格式,如jpg,png等 支持多种数据源,网络,本地,资源,Asset ...

  6. CCF|游戏|Java

    import java.util.Scanner; public class tyt { public static void main(String[] args) { Scanner in = n ...

  7. .Net Mvc EasyUI DataGrid 分页

    由于项目的需要,最近一直在学习 .net MVC 和EasyUI.上周写了一个<.Net Mvc 返回Json,动态生成EasyUI Tree>,今天再写一个EasyUI中另一个重要的组件 ...

  8. 网页制作常用的CSS知识

    在制作网页中,我们会用到很多CSS的知识,在这里我简单的总结了一些. div    划分区块 ul,li 无序列表(配合划分区块) ol,li 有序列表 a 超链接标签 p 段落标签 h 标题标签 i ...

  9. sosoapi的安装

    sosoapi简介及其用户手册:http://www.sosoapi.com/pass/help/manual.htm 该随笔的大概分为: 1.sosoapi的基础安装  2.sosoapi使用域名访 ...

  10. windows环境开启PHP fileinfo扩展

    fileinfo作用:本模块中的函数通过在文件的给定位置查找特定的 魔术 字节序列 来猜测文件的内容类型以及编码(通俗来讲就是获取文件的MIME信息) 开启PHP fileinfo扩展的方法: 1.下 ...