【暴力】POJ-3006 Dirichlet's Theorem on Arithmetic Progressions

来源:互联网 发布:mac pro 贴膜涂层脱落 编辑:程序博客网 时间:2024/05/01 10:03
Dirichlet's Theorem on Arithmetic Progressions
Time Limit: 1000MS Memory Limit: 65536K   

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

FYI, it is known that the result is always less than 106 (one million) under this input condition.

Sample Input

367 186 151179 10 203271 37 39103 230 127 104 185253 50 851 1 19075 337 210307 24 79331 221 177259 170 40269 58 1020 0 0

Sample Output

928096709120371039352314503289942951074127172269925673
————————————————————暴力の分割線————————————————————
思路:就先打个1e6的素数表,然后暴力来吧。。。
注意,还挺难调的。首先将a减去一个d,然后从第1个开始,每加上d判断一次,找到第n个素数。
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 1e6;int p[N];bool vis[N];void get_prime(){int cnt = 0;vis[1] = 1;for(int i = 2; i < N; i++) {if(!vis[i]) p[cnt++] = i;for(int j = 0; j < cnt && p[j]*i < N; j++) {vis[p[j]*i] = 1;if(i % p[j] == 0) break;}}}int main(){#ifdef J_Surefreopen("000.in", "r", stdin);freopen("999.out", "w", stdout);#endifget_prime();int a, d, n;while(scanf("%d%d%d", &a, &d, &n), a||d||n) {a -= d;int i = 1;while(i <= n) {a += d;if(!vis[a]) i++;}printf("%d\n", a);}return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 六个月宝宝喝贝因美过敏怎么办 超市盘点少的货扣员工工资怎么办 孩子东西在学校被偷了怎么办 想退出却不舍的感情不知怎么办? 我被老板摸全身失眠有阴影怎么办 不知道绑定支付宝的微博账号怎么办 银行卡丢了补办后支付宝账户怎么办 未激活的银行卡被锁定了怎么办 手机网银上的钱被盗走了怎么办 支付宝支付失败银行卡扣了钱怎么办 小程序的二次验证码丢失怎么办 苹果手机不小心购买游戏币怎么办 在淘宝买的洗衣粉是假的怎么办 京东快递电话填错了怎么办 新买的手机卡被注册过支付宝怎么办 手机卡掉了别人登陆支付宝怎么办 手机卡注销了支付宝欠的钱怎么办 手机丢了支付宝和微信里的钱怎么办 新办的手机号已被注册支付宝怎么办 新办的移动卡不能注册支付宝怎么办 新办的宝卡支付宝注册过怎么办 办支付宝后银行卡被盗刷怎么办 微信扫二维码送平衡群发了怎么办车 拼多多没收到货点了确认收货怎么办 拼多多收货地址填错了怎么办 京东订单申请退款已发货怎么办 手机播放声音的地方进水了怎么办 手机出声音的地方进水了怎么办 手机听声音的地方进水了怎么办 装修公司我只要基装她不肯怎么办 雄迈未来家庭忘记账号和密码怎么办 地方文件和国家政策有冲突怎么办? 网上贷款放款成功没有到账怎么办 我来贷放款中不到账怎么办 手持身份证被别人网贷了怎么办 身份证被别人做了网贷怎么办 身份证丢了被别人网贷了怎么办 户口身份证被注销了网贷怎么办 首付交了贷款办不下来怎么办 付首付后贷款没有批下来怎么办 交了首付贷款没有办下来怎么办