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. 【工具】sublime使用技巧

    Ctrl+N 新建一个编辑区,Ctrl+Shift+C 或!加 Ctrl+E新建一个骨架完好的文件. Ctrl+Shift+P开启命令模式,sshtml 切换html语法. esc退出,Ctrl+`打 ...

  2. js 将XML字符串解析成XML文档 --- attribute construct error--- 空白字符与空格问题

    最近在做xml在线编辑器,遇到一个字符串解析成xml文档的问题,记录一下. 原始xml内容读取自xml文档 <label class="test" id="labe ...

  3. jQuery实现复选框的全选与全不选

    对于复选框的选中checked属性,实在是无力吐槽. 从上图可以看出,当复选框不设置checked属性时,默认没有被选中:其它三种情况,设置checked属性但不设置属性值即置空,或者将checked ...

  4. mysql中int(1)与int(10)的区别

    INT[(M)] [UNSIGNED] [ZEROFILL] 普通大小的整数.带符号的范围是-2147483648到2147483647.无符号的范围是0到4294967295. INT(1) 和 I ...

  5. python学习一 hello world of python

    我使用的环境是 OS:Red Hat Enterprise Linux AS release 4 (Nahant Update 3) PYTHON:2.7.2 如果操作系统没有安装python, 百度 ...

  6. 遮罩 HUD 指示器 蒙板 弹窗

    遮罩 HUD 指示器 蒙板 弹窗 UIAlertView的使用<代理方法处理按钮点击> UIAlertView *alertView = [[UIAlertView alloc] init ...

  7. 原生 js 整理

    常见的事件 window.event     代表着,事件的状态,只有在事件的过程中才有效.

  8. python的unitest的简单使用

    python的unitest的简单使用 unittest提供一个TestLoader类用于自动创建一个测试集并把单个测试放入到测试集中. TestLoader自动运行测试用例以test开头的方法的测试 ...

  9. docker使用registry搭建本地私有仓库

    参考链接来自:http://blog.csdn.net/wangtaoking1/article/details/44180901/ 和Mavan的管理一样,Dockers不仅提供了一个中央仓库,同时 ...

  10. Asp.Net MVC中Controller、Action、View是如何激活调用的

    上篇我们介绍了MVC的路由,知道在注册路由的时候会创建一个MvcHandler将其和Url规则一起放入到了RouteCollection中,之后请求通过UrlRoutingModule,根据当前的UR ...