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的质因子分解中的最大幂指数 $ ...
随机推荐
- ACM 第八天
数据结构和算法目录表 数据结构和算法目录表 C C++ Java 线性结构 1. 数组.单链表和双链表 2. Linux内核中双向链表的经典实现 数组.单链表和双链表 数组.单链表和双链表 ...
- PAT 1052 卖个萌
https://pintia.cn/problem-sets/994805260223102976/problems/994805273883951104 萌萌哒表情符号通常由“手”.“眼”.“口”三 ...
- AWVS使用基础教程
什么是AWVS Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测试你的网站安全,检测流行安全漏洞,现已更新到10.(下 ...
- Ubuntu编译内核树
什么是内核树?刚开始我也没弄明白,通过这几天的学习,有所感悟,就说说我的理解吧!从形式上看,内核树与内核源码的目录结构形式是相同的,都是由各个层次的文件目录结构组成,但是其中的具体内容肯定是不同的.从 ...
- ICommand接口
WPF 中的命令是通过实现 ICommand 接口创建的.ICommand 的 WPF 实现是 RoutedCommand 类. WPF 中的主要输入源是鼠标.键盘.墨迹和路由命令.更加面向设备的输入 ...
- matlab isfield
isfield 函数功能:判断输入是否是结构体数组的域(成员). 调用格式: tf=isfield(S,'fieldname') 检查结构体S是否包含由fieldname指定的域,如果包含,返回逻辑1 ...
- 【bzoj4300】绝世好题 dp
题目描述 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). 输入 输入文件共2行. 第一行包括一个整数n. 第二行包括n个 ...
- BZOJ4698 & 洛谷2463:[SDOI2008]Sandy的卡片——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4698 https://www.luogu.org/problemnew/show/P2463#sub ...
- BZOJ4873:[SHOI2017]寿司餐厅——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4873 https://www.luogu.org/problemnew/show/P3749 简要题 ...
- Hcharts和Echarts----制作报表的工具
Hcharts官网:https://www.hcharts.cn/Hcharts API文档:https://api.hcharts.cn/highcharts Echarts官网:http://ec ...