Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16733 | Accepted: 8427 |
Description
If a and d are relatively prime positive integers, the arithmetic sequence beginning with
a and increasing by d, i.e., a, a + d,
a + 2d, a + 3d, a + 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
a, d, and n.
Input
The input is a sequence of datasets. A dataset is a line containing three positive integers
a, d, 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 a, d, n 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
欧拉筛选改进代码
#include <cstdio>
#include <string.h>
#include <cmath>
#include <iostream>
#include <algorithm>
#define WW freopen("output.txt","w",stdout)
using namespace std;
const int Max=1000000;
bool prime[Max];
int main()
{
memset(prime,false,sizeof(prime));
prime[1]=true;
for(int i=2;i*i<=Max;i++)
{
if(!prime[i])
{
for(int j=i*i;j<Max;j+=i)
prime[j]=true;
}
}
int a,b,n;
while(scanf("%d %d %d",&a,&b,&n))
{
if(a==0&&b==0&&n==0)
break;
int top=0;
for(int i=a;;i+=b)
{
if(!prime[i])
top++;
if(top==n)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏的更多相关文章
- IIS上虚拟站点的web.config与主站点的web.config冲突解决方法 分类: ASP.NET 2015-06-15 14:07 60人阅读 评论(0) 收藏
IIS上在主站点下搭建虚拟目录后,子站点中的<system.web>节点与主站点的<system.web>冲突解决方法: 在主站点的<system.web>上一级添 ...
- leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏
for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...
- 二分图匹配(KM算法)n^3 分类: ACM TYPE 2014-10-01 21:46 98人阅读 评论(0) 收藏
#include <iostream> #include<cstring> #include<cstdio> #include<cmath> const ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- C++ Virtual介绍 分类: C/C++ 2015-06-16 21:36 26人阅读 评论(0) 收藏
参考链接:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html 学过C++的人都知道在类Base中加了Virtual关键字的函 ...
- 跨服务器修改数据 分类: SQL Server 2014-08-21 21:24 316人阅读 评论(0) 收藏
说明: 两个服务器: 192.168.0.22 A 192.168.0.3 B 数据库备份在A上 数据库在B上 在A上写: exec sp_addlinkedserver 'ITSV ...
- 树莓派入手(烧写系统,调整分区,配置Java环境,串口GPS配置) 分类: Raspberry Pi 2015-04-09 21:13 145人阅读 评论(0) 收藏
原来的tf卡无故启动不起来,检查发现其文件系统分区使用率为0%. 数据全部丢失!!!!! 血的教训告诉我们备份文件系统的重要性,一切需要重头来.... 烧录系统 安装系统有两种方式, NOOBS工具安 ...
- UI基础:UITextField 分类: iOS学习-UI 2015-07-01 21:07 68人阅读 评论(0) 收藏
UITextField 继承自UIControl,他是在UILabel基础上,对了文本的编辑.可以允许用户输入和编辑文本 UITextField的使用步骤 1.创建控件 UITextField *te ...
- Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏
Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...
随机推荐
- c# Start/Stop/Check Status远程计算机的Windows Service
static void Main(string[] args) { ConnectionOptions op = new ConnectionOptions(); // 登陆远程计算机的远程, op. ...
- GTA项目 二, JSON接口开放跨域访问
public class CORSAttribute : ActionFilterAttribute { public string Domains { get; set; } public CORS ...
- NABCD模式
各位用户: 我们的“昵妆”是为了帮助不会化妆的用户解决困难, 他们需要有适合他们的优质的化妆品和 正确的视频或者化妆师来指导他们,但是现有的方案并没有很好地解决这些需求,我们有独特的办法, ...
- Lintcode: O(1) Check Power of 2
Using O(1) time to check whether an integer n is a power of 2. Example For n=4, return true For n=5, ...
- JavaScript内的类型转换
JavaScript内的类型转换 1.分为自动转换和强制转换,我们一般用强制转换.其他类型转换为整数是parseInt();其他类型转化为小数parseFloat(); 2.判断是不是一个合法数字 ...
- SQL top查询
select *from emp;
- dij最短路优先队列堆的时候,加边
不能用全局数组d[u]>d[rhs.u]. 这样后面会修改d[u]值然而本来里面的点顺序不该修改,却被修改了. 应该用栈还存进去的临时变量,比如d>rhs.d. 优先队列重载小于号'< ...
- 判断java中两个对象是否相等
java中的基本数据类型判断是否相等,直接使用"=="就行了,相等返回true,否则,返回false. 但是java中的引用类型的对象比较变态,假设有两个引用对象obj1,obj2 ...
- C++新手之详细介绍MFC
MFC (Microsoft Foundation Class Library)中的各种类结合起来构成了一个应用程序框架,它的目的就是让程序员在此基础上来建立Windows下的应用程序,这是一种相对 ...
- [php笔记]项目开发五个阶段/雇员管理系统
zend 公司,管理PHP版本的升级. 功能强大, 官方推荐. (开发一个PHP项目) 软件开发的五个阶段. 1.创建一个项目(工程)2.设置该项目的路径3.创建一个文件test.php ***使用Z ...