/*
求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. SQL between查询 范围查询

    --sal为员工工资 select * from emp;

  2. [转]SecureCRT使用配置详细图文教程

        Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,SecureCRT ...

  3. 关于内存 GetMemory( ) 笔试分析

    1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...

  4. fackbook的Fresco (FaceBook推出的Android图片加载库-Fresco)

    [Android开发经验]FaceBook推出的Android图片加载库-Fresco   欢迎关注ndroid-tech-frontier开源项目,定期翻译国外Android优质的技术.开源库.软件 ...

  5. AP模块的发票过账后关联对应的凭证编号。

    --AP发票 SELECT GJH.NAME,GJH.LEDGER_ID,GJH.JE_CATEGORY, GJH.JE_SOURCE,XDL.SOURCE_DISTRIBUTION_TYPE, XT ...

  6. Deep Learning 深度学习 学习教程网站集锦

    http://blog.sciencenet.cn/blog-517721-852551.html 学习笔记:深度学习是机器学习的突破 2006-2007年,加拿大多伦多大学教授.机器学习领域的泰斗G ...

  7. 【fedora】强制解除yum锁定

    运行yum makecache时出现yum update时候出现Another app is currently holding the yum lock解决方法yum被锁定了. 可以通过执行 rm ...

  8. sql语句中where和having的区别

    WHERE语句在GROUPBY语句之前:SQL会在分组之前计算WHERE语句. HAVING语句在GROUPBY语句之后:SQL会在分组之后计算HAVING语句.

  9. JVM 指令集

    指令码 助记符 说明 0x00 nop 什么都不做 0x01 aconst_null 将null推送至栈顶 0x02 iconst_m1 将int型-1推送至栈顶 0x03 iconst_0 将int ...

  10. 关于JDK,tomcat,MyEclipse的配置

    1.下载安装JDK 在自定义安装路径时,jdk和之后的jre文件夹是属于平行结构,我的安装路径为:D:\jdk\jdk1.6.0_43和D:\jdk\jre6 然后是对环境变量的配置, 计算机→属性→ ...