/*
求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. 工龄居然这么有用![Reprint]

    工龄有多重要?你恐怕未必知道,别以为没啥大不了,事实上,工龄会从各方面影响你的生活 .为了自己的权益,关于工龄的这些事儿,你必须要了解! 1.影响带薪年休假 (1)职工本单位累计工作满1年不满10年的 ...

  2. [转]Eclipse Java注释模板设置详解

    原文链接:http://blog.csdn.net/ahhsxy/archive/2009/09/11/4542682.aspx 设置注释模板的入口: Window->Preference-&g ...

  3. Eclipse下Ruby的配置]

      简述: 在Eclipse中开发Ruby开发环境   步骤: 第一步, 1. 在Eclipse的Help ->  Install New Software输入 http://download. ...

  4. 自定义view(使用EditTetx实现记事本特效)

    先看一下效果图: 思路: 创建一个类,继承自EditText,在onDraw方法中绘制我们的下划线,通过屏幕的高度和每行控件的高度的比值得出屏幕中应该绘制多少行下划线,再来实现一些自定义的属性,设置控 ...

  5. 杭电 1595 find the safest road

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. 安装交叉编译器arm-linux-gcc

    需要交叉编译环境故安装交叉编译环境    1.在宿主机的/usr/local/arm目录存放交叉编译器        mkdir /usr/local/arm    2.解压交叉编译器包至/usr/l ...

  7. syscolumns表中所有字段的意思

    --syscolumns表中所有字段的意思 name sysname --列名或过程参数的名称. id int --该列所属的表对象 ID,或与该参数关联的存储过程 ID. xtype tinyint ...

  8. SSAS中角色(Role)定义需要注意的两个地方

    开发过SSAS Cube的朋友应该都知道,我们可以在SSAS中设置若干个角色,把windows账号放入这些角色中来限制不同的windows账号可以看到的数据有哪些,这里有两点需要注意一下. 首先在Cu ...

  9. NOIP200504循环

    NOIP200504循环 乐乐是一个聪明而又勤奋好学的孩子.他总喜欢探求事物的规律.一天,他突然对数的正整数次幂产生了兴趣.众所周知,2的正整数次幂最后一位数总是不断的在重复2,4,8,6,2,4,8 ...

  10. T-sql语句中GO的作用及语法【转】

    1. 作用: 向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号.2. 语法:一批 Transact-SQL 语句GO如Select 1Select 2Select ...