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

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<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<algorithm> using namespace std; const int N=; int prime[N],isprime[N]; void getPrime(){
for(int i=;i<N;i++)
prime[i]=;
prime[]=prime[]=;
int cnt=;
for(int i=;i<N;i++){
if(prime[i])
isprime[++cnt]=i;
for(int j=;j<=cnt && i*isprime[j]<N;j++){
prime[i*isprime[j]]=;
if(i%isprime[j]==)
break;
}
}
} int main(){ //freopen("input.txt","r",stdin); getPrime();
int a,d,n;
while(~scanf("%d%d%d",&a,&d,&n)){
if(a== && d== && n==)
break;
int cnt=,tmp=a;
if(prime[tmp])
cnt++;
while(cnt<n){
tmp+=d;
if(prime[tmp])
cnt++;
}
printf("%d\n",tmp);
}
return ;
}

POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)的更多相关文章

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

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

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

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

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

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

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

  6. Dirichlet's Theorem on Arithmetic Progressions

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

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

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

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

  9. Dirichlet's Theorem on Arithmetic Progression

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

随机推荐

  1. JDBC在Java Web中的应用——分页查询

    分页查询 通过JDBC实现分页查询的方法有很多种,而且不同的数据库机制也提供了不同的分页方式,在这里介绍两种非常典型的分页方法. 通过ResultSet的光标实现分页 通过ResultSet的光标实现 ...

  2. 第四章 JVM垃圾回收算法

    说明:在阅读本篇之前,需要知道怎么判断对象的存活与否,见<第三章 JVM内存回收区域+对象存活的判断+引用类型+垃圾回收线程> 注意:本文主要参考自<分布式Java应用:基础与实践& ...

  3. 什么是ScaleIO中的forwards rebuild和backwards rebuild?

    Forward rebuild发生在磁盘或节点挂掉的时候. 这种rebuild的过程的load是均衡到protection domain中所有可用的磁盘和节点上的. 这种rebuild相对于backw ...

  4. python中的exec()、eval()以及complie()

    参考博客:http://www.cnblogs.com/yyds/p/6276746.html 1.eval函数 函数的作用: 计算指定表达式的值.也就是说它要执行的python代码只能是单个表达式( ...

  5. linux安装scikit-learn

    原文:http://www.cnblogs.com/cyttina/archive/2013/06/08/3127345.html ubuntu的看官方的文档就好了. http://scikit-le ...

  6. React Native for Android 热部署图片自己定义方案

    情景 热部署时,我们期望升级包中包括js代码与图片资源. bundle的热部署网上已经有两种方案了,一种是用反射,一种是利用RN自带函数.将bundle初始化时直接放到指定文件夹下,之后通过替换bun ...

  7. 遭遇java.lang.NoClassDefFoundError: org/apache/tomcat/PeriodicEventListener

    前天还正常的程序,今天忽然无法启动了,MyEclipse的Console提醒我如下错误: 严重: Error deploying web application directory rttsbizja ...

  8. 隐马尔可夫模型HMM与维特比Veterbi算法(一)

    隐马尔可夫模型HMM与维特比Veterbi算法(一) 主要内容: 1.一个简单的例子 2.生成模式(Generating Patterns) 3.隐藏模式(Hidden Patterns) 4.隐马尔 ...

  9. (笔试题)小米Git

    题目: git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需 ...

  10. Mapnik读取PostGIS数据渲染图片

    __author__ = 'Administrator' # encoding: utf-8 import sys import datetime import mapnik m = mapnik.M ...