poj3006
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15245 | Accepted: 7641 |
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
Source
#include <iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#define MAXN 1000000
using namespace std; int main()
{
bool vis[MAXN];
memset(vis,false,sizeof(vis));
vis[]=true;
int i,j;
for(i=;i<(int)sqrt((double)MAXN);i++)
{
if(!vis[i])
{
for(j=i*i;j<MAXN;j+=i)
vis[j]=true;
}
}
int a,d,n;
while(scanf("%d%d%d",&a,&d,&n)!=EOF&&a!=&&d!=&&n!=)
{
int sum=,flag=;
for(i=,j=i;i<=n;i++)
{
sum = a + (j-)*d;
if(vis[sum]==false)
j++;
while(vis[sum]==true)
{
j++;
sum = a + (j-)*d;
flag=;
}
if(flag==)
{
j++;
flag=;
} }
cout<<sum<<endl;
}
return ;
}
poj3006的更多相关文章
- 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...
- poj3006 筛选法求素数模板(数论)
POJ:3006 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. 筛选法求素数的大概思路是: 如果a这个数是一个质数,则n*a不是质数. 用一个数组实现就是 ...
- 【转】POJ题目分类推荐 (很好很有层次感)
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...
- POJ推荐50题
此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求, ...
- 【转】ACM训练计划
[转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...
- ACM训练计划建议(写给本校acmer,欢迎围观和指正)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- 【POJ水题完成表】
题目 完成情况 poj1000:A+B problem 完成 poj1002:电话上按键对应着数字.现在给n个电话,求排序.相同的归一类 完成 poj1003:求最小的n让1+1/2+1/3+...+ ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
随机推荐
- EasyUI 使用心得
最近项目中用到EasyUI,总结了一下 注:EasyUI中所有的控件不能重名,否则会出现意向不到的后果.这是EasyUI框架决定的. ① EasyUI 获取文本框中的值 //日期 $('#beginD ...
- activity 的返回按钮
http://www.2cto.com/kf/201210/160251.html 连续点击两次程序就退出程序,这是一个很有趣的程序功能,下来介绍一下我的实现方式(欢迎大家拍砖指点): 1.在Ac ...
- 一个AVRUSB作品HID类
最近为了HID类设备弄了半个把月.终于完成这个无线(红外)多媒体键盘,带鼠标功能.难就难在描述符上,不过在近4天的资料翻阅及多次的测试下,电脑不知道重启了多少次啊,的情况下 搞定了这个东西,大家可以看 ...
- 折腾Python中的Tkinter
折腾Python中的Tkinter 从oschina看到了关于Python的Tkinter简介: Tk图形用户界面 Tkinter 又从Python官网文档: Tkinter — Python int ...
- 电子科大POJ "孤单整数"
孤单整数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) C-sources: ...
- poj 2632 Crashing Robots_模拟
做的差点想吐,调来调去,编译器都犯毛病,wlgq,幸好1a. 题意:给你机器人怎走的路线,碰撞就输出 #include <cstdlib> #include <iostream> ...
- nginx配置方法
nginx配置的代码: user www www; worker_processes 8; error_log /data111/logs/nginx/nginx-error.log crit; pi ...
- GDB 多进程调试
启动: $gdb <file> || $gdb 然后(gdb)file <file> 运行: (gdb)run <该程序本身的命令行参数> 查看代码: (gdb) ...
- linux 内核源代码分析 - 获取数组的大小
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 測试程序: #include<stdio.h> #include<stdlib. ...
- 将ImageView中的图片保存到本地相冊
private void SaveImageToSysAlbum() { if (FileUtil.isSdCardExist()) { BitmapDrawable bmpDrawable = (B ...