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)。把 d 质因数分解,设 t 为 Q 的指数, w 为 P 的指数,ans变成每个质数的 Σ(i=0~t) p^i * phi( p^(w-i) ) 连乘。
分解质因数用 Pollar Rho 。
注意 Q=0 就是 Q=P,要特判!而且不要以为答案变成 (!x || !y) 了!
d从0到P-1 就是 d从1到P!不要特判 P==Q时给答案减P !因为算的时候就没算d=0的!
每个质数的那个式子可以化简,把 phi 写开,和 p^i 合并,就不用枚举 i 。但要注意 w-i ==0 时 phi 的式子有些不同了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#define ll long long
using namespace std;
const int N=,mod=1e9+;
int n,ans=,c[N],c2[N],tot;
ll p[N],P=;
bool vis[N],flag;
ll rdl()
{
ll ret=;bool fx=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')fx=;ch=getchar();}
while(ch>=''&&ch<='') ret=(ret<<3ll)+(ret<<1ll)+ch-'',ch=getchar();
return fx?ret:-ret;
}
void upd(ll &x,ll md){x-=(x>=md?md:);}
ll mul(ll a,ll b,ll md)
{
ll ret=;//0!
while(b)
{
if(b&1ll)ret=ret+a,upd(ret,md);
a=a+a,upd(a,md);b>>=1ll;
}
return ret;
}
ll pw(ll x,ll k,ll md)
{
ll ret=;
while(k)
{
if(k&1ll)ret=mul(ret,x,md);
x=mul(x,x,md);k>>=1ll;
}
return ret;
}
int phi(ll p,int k)
{
if(!k) return ;
return mul(p-,pw(p,k-,mod),mod);
}
void add(ll a,bool flag)
{
if(flag)
{
for(int i=;i<=tot;i++)
if(p[i]==a)
{ c[i]++;return;}
p[++tot]=a; c[tot]=;
}
else
{
for(int i=;i<=tot;i++)
if(p[i]==a&&c2[i]<c[i])
c2[i]++;//Q=gcd()
}
}
bool ml_rb(ll n)
{
if(n<)return false;if(n==)return true;if((n&)==)return false;
ll u=n-,t=;
while((u&)==){u>>=,t++;}
int s=;
while(s--)
{
ll a=rand()%(n-)+;//2~n-1
a=pw(a,u,n); ll pre=a;
for(int i=;i<=t;i++)
{
a=mul(a,a,n);
if(a==&&pre!=&&pre!=n-)return false;
pre=a;
}
if(a!=) return false;
}
return true;
}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll pl_rho(ll x,ll c)
{
ll x0=rand()%x,y0=x, k=,i=;
while()
{
x0=(mul(x0,x0,x)+c)%x;
ll g=gcd(abs(x0-y0),x);
if(g!=&&g!=x) return g;
if(x0==y0)return x;
if((++i)==k){k<<=;y0=x0;}
}
}
void fd_fac(ll n,bool flag)
{
if(n<)return;
if(ml_rb(n))
{
add(n,flag);return;
}
ll p=n;
while(p==n)p=pl_rho(p,rand()%(n-)+);
fd_fac(p,flag); fd_fac(n/p,flag);
}
int main()
{
srand(time()); n=rdl();
for(int i=;i<=n;i++)
{
ll a=rdl(); P=mul(P,a,mod);
fd_fac(a,);
}
for(int i=;i<=n;i++)
{
ll a=rdl(); if(!a){flag=;continue;}
if(flag)continue;
fd_fac(a,);
}
if(flag)for(int i=;i<=tot;i++)c2[i]=c[i];
for(int i=,d,tp;i<=tot;i++)
{
tp=pw(p[i],c[i]-,mod);
d=mul(tp,mul(p[i]-,c2[i]+,mod)+(c[i]==c2[i]),mod);
ans=mul(ans,d,mod);
}
printf("%d\n",ans);
return ;
}
bzoj 3481 DZY Loves Math III——反演+rho分解质因数的更多相关文章
- bzoj 3309 DZY Loves Math 莫比乌斯反演
DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1303 Solved: 819[Submit][Status][Dis ...
- bzoj 3481 DZY loves math —— 反演+Pollard_rho分解质因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3481 推式子:xy % P = Q 的个数 由于 0 <= x,y < P,所以 ...
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
- BZOJ 3309: DZY Loves Math [莫比乌斯反演 线性筛]
题意:\(f(n)\)为n的质因子分解中的最大幂指数,求\(\sum_{i=1}^n \sum_{j=1}^m f(gcd(i,j))\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d| ...
- bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...
- BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表
有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...
- ●BZOJ 3309 DZY Loves Math
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...
- BZOJ 3561 DZY Loves Math VI
BZOJ 3561 DZY Loves Math VI 求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\text{lcm}(i,j)^{\gcd(i,j)}\),钦定\(n\leq m ...
- 【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) ...
随机推荐
- AngularJS的ng-class示例
程序下载:https://files.cnblogs.com/files/xiandedanteng/angularJSRender.rar 代码: <!DOCTYPE HTML PUBLIC ...
- Git以及github的使用方法(二)创建仓库,git add添加到“暂储区”,git commit添加到“本地仓库”
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- spring secrity 一些常用小知识
1.在JSP页面获取当前登录的用户名的方法 首先引入taglib:<%@ taglib prefix="sec" uri="http://www.springfra ...
- 有关C/C++指针的经典面试题(转)
参考一: 有关C/C++指针的经典面试题 0.预备知识,最基础的指针 其实最基础的指针也就应该如下面代码: int a; int* p=&a; 也就是说,声明了一个int变量a,然后声明一个i ...
- Solaris主机间的信任关系机制
解决问题: 管理员经常在其他服务器之间登录,是否需要密码切换. 知识点:主机间信任关系.R 命令集 /etc/hosts/equiv 文件 R服务是不加密的,别人可以破解. 主机名 + 用户名. + ...
- CSS3中的动画效果-------Day72
还记得么,在前面也曾实现过"仅仅用css让div动起来",还记得当时是怎么实现的么,是的,transition,针对的也比較局限,仅仅有旋转角度啊,长宽啊之类的,所以说,与其说是动 ...
- 使用Python处理Excel文件的一些代码示例
笔记:使用Python处理Excel文件的一些代码示例,以下代码来自于<Python数据分析基础>一书,有删改 #!/usr/bin/env python3 # 导入读取Excel文件的库 ...
- openshifit 安装 redis
http://blog.csdn.net/lsx991947534/article/details/48860537 http://blog.csdn.net/aguangg_6655_la/arti ...
- BZOJ 2818 Gcd 线性欧拉
题意:链接 方法:线性欧拉 解析: 首先列一下表达式 gcd(x,y)=z(z是素数而且x,y<=n). 然后我们能够得到什么呢? gcd(x/z,y/z)=1; 最好还是令y>=x 则能 ...
- MVC入门——列表页
创建控制器UserInfoController using System; using System.Collections.Generic; using System.Linq; using Sys ...