/*
求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 多态

    Sealed关键字:密封类 该类无法被继承 部分类: Namespace 命名空间 虚拟文件夹 Partial关键字 可以将一个类拆分成多个部分,分别放在多个文件里 多态: 1.编译多态 函数重载 2 ...

  2. Java基础(52):ClassCastException详解(转)

    ClassCastException,从字面上看,是类型转换错误,通常是进行强制类型转换时候出的错误.下面对产生ClassCastException异常的原因进行分析,然后给出这种异常的解决方法. 这 ...

  3. Apache与Nginx虚拟主机设置(多域名和多端口的区别)

    为了方便管理虚拟主机,应该尽量少修改主配置文件http.conf或者nginx.conf,大部分修改变更都在虚拟主机片配置文件httpd- vhost.conf或者vhost.conf中完成,这样有利 ...

  4. event对象的属性

    事件类型: bubbles:布尔值,表示事件是否通过DOM以冒泡形式触发. 事件发生时,反应当前环境信息的属性: button: 表示(如果有)鼠标所按下的按钮 ctrlKey: 布尔值,表示Ctrl ...

  5. 2016-9-6 批量给文件名的前面加上“igeek_高薪就业” 2、 利用FileInputStream和FileOutputStream复制文件

    在此只列出典型题目,有的题目扫一眼就有代码的不去浪费时间了,想要完整题目的评论留邮箱,看到就发.持续更新中... 1.批量给文件名的前面加上“igeek_高薪就业” package com.work; ...

  6. VMware搭建 sql server2012集群加节点 KB953748

    --node4加入节点前 将共享磁盘挂载到node4,仅测试验证磁盘挂载,否则会导致整个集群磁盘offline切换 --node4 加入集群报错,无法访问计算机“node4” 0.public网卡勾选 ...

  7. 创建本地yum软件源,为本地Package安装Cloudera Manager、Cloudera Hadoop及Impala做准备

    一.包管理工具及CentOS的yum 1.包管理工具如何发现可以用的包 包管理工具依赖一系列软件源,工具下载源的信息存储在配置文件中,其位置随某包管理工具不同而变化 使用yum的RedHat/Cent ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON DispCross

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

  9. 如何在图像处理工具包ImagXpress中对图像进行捕捉、复制和粘贴

    如何在在ImagXpress中进行图像的捕捉. 复制和粘贴呢?下面详细来看一下,在多种情况下,图和实现这些操作. 捕捉屏幕图像 捕捉通过ImageXView窗口绑定的屏幕范围,以及保存到一个Image ...

  10. simpleBLEPeripheral.c 文件分析

    这个配置或者说任务, 让这个蓝牙设备成为了一个简单的BLE外设. 这里定义了外设的广播数据, 以及最重要, char被改变之后的回调, 引出后来的coreHandler里面的, ack 以及写e2pr ...