Bzoj3481 DZY Loves Math III
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 310 Solved: 65
Description
.jpg)
Input

Output

Sample Input
1
2
3
2
4
2
Sample Output
HINT

1<=N<=10,0<=Qi<=10^18,1<=Pi<=10^18,P>=2
本题仅四组数据。
Source
数学问题 欧拉函数 Miller-Rabin Pollard-rho
花了一整晚来怼这道题……在long long的领域遇到了许多问题。
假设我们有充足的时间枚举每一个x,那么在x确定的情况下,原式变成了一个模方程。
根据裴蜀定理,我们知道只有当$ gcd(x,P)|Q $ 的情况下方程有 $ gcd(x,P) $ 个解。
现在我们可以愉快地枚举每一个gcd,那么答案就是:
$$ans=\sum_{d|P,d|Q} d * \sum_{x}[gcd(x,\frac{P}{d})==1]$$
后面那个sum显然是欧拉函数
那么$$ ans=\sum_{d|P,d|Q} d · \varphi(\frac{P}{d}) $$
分(an)析(zhao)一(tao)波(lu),发现这是一个积性函数,所以我们可以分别考虑每个质因子的贡献,再把它们累乘起来。
我们现在考虑一个质因子p,它在P中有$q$个,在Q中有$q'$个:
它对答案的贡献是
$$ \sum_{i=0}^{q'} p^i * \varphi(p^{q-i})$$
我们知道
$$\varphi(p^{q})=p^{q}·\frac{p-1}{p}$$
所以上面的sum可以化成:
$$p^{q-1}·(p-1)·(q'+1)$$
但是有一个特殊情况,当i=q的时候,$\varphi(1)=1$,不能表示成$\frac{p-1}{p}$的形式,而我们却把它用这种形式算进去了。
也就是说我们把一个$p^q$ 的贡献算成了 $(p-1)p^{q-1}$,特判一下消除即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define LL long long
using namespace std;
const int mxn=;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
LL Rand(LL n){return (((LL)rand()<<)|rand())%(n-)+;}
//
map<LL,int>primap;
LL pri[mxn*];int cnt=;
bool vis[mxn];
void init(){
int mxn=;
for(int i=;i<mxn;i++){
if(!vis[i]){
pri[++cnt]=i;
primap[i]=cnt;
}
for(int j=;j<=cnt && pri[j]*i<mxn;j++){
vis[pri[j]*i]=;
if(i%pri[j]==)break;
}
}
return;
}
//
LL mul(LL x,LL k,LL P)
{
LL res=;
while(k){
if(k&)res=(res+x)%P;
x=(x+x)%P;
k>>=;
}
return res;
}
/*
LL mul(LL a,LL b,LL P){
LL d=(long double)a/P*b+1e-8; a=a*b-d*P;
a=(a<0)?a+P:a;
printf("%lld \n",a);
return a;
}*/
LL ksm(LL a,LL k,LL P){
LL res=;
while(k){
if(k&)res=mul(res,a,P);
a=mul(a,a,P);
k>>=;
}
return res;
}
///
bool check(LL a,LL n,LL r,LL s){
LL res=ksm(a,r,n);LL b=res;
for(int i=;i<=s;i++){
res=mul(res,res,n);
if(res== && b!= && b!=n-)return ;
b=res;
}
return (res!=);
}
bool MR(LL n){//素数测试
if(n<=)return ;
if(n==)return ;
if(~n&)return ;
LL r=n-,s=;
while(!(r&))r>>=,s++;
for(int i=;i<=;i++)
if(check(Rand(n),n,r,s))return ;
return ;
}
///
LL p[mxn],q[mxn];
void addpri_P(LL x){
if(primap.count(x)){
++p[primap[x]];return;
}
pri[++cnt]=x;
primap[x]=cnt;
p[cnt]=;
return;
}
void addpri_Q(LL x){
if(primap.count(x)){
int t=primap[x];
if(q[t]<p[t])++q[t];
}
return;
}
LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
LL Rho(LL n,LL c){
if(n<)while();
LL k=,x=Rand(n),y=x,p=;
for(LL i=;p==;i++){
x=(mul(x,x,n)+c)%n;
p=(y>x)?(y-x):(x-y);
p=gcd(n,p);
if(i==k)y=x,k<<=;
}
return p;
}
void solve(LL x,bool flag){//分解质因数
if(x==)return;
if(MR(x)){
if(!flag)addpri_P(x);//P
else addpri_Q(x);//Q
return;
}
LL t=x;
while(t==x)t=Rho(x,Rand(x));
solve(t,flag);
solve(x/t,flag);
return;
}
//
const int mod=1e9+;
int n;
LL P[],Q[];
void Calc(){
LL ans=;
for(int i=;i<=cnt;i++){
if(!p[i])continue;
LL R=mul((q[i]+),(pri[i]-),mod);
if(p[i]==q[i])R++;
R=mul(R,ksm(pri[i],p[i]-,mod),mod);
ans=mul(ans,R,mod);
}
printf("%lld\n",ans);
return;
}
int main(){
srand();
init();
int i,j;
n=read();
for(i=;i<=n;i++){
P[i]=read();
solve(P[i],);
}
for(i=;i<=n;i++){
Q[i]=read();
if(!Q[i]){//特判0
for(j=;j<=cnt;j++)q[j]=p[j];
break;
}
else solve(Q[i],);
}
Calc();
return ;
}
Bzoj3481 DZY Loves Math III的更多相关文章
- BZOJ3481 DZY Loves Math III(数论+Pollard_Rho)
考虑对于每一个x有多少个合法解.得到ax+by=c形式的方程.如果gcd(x,y)|c,则a在0~y-1范围内的解的个数为gcd(x,y).也就是说现在所要求的是Σ[gcd(x,P)|Q]*gcd(x ...
- bzoj 3481 DZY Loves Math III——反演+rho分解质因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3481 推推式子发现:令Q=gcd(P,Q),ans=Σ(d|Q) d*phi(P/d).把 ...
- DZY Loves Math系列
link 好久没写数学题了,再这样下去吃枣药丸啊. 找一套应该还比较有意思的数学题来做. [bzoj3309]DZY Loves Math 简单推一下. \[\sum_{i=1}^n\sum_{j=1 ...
- DZY Loves Math 系列详细题解
BZOJ 3309: DZY Loves Math I 题意 \(f(n)\) 为 \(n\) 幂指数的最大值. \[ \sum_{i = 1}^{a} \sum_{j = 1}^{b} f(\gcd ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
- 【BZOJ】3309: DZY Loves Math 莫比乌斯反演优化
3309: DZY Loves Math Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007) ...
- [BZOJ3561] DZY Loves Math VI
(14.10.28改) 本来只想写BZOJ3739:DZY Loves Math VIII的,不过因为和VI有关系,而且也没别人写过VI的题解,那么写下. 不过我还不会插公式…… http://www ...
- BZOJ 3512: DZY Loves Math IV [杜教筛]
3512: DZY Loves Math IV 题意:求\(\sum_{i=1}^n \sum_{j=1}^m \varphi(ij)\),\(n \le 10^5, m \le 10^9\) n较小 ...
- ●BZOJ 3309 DZY Loves Math
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...
随机推荐
- mysql 只返回一条数据
问题描述: 需要得到时间最近的一条记录,但是按照时间字段排完序之后,得到的是全部. 解决办法: order by createtime desc //降序:asc:升序 LIMIT 1
- cacti 添加mysql 监控 (远程服务器)
监控主机 192.168.24.69 ,以下用A表示 被监控主机 192.168.24.79,以下用B标识 记得在A服务器的cacti中导入监控mysql的templates文件 1.在B上安 ...
- [计算机网络] 互联网协议栈(TCP/IP参考模型)各层的主要功能及相应协议
应用层:提供用户与网络间的接口.----HTTP.FTP.SMTP 运输层:进程到进程间的数据传输.---TCP.UDP 网络层:主机到主机之间的数据传输.---IP.选路协议 数据链路层:相邻结点之 ...
- 求助 delphi ADO组件的 CursorLocation属性设置为 clUseServer 用法 [问题点数:20分]
我有个管理系统,所有ADOQUERY组件的 CursorLocation属性设置为 clUseClient,一直运行正常,我尝试全部设置为clUseServer, 系统不运行了,请大家帮忙. 我的做法 ...
- [洛谷P5057][CQOI2006]简单题
题目大意:有一个长度为$n$的$01$串,两个操作: $1\;l\;r:$把区间$[l,r]$翻转($0->1,1->0$) $2\;p:$求第$p$位是什么 题解:维护前缀异或和,树状数 ...
- BZOJ1012:[JSOI2008]最大数——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=1012 https://www.luogu.org/problemnew/show/P1198 现在 ...
- POJ.2142 The Balance (拓展欧几里得)
POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...
- Java Web用Freemarker生成带图片的Word文档
步骤一:模板制作 用world2003做一个导出模板,如果有图片则加入一张图片占位,将world另存为xml,将xml中需要导出的内容用Freemarker标签表示,最后另存为.ftl结尾的模板: 步 ...
- Widows与linux关于隐形文件和非隐形文件の对比
Widows与linux关于隐形文件和非隐形文件の对比 对于windows来说 ,它本身有一些隐藏文件,为了防止一些菜鸟不小心把电脑的主要文件删除,还有就是里面存放一些你不知道的后门. 对此我们一些同 ...
- iOS-防止向SQLite数据库中插入重复数据记录:
原则:先检测该数据库的指定表中,是否已经存在我们要插入的这条数据记录,若已经存在,则不插入这条数据记录(即忽略此次插入操作),若尚不存在,才插入这条数据记录(即才执行此次插入操作) 我们这里使用的是F ...