POJ3006-Dirichlet's Theorem on Arithmetic Progressions
题意:
设一个等差数列,首元素为a,公差为d
现在要求输入a,d,n ,要求找出属于该等差数列中的第n个素数并输出
思路:空间换时间是个主旋律。素数表的生成用素数筛选法。方法是从2开始,对每个目前还标记为素数的数(初始情况下每个数都标记为素数),把它的所有倍数都标记为非素数。这些扫描过去后,一直没被标记的(即保持为素数的)就是所有的素数。
之后的事情就比较简单了,对等差序列中的每个数一个个去查预先生成的素数表,一直数到第n个素数输出即可。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 1000000 int prime[N];//装素数
bool vis[N];//记录该值是否已被访问 void dabiao()//欧拉筛法打表
{
int i,j,cnt=;//cnt为素数个数 memset(prime,,sizeof(prime));
memset(vis,true,sizeof(vis));//先全部假设为素数
vis[]=vis[]=false; for (i=;i<=N;i++)
{
if (vis[i])//找到一个素数
{
prime[cnt++]=i;
} for (j=;j<cnt&&i*prime[j]<=N;j++)
{
vis[i*prime[j]]=false;//打上标记 if (i%prime[j]==)//停止筛选,因为在以后的合数倍筛选中会筛到
{
break;
}
}
}
} void test()
{
int i;
for (i=;i<;i++)
{
printf("%2d",vis[i]);
}
} int main()
{
int i,cnt;
dabiao();
// test(); while ()
{
int a,d,n; scanf("%d %d %d",&a,&d,&n);
cnt=; if (a==&&d==&&n==)
{
break;
} for (i=a;;i+=d)
{
if (vis[i]==true)
{
cnt++;
} if (cnt==n)//找到第n个素数
{
printf("%d\n",i);
break;
}
}
} return ;
}
POJ3006-Dirichlet's Theorem on Arithmetic Progressions的更多相关文章
- 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 (素数)
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 ...
- 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...
- 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 ...
- 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) { ...
- Dirichlet's Theorem on Arithmetic Progression
poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...
- (素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
随机推荐
- codeforces 703E Mishka and Divisors
codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...
- 怎么在Linux环境下通过VS Code调试Python 3+?
今天突然想写一写Python了,于是就开始弄.使用源码包安装好Python3.6.6之后,发现Linux下只能通过python3来调用python 3.6.6.如果直接使用python的话,调用的是系 ...
- js调用echarts getImage方法 将图表转换为img
function chart(opt,id,chartName){//配置option的方法 var chartName = echarts.init(document.getElementById( ...
- 为什么 Category 不能增加成员变量-nonfragile
三.既然是 non-fragile ivars,为什么 Category 不能增加成员变量? 看过一些资料,理由并不是很让人信服.我觉得并不是做不到,只是现在没有做,现在不支持.我在 Opti ...
- Django的模版引擎与模版使用
Django的模版引擎与模版使用 模版引擎是模版响应的后端.模版指的是HTML.css,js等相关的文件.模版引擎是将这些表示层文件与数据相整合在一起,然后将整合后的数据给到响应类型判断采用一次性响应 ...
- R基础-适合于纯小白
#说明 文中‘test’均为模拟名称,原始编码为GKB方式###1.R语言介绍#### 偏统计 ###2.Rstudio和R的基本操作#### #2.1查看R语言自带的数据集#### data() # ...
- 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- ubuntu ibus 输入法总在左下角不跟随光标的处理
sudo apt-get install ibus-gtk ibus-gtk3 ibus-qt4 参考文章
- ethereumjs/ethereumjs-vm-3-StateManager
https://github.com/ethereumjs/ethereumjs-vm/blob/master/docs/stateManager.md StateManager 要与本博客的ethe ...
- 设置af对 Cookies 的管理
manager.requestSerializer.HTTPShouldHandleCookies = NO;