Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

  题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出

  直接线性筛模拟即可

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 bool Is_Primes[1000005];
 5 int Primes[1000005];
 6 int A[1000005];
 7 int cnt;
 8 void Prime(int n){
 9     cnt=0;
10     memset(Is_Primes,0,sizeof(Is_Primes));
11     for(int i=2;i<=n;i++){
12         if(!Is_Primes[i])
13             Primes[cnt++]=i;
14         for(int j=0;j<cnt&&i*Primes[j]<n;j++){
15             Is_Primes[i*Primes[j]]=1;
16             if(i%Primes[j]==0)break;
17         }
18     }
19
20     memset(Is_Primes,0,sizeof(Is_Primes));
21     for(int i=0;i<cnt;i++){
22         Is_Primes[Primes[i]]=1;
23     }
24
25 }
26 int main(){
27     int a,b,n;
28     Prime(1000003);
29     while(scanf("%d%d%d",&a,&b,&n)==3&&a+b+n){
30
31         int ans=0;
32         for(int i=0;i<1000003;i++){
33             A[i]=i*b +a;
34         //    printf("%d %d\n",A[i],i);
35             if(Is_Primes[A[i]])ans++;
36             if(ans==n){
37                 ans=A[i];
38                 break;
39             }
40         }
41         printf("%d\n",ans);
42
43     }
44     return 0;
45 }

Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛

原文地址:https://www.cnblogs.com/ttttttttrx/p/10279617.html

时间: 2024-10-05 05:04:44

Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛的相关文章

Goldbach&#39;s Conjecture POJ - 2262 线性欧拉筛水题 哥德巴赫猜想

题意 哥德巴赫猜想:任一大于2的数都可以分为两个质数之和 给一个n 分成两个质数之和 线行筛打表即可 可以拿一个数组当桶标记一下a[i]  i这个数是不是素数  在线性筛后面加个装桶循环即可 #include<cstdio> #include<cstring> using namespace std; bool Is_Primes[1000005]; int Primes[1000005]; int cnt; void Prime(int n){ cnt=0; memset(Is_

Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)

题意:给一个数 可以写出多少种  连续素数的合 思路:直接线性筛 筛素数 暴力找就行   (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net/nk_test/article/details/46242401 https://blog.csdn.net/qq_40873884/article/details/79124552 https://blog.csdn.net/baoli1008/article/details/50788512

poj 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

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 Theore

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 素数 难度:0

http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool usd[1000002]; bool judge(int x) { if(usd[x])return pm[x]; usd[x] = true; if(x == 2) return pm[x] = true; if(((x & 1) == 0) || (x < 2))return pm[x] =

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 快筛质数

题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考如何筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 1000010 using namespace std; int prime[MAX],primes; bool notp[MAX]; int a,d,n;

POJ - 3006 - Dirichlet&#39;s Theorem on Arithmetic Progressions = 水题

http://poj.org/problem?id=3006 给一个等差数列,求其中的第n个质数,答案保证不超过1e6.n还特别小?!!! 埃筛之后暴力. #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<map> #include<set> #include<stack

(素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit 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 

poj3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16333   Accepted: 8205 Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing

【POJ3006】Dirichlet&#39;s Theorem on Arithmetic Progressions(素数筛法)

简单的暴力筛法就可. 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cctype> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std; 10 11 co