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,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...
随机推荐
- java Servlet中的过滤器Filter
web.xml中元素执行的顺序listener->filter->struts拦截器->servlet. 1.过滤器的概念 Java中的Filter 并不是一个标准的Servlet ...
- Lintcode: Nth to Last Node in List
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Exam ...
- Java基础(40):Java中的集合介绍---Collection与Map
集合类说明及区别Collection├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap└WeakHashMap ...
- java的I/O操作:文件的路径
package solutions; import java.io.*; /** * Created by Administrator on 2016/3/14. */ public class Re ...
- css3:与背景的相关样式
1. (1)background-origin : border-box | padding-box | content-box;(设置元素背景图片的原始起始位置.) //需要注意的是,如果背景不是n ...
- geotools
http://blog.tigerlihao.cn/2010/01/geotools-based-web-map-service.html
- 3D语音天气球(源码分享)——创建可旋转的3D球
开篇废话: 在9月份时参加了一个网站的比赛,比赛的题目是需要使用第三方平台提供的服务做出创意的作品. 于是我选择使用语音服务,天气服务,Unity3D,Android来制作一个3D语音天气预报,我给它 ...
- sql server output用法说明
带有output的insert语句. @@identity只能返回当前会话最后生产的标识列. 如果一次性插入多条语句的话. 需要返回这些自动生产的标识列. 那么outpu就派上用场了. declar ...
- android之费电检查 BetterBatteryStats
今天老大给了一个任务,是说我们的应用在后台时,还会比较费电!让我查一下 我立马头大了!无从下手! 一.赶紧百度,得到以下几个信息: ①费电的操作有:大数据量的传输;不停的在网络间切换;解析大量的文本数 ...
- C# 时间现实问题(12小时制与24小时制)
最近在修改项目中遇到时间问题,12小时制与24小时制的问题,想再次跟各位同仁提个醒. yyyy-MM-dd HH:mm:ss------大写的HH为24小时制 yyyy-MM-dd hh:mm:ss- ...