/*
求ax+b x属于区间[L,R];范围内素数的个数。
a*R+b<=10^12 ; R-L+1<=10^6 枚举,超时。
1.如果GCD(a,b)>1 那么a+b 2*a+b ..都会是合数。此时只有判断b是否为素数。 2.如果GCD(a,b)=1 那么就可以列式子
ax+b %p = 0 其中p为素数。 如果满足,那么ax+b就是合数。
筛选整个区间即可。 由于最大的数字是10^12,只能被sqrt(10^12)的素数整除,所以筛选10^6内的素数。
由于区间L,R 10^6,开一个bool hash[ ]。 ax+b % py = 0 ===> ax+py = -b; 根据扩展欧几里得求出 最小的x,此时的x可以为0. while(x<0) x+p; 求出最小的x,关键还要求出第一个满足在区间[ L ,R ]里的数字。 temp = L%p;
x = x - temp; while(a*(L+x)+b<=p) { //关于此处等号,是一个问题 既然a*x+b 是合数,怎么会=p,加了也不会错。
x = x + p;
} 这样的L+x就是区间[L ,R]里的第一个满足的数字。
而且x可以为0,刚好用hash的时候,直接对x进行哈希。 while(x<(R-L+1)){//不能等于,从0 --R-L 有 R-L+1个了。
hash[x] = false;
x = x+p;
} 3.最后求出结果。扫一遍哈希。 需要注意的是,由于a*x+b <=2的情况,所以对x==0 || x<=1 进行特判。
*/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<math.h>
using namespace std;
typedef long long LL; const int maxn = 1e6+;
int prime[maxn],len = ;
bool s [maxn];
bool hash1[maxn];
void init()
{
int i,j;
memset(s,true,sizeof(s));
for(i=;i<maxn;i++)
{
if(s[i]==false) continue;
prime[++len] = i;
for(j=i*;j<maxn;j=j+i)
s[j]=false;
}
s[] = s[] = false;
}
bool isprime(LL n)
{
LL i,ans;
if(n<maxn) return s[n];
ans = (LL)sqrt(n*1.0); for(i=; i<=len && prime[i]<=ans; i++)
{
if(n%prime[i]==) return false;
}
return true;
}
LL Ex_GCD(LL a,LL b,LL &x,LL& y)
{
if(b==)
{
x=;
y=;
return a;
}
LL g=Ex_GCD(b,a%b,x,y);
LL hxl=x-(a/b)*y;
x=y;
y=hxl;
return g;
}
int main()
{
LL a,b,L,U,x,y;
LL i,p;
int t = ;
init();
while(scanf("%I64d",&a)>)
{
if(a==)break;
scanf("%I64d%I64d%I64d",&b,&L,&U);
LL g = Ex_GCD(a,b,x,y);
if(g>)
{
if(L== && isprime(b))
printf("Case %d: 1\n",++t);
else printf("Case %d: 0\n",++t);
}
else if(g==)/** gcd(a,b) == 1 **/
{
memset(hash1,true,sizeof(hash1));
if(L==)
hash1[] = isprime(b);
if(L<=)
hash1[-L] = isprime(a+b);
LL length = U-L+;
LL MAX = a*U+b;
for(i=; i<=len; i++)
{
p = prime[i];
if(a%p==)continue;
if(p*p>MAX)break;; g = Ex_GCD(a,p,x,y);// ax+py = -b;
x = (x*-b) % p;
while(x<) x=x+p; LL temp = L%p;
x = x - temp;
while(x<) x=x+p; while(a*(x+L)+b<=p)
{
x = x+p;
}
while(x<length)
{
hash1[x]=false;
x=x+p;
}
}
LL hxl = ;
for(i=; i<length; i++) if(hash1[i]==true) hxl++;
printf("Case %d: %I64d\n",++t,hxl);
}
}
return ;
}

hnu Dirichlet's Theorem的更多相关文章

  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. Dirichlet's Theorem on Arithmetic Progression

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

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

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

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

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

  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. 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)

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

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

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

  9. Dirichlet's Theorem on Arithmetic Progressions

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

随机推荐

  1. thinkphp介绍

    1.thinkphp是一个免费的开源的轻量级的高效的国产的php框架 2.现在主流的框架有:   zend framwork 框架,功能十分齐全,是php官网开发的一个框架   yii框架 十分轻巧的 ...

  2. have you declared this activity in your AndroidManifest.xml

    对于那些刚开始接触安卓的开发者来说,遇到这个问题再正常不过了,出现这种问题的原因大概可分为: 1.android的四大组件都必须在AndroidMainifest.xml里面声明,所以首先看看有没有在 ...

  3. ajax基本用法

    ajax能做到无刷新数据交互,给用户体验带来好处的同时也减小了服务器的压力,所以运用ajax能使网站性能更强劲.更吸引用户. 大型网站少不了注册页面,而大多数情况下我们不想让用户有相同的注册ID,所以 ...

  4. 02---Net基础加强

    将普通日期格式:“2014年7月8日”转换成汉字日期格式:“二零一四年七月八日”.(暂时不考虑10日,13日,23日) class Program { static void Main(string[ ...

  5. zw版【转发·台湾nvp系列Delphi例程】HALCON ObjToInteger1-4

    zw版[转发·台湾nvp系列Delphi例程]HALCON ObjToInteger1 procedure TForm1.Button1Click(Sender: TObject);var img, ...

  6. Mysql数据库读写分离配置

    环境模拟 实现读写分离 减轻数据库的负荷 主服务器  master   10.0.0.12 从服务器 slave    10.0.0.66 配置主服务器: 在10.0.0.12服务器操作   创建数据 ...

  7. Parse_ini_file

    parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置. 注释:本函数可以用来读取你自己的应用程序的配置文件.本函数与 php.ini 文件没有关系,该文件在运行脚本时就 ...

  8. DIV怎样能够垂直居中

    这里只说固定宽高的情况: 1.Top:50%; 2.margin-top:-(height/2); 就这样. 不过很好奇有没有v-align之类的属性可以直接实现.

  9. Openstack的计算节点的nova-network异常中止及实例无法删除排错过程

    在预生产环境(172.17.46.2)发现无法删除实例,可以对实例做暂停,恢复操作. 查询原因发现计算节点的nova-network异常 [root@node-12 ~]# /etc/init.d/o ...

  10. 视频处理控件TVideoGrabber如何重新编码视频

    TVideoGrabber中可以对音频.视频剪辑进行重新编码剪辑,多的朋友知道这个功能更点,但是具体操作上还是不是很熟悉,这里总结一下,主要步骤如下: 1.通过指定开始和停止的时间,可以简单的剪辑视频 ...