题意:

设一个等差数列,首元素为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的更多相关文章

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

  2. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  3. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  4. 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)

    简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...

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

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

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

    题意 给出a d n    给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...

  8. Dirichlet's Theorem on Arithmetic Progressions

    http://poj.org/problem?id=3006 #include<stdio.h> #include<math.h> int is_prime(int n) { ...

  9. Dirichlet's Theorem on Arithmetic Progression

    poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...

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

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

随机推荐

  1. Web Service超限

    问题现状: {System.ServiceModel.CommunicationException: An error occurred while receiving the HTTP respon ...

  2. 【SQL.基础构建-第三节(3/4)】

    --      Tips:聚合和排序 --    一.对表进行聚合查询 -- 1.聚合函数 -- (1)5 个常用函数: -- ①COUNT:计算表中的记录(行)数. -- ②SUM:计算表中数值列的 ...

  3. Java自带线程池和队列详解

    Java线程池使用说明 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极其之前的jdk版本中,关于线程池的使用是极其简陋的.在jdk1.5之后这一情况有了很大的改观.Jdk1.5之后 ...

  4. langularJs的MVC模式

    1.数据的挂载 在函数中设置数据 function Aaa($scope){ $scope.name = 'hello'; $scope.age = '20'; } 2.ng-controller 这 ...

  5. 泛型和 Any 类型

    泛型和 Any 类型 这两个类型看起来很相似,但是一定要小心两者的区别.他们区别在于 Any 类型会避开类型的检查,所以尽量少用最好不用.泛型一方面很灵活一方面也很安全,下面举个例子感受下两者的区别: ...

  6. 优酷视频上传api及demo代码

    1,优酷正常上传流程: 1). create:连接开放平台上传接口服务器,服务器端会返回upload_token以及upload_server_uri.2). create_file:连接上传服务器( ...

  7. 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】

    Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...

  8. 2241. [SDOI2011]打地鼠【暴力+剪枝】

    Description 打地鼠是这样的一个游戏:地面上有一些地鼠洞,地鼠们会不时从洞里探出头来很短时间后又缩回洞中.玩家的目标是在地鼠伸出头时,用锤子砸其头部,砸到的地鼠越多分数也就越高. 游戏中的锤 ...

  9. POJ3041 Asteroids(匈牙利算法)

    嘟嘟嘟 虽然我已经会网络流了,但是还是学了一个匈牙利算法. --就跟我会线段树,但还是学了树状数组一样. 其实匈牙利算法挺暴力的.简单来说就是先贪心匹配,然后如果左部点\(i\)匹配不上了,就尝试更改 ...

  10. Mysql 用户权限管理--从 xxx command denied to user xxx

    今天遇到一个mysql 权限的问题,即标题所述  xxx command denied to user xxx,一般mysql 这种报错,基本都属于当前用户没有进行该操作的权限,需要 root 用户授 ...