(素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
cid=1006#status//I/0" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="font-family:Verdana,Arial,sans-serif; font-size:1em; border:1px solid rgb(211,211,211); background-color:rgb(227,228,248); color:rgb(85,85,85); display:inline-block; position:relative; padding:0px; margin-right:0.1em; zoom:1; overflow:visible; text-decoration:none">Status
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 aand 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 <iostream>
#include <cmath>
using namespace std;
bool ss(int x)
{
if(x < 2)
return 0;
else
{
for(int i = 2; i <= sqrt((float)x); i++)
if(x % i == 0)
return 0;
return 1;
}
} int main()
{
int a, d, n;
while (cin>>a>>d>>n)
{
if (a==d&&d==n&&n==0)
{
return 0;
}
int i, count = 0;
for (i = a; count < n; i += d) {
if (ss(i))
{
count++;
}
}
cout<<i-d<<endl;
}
return 0;
}
(素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)的更多相关文章
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 快筛质数
题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考怎样筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <c ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions 素数 难度:0
http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool u ...
- 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions
题目大意:a和d是两个互质的数,则序列a,a+d,a+2d,a+3d,a+4d ...... a+nd 中有无穷多个素数,给出a和d,找出序列中的第n个素数 #include <cstdio&g ...
- Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛
题意 给出a d n 给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...
- Dirichlet's Theorem on Arithmetic Progressions
http://poj.org/problem?id=3006 #include<stdio.h> #include<math.h> int is_prime(int n) { ...
随机推荐
- linux编译安装protobuf2.5.0
1.下载安装包 https://github.com/google/protobuf/releases?after=v3.0.0-alpha-4.1 找到相应的版本下载 2.解压安装包 #.tar.g ...
- Boolean占几个字节
Boolean:1.1bit 2.1byte 3.4byte 简书地址: http://www.jianshu.com/p/2f663dc820d0 官网地址: http://docs. ...
- C#中大批量导入数据SqlBulkCopy
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 使用纯 CSS 实现 Google Photos 照片列表布局
文章太长,因为介绍了如何一步一步进化到最后接近完美的效果的,不想读的同学可以直接跳到最后一个大标题之后看代码.demo及原理就好,或者也可以直接看下面这个链接的源代码. 不过还是建议顺序读下去,因为后 ...
- Matplotlib库常用函数大全
Python之Matplotlib库常用函数大全(含注释) plt.savefig(‘test’, dpi = 600) :将绘制的图画保存成png格式,命名为 test plt.ylabel(‘Gr ...
- 4) 十分钟学会android--建立第一个APP,启动另一个Activity
在完成上一课(建立简单的用户界面)后,我们已经拥有了显示一个activity(一个界面)的app(应用),该activity包含了一个文本字段和一个按钮.在这节课中,我们将添加一些新的代码到MyAct ...
- 根据业务自己设计的.NET工厂模式架构
最近项目的架构需要做调整优化,根据业务需要写了一个简单的工厂模式架构 项目介绍:整个系统分为三大平台(这里用A,B,C来标示),每个平台又细分为多个APP客户端(每个APP都有appid来区分) 因为 ...
- ArrayList集合如何存储基本数据类型
ArrayList对象不能存储基本类型,只能存储引用类型的数据.类似 <int> 不能写,但是存储基本数据类型对应的包装类型是可以的. 所以,想要存储基本类型数据, <> 中的 ...
- day25-1 time,datetime模块
目录 time 为什么要有time模块,time模块有什么用 时间戳形式 格式化时间 结构化时间 各种时间格式互相转换 datetime 为什么要有datetime模块,detatime模块有什么用 ...
- NGUI学习随笔
一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加.(注:用Component添加无效). 2. ...