hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥
Senior PanⅡ
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)

Senior Pan had just failed in his math exam, and he can only prepare to make up for it. So he began a daily task with Master Dong, Dong will give a simple math problem to poor Pan everyday.
But it is still sometimes too hard for Senior Pan, so he has to ask you for help.
Dong will give Pan three integers L,R,K every time, consider all the positive integers in the interval [L,R], you’re required to calculate the sum of such integers in the interval that their smallest divisor (other than 1) is K.
Then T lines, each contains three integers L,R,K(1≤L≤R≤10^11,2≤K≤10^11)
1 20 5
2 6 3
Case #2: 3
占坑;。。。其实是不想写,容斥我用莫比乌斯函数推出来的
突然发现我sb,为什么写大素数测试啊。。。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<bitset>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-8
#define bug(x) cout<<"bug"<<x<<endl;
const int N=3e3+,M=1e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; LL gcd(LL a, LL b)
{
return b? gcd(b, a % b) : a;
} LL multi(LL a, LL b, LL m)
{
LL ans = ;
a %= m;
while(b)
{
if(b & )
{
ans = (ans + a) % m;
b--;
}
b >>= ;
a = (a + a) % m;
}
return ans;
} LL quick_mod(LL a, LL b, LL m)
{
LL ans = ;
a %= m;
while(b)
{
if(b & )
{
ans = multi(ans, a, m);
b--;
}
b >>= ;
a = multi(a, a, m);
}
return ans;
}
const int Times = ;
bool Miller_Rabin(LL n)
{
if(n == ) return true;
if(n < || !(n & )) return false;
LL m = n - ;
int k = ;
while((m & ) == )
{
k++;
m >>= ;
}
for(int i=; i<Times; i++)
{
LL a = rand() % (n - ) + ;
LL x = quick_mod(a, m, n);
LL y = ;
for(int j=; j<k; j++)
{
y = multi(x, x, n);
if(y == && x != && x != n - ) return false;
x = y;
}
if(y != ) return false;
}
return true;
} int vis[M];
vector<int>pri;
void init()
{
for(int i=;i<=;i++)
{
if(!vis[i])
pri.push_back(i);
for(int j=i+i;j<=;j+=i)
vis[j]=;
}
}
LL out;
void dfs(LL p,int pos,int step,LL L,LL R,LL K)
{
if(p*K>R)return;
LL d=p*K;
LL x1=(R/d),x2=(L-)/d;
//cout<<p<<" "<<d<<" "<<step<<endl;
//cout<<p<<" "<<pos<<" "<<step<<endl;
if(step%==)
{
out+=((multi(x1,x1+,mod)*1LL*)%mod)*d;
out%=mod;
out-=((multi(x2,x2+,mod)*1LL*)%mod)*d;
out=(out%mod+mod)%mod;
}
else
{
out-=((multi(x1,x1+,mod)*1LL*)%mod)*d;
out=(out%mod+mod)%mod;
out+=((multi(x2,x2+,mod)*1LL*)%mod)*d;
out%=mod;
}
for(int i=pos;i<pri.size();i++)
{
if(pri[i]>=K)return;
if(p*pri[i]*K>R)return;
dfs(p*pri[i],i+,step+,L,R,K);
}
}
int main()
{
init();
int T,cas=;
scanf("%d",&T);
while(T--)
{
out=;
LL l,r,k;
scanf("%lld%lld%lld",&l,&r,&k);
bool isp=Miller_Rabin(k);
printf("Case #%d: ",cas++);
if(!isp){
printf("0\n");
continue;
}
if(k>=)
{
if(l<=k&&r>=k)printf("%lld\n",k%mod);
else printf("%lld\n",);
}
else
{
dfs(,,,l,r,k);
printf("%lld\n",out);
}
}
return ;
}
hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥的更多相关文章
- 2017多校第9场 HDU 6169 Senior PanⅡ 数论,DP,爆搜
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6169 题意:给了区间L,R,求[L,R]区间所有满足其最小质数因子为k的数的和. 解法: 我看了这篇b ...
- 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29046 Accepted: 7342 Case ...
- Miller_Rabin 素数测试
费马定理的逆定理几乎可以用来判断一个数是否为素数,但是有一些数是判断不出来的,因此,Miller_Rabin测试方法对费马的测试过程做了改进,克服其存在的问题. 推理过程如下(摘自维基百科): 摘自另 ...
- Miller_Rabin素数测试【学习笔记】
引语:在数论中,对于素数的研究一直就很多,素数测试的方法也是非常多,如埃式筛法,6N±1法,或者直接暴力判(试除法).但是如果要判断比较大的数是否为素数,那么传统的试除法和筛法都不再适用.所以我们需要 ...
- HDU 6166 Senior Pan (最短路变形)
题目链接 Problem Description Senior Pan fails in his discrete math exam again. So he asks Master ZKC to ...
- HDU 6166.Senior Pan()-最短路(Dijkstra添加超源点、超汇点)+二进制划分集合 (2017 Multi-University Training Contest - Team 9 1006)
学长好久之前讲的,本来好久好久之前就要写题解的,一直都没写,懒死_(:з」∠)_ Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memor ...
- HDU 6166 Senior Pan 二进制分组 + 迪杰斯特拉算法
Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Probl ...
- HDU How many integers can you find 容斥
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU - 4336:Card Collector(min-max容斥求期望)
In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...
随机推荐
- @RefreshScope 的作用
让在application.properties里自定义的变量也能通过@Value 注解正常注入
- Spring AOP(基于代理类的AOP实现)
#基于代理类的AOP实现:step1: 1 package com.sjl.factorybean; /**切面类*/ import org.aopalliance.intercept.MethodI ...
- nginx+php+memcache实现hash一致性memcache 集群
我们工作中可能会遇到key-value数据库,如果我们面对的不止一台memcache服务器,而是很多台.那么现在就回出现一个问题: 当我们访问nginx服务器的时候,我们会判断memcache中是否有 ...
- JDK源码之Lock接口
public interface Lock { //阻塞的获取锁,如果获取到锁,从该方法返回 void lock(); //可中断的获取锁,该方法会响应中断,在锁的获取中可以中断当前线程 void l ...
- PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转
ChineseUtil 下载地址:https://github.com/Yurunsoft/ChineseUtil 另外一个中文转拼音工具:https://github.com/overtrue/pi ...
- 获取ip,获取客户端浏览器,获取客户端访问操作系统,获取客户端访问设备
/** * 获取ip */ public static function getIp() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CL ...
- PyCharm 安装及破解方法
PyCharm 安装及破解方法 一.安装 首先要下载Pycharm这个软件,可以在官网下载进行下载. 下载完软件之后,双击安装文件即可进行安装,双击后出现下图所示的提示,点击Next即可. 然后会出现 ...
- runtime.Gosched()的作用分析
untime.Gosched()用于让出CPU时间片.这就像跑接力赛,A跑了一会碰到代码runtime.Gosched()就把接力棒交给B了,A歇着了,B继续跑 测试代码: package main ...
- mybatis generator自动生成sqlmap代码的不完善之处以及解决方法
a) 建表时,字段名称建议用"_"分隔多个单词,比如:AWB_NO.REC_ID...,这样生成的entity,属性名称就会变成漂亮的驼峰命名,即:awbNo.recId b)or ...
- android之发送Get或Post请求至服务器接口
import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; imp ...