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,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...
随机推荐
- zabbix服务器监控suse系统教程
zabbix服务器监控suse系统教程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 花了近一个星期才学会了如何监控window和linux主机的基本信息以及报价情况(我已经把笔记 ...
- (转)json+flexgrid+jbox组合运用页面刷新<jsp>
插件效果 1.JSP页面 1 <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- Codeforce Round #226 Div2
这次CF虽然,但是- - 第一题看了很久的题目意思额,虽然慢了点- -,但还算没出错,还学会了hack了- -,还+了100- - 第二题想了很久- -...后来发现可以暴力- -,哎 第三题本来也应 ...
- prezi破解教程
http://www.joenchen.com/archives/998 http://www.joenchen.com/archives/945 Prezi Desktop 4.7.5免注册无时间限 ...
- bzoj4547 小奇的集合
当序列中最大和次大都是负数的时候,其相加会是一个更小的负数,因此答案为(Σai)+(m1+m2)*k,如果最大是正数次大是负数,那么一直相加直到两个数都为正数,当最大和次大都是正数时,做一下矩阵乘法即 ...
- 关于 static 的用途
1.三个作用 第一个作用是 隐藏 输出: Hello 所有未加static前缀的全局变量和函数都具有全局可见性,其它的源文件也能访问.此例中,a是全局变量,msg是函数,并且都没有加static前缀, ...
- 实现listview的条目点击后改变背景颜色
gv_categoryeffect_gridview.setChoiceMode(GridView.CHOICE_MODE_SINGLE);,再设置一个selector的背景选择器 getResour ...
- CDC
CDC中最重要的问题是metastability问题. 加入Synchronizer来进行异步时钟的同步,两级的Sync,第二级仍然会出现亚稳态的概率由MTBF决定. MTBF:mean time b ...
- HTML5,添加图片
<img src="0.jpg" width="100" height="150" alt="11">
- 查看lnmp 编译参数
nginx :版本/opt/local/nginx/sbin/nginx -v 编译参数:/opt/local/nginx/sbin/nginx -V apache:版本/opt/local/http ...