hnu Dirichlet's Theorem

/*
求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的更多相关文章
- 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 ...
- Dirichlet's Theorem on Arithmetic Progression
poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- 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 ...
- 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 ...
- 【POJ3006】Dirichlet's Theorem on Arithmetic Progressions(素数筛法)
简单的暴力筛法就可. #include <iostream> #include <cstring> #include <cmath> #include <cc ...
- Dirichlet's Theorem on Arithmetic Progressions POJ - 3006 线性欧拉筛
题意 给出a d n 给出数列 a,a+d,a+2d,a+3d......a+kd 问第n个数是几 保证答案不溢出 直接线性筛模拟即可 #include<cstdio> #inclu ...
- Dirichlet's Theorem on Arithmetic Progressions
http://poj.org/problem?id=3006 #include<stdio.h> #include<math.h> int is_prime(int n) { ...
随机推荐
- malloc心得
使用malloc时,要有一种在内存中随机分配一块内存的思想,然后再把分配好的内存的首地址返回来.
- Spring 中的 Bean 配置
内容提要 •IOC & DI 概述 •配置 bean –配置形式:基于 XML 文件的方式:基于注解的方式 –Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & ...
- mongo自带测试
benchrun > res = benchRun({ ops:[{ ns:"test.foo", op:"insert", doc:{y:{,]}} } ...
- Java生成excel导出文件(使用poi+JXL)
1.使用poi生成文件 package com.mi.entity; import java.util.Date; public class Student { private int id; pri ...
- 关于teleport_pro使用过程中的一点疑惑
在我新建工程的时候,有两个选项,一个是"new project wizard"另一个是"new project",然后就纠结了,我应该使用那个呢? 使用第一个的 ...
- some software that is used to speed up your system
1.RAMDISK take some space in ram and use them as the disk. Primo Ramdisk Server Edition 5.6.0 regist ...
- android 学习随笔二十三(动画:Fragment )
Fragment * 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容 * 在一个Activity中切换多个界面,每个界面就是一个Fragment* Fragmnen ...
- 160905、c3p0详细配置
官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-config> &l ...
- JSONArray.fromObject()注入处理日期Date格式
package jsonDateProcess; import java.sql.Date; import java.text.SimpleDateFormat; import java.util.L ...
- Java中类方法与实例方法的区别
实例方法可以对当前对象的实例变量进行操作,也可以对类变量进行操作,但类方法不能访问实例变量.实例方法必须由实例对象来调用,而类方法除了可由实例对象调用外,还可以由类名直接调用. 另外,在类方法中不能使 ...