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 ...
随机推荐
- nmap扫描验证多种漏洞
nmap在Kali Linux中是默认安装的.它不仅可以用来确定目标网络上计算机的存活状态,而且可以扫描各个计算机的操作系统.开放端口.服务,还有可能获得用户的证书. 命令结构: nmap -sS - ...
- 在Eclipse中创建Dynamic Web Project具有和MyEclipse中Web Project一样的目录结构
1.在Eclipse中新建Dynamic Web Project 1.1.修改default output folder build\classes修改为:WebRoot\WEB-INF\classe ...
- JavaWeb中的资源映射
一./与/* <url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url- ...
- linux下mysql 8.0安装
安装本身同mysql 5.7,仍然建议使用tar.gz解压版,而非rpm安装包版. mysql已经将之前的mysql_native_password认证,修改成了caching_sha2_passwo ...
- Excel vba map/dictionary
今天在调整一生成java代码的工具时,要用到在不同语言间互转数据类型的逻辑,原来的代码中根据excel记录的行号来计算,再到另外一个collection中获取,个人想着这也太土鳖了,于是搜了下,在vb ...
- 【题解】Luogu P2783 有机化学之神偶尔会做作弊
原题链接:P2783 有机化学之神偶尔会做作弊 一看,是黑题,太毒瘤了,不写 什么单链??! 只会画有机化学中正六边形的我觉得这样不行QAQ(我才初二) 当然,题目也给你了详细的解释 实际呢,这道题先 ...
- 05: greenlet:轻量级的并发编程
网络编程其他篇 1.1 greenlet简介 1.greenlet原理 & 使用 1. greenle间切换 1)一个 “greenlet” 是一个很小的独立微线程,可以把它想像成一个堆栈帧, ...
- 08: MySQL慢查询
1.1 寻找慢查询 定义:我们将超过指定时间的SQL语句查询称为“慢查询”. 1.在mysql日志中开启慢查询日志 1. 修改配置文件 在 my.ini 增加几行: 主要是慢查询的定义时间(超 ...
- AppStore 添加回复
itunes connect 评论位置 1, 2, 添加用户权限:除了管理和客户支持可以回复.开发人员等只有只读权限
- Installing Jenkins as a Windows service
Install Jenkins as a Windows service NOTE: if you installed Jenkins using the windows installer, you ...