bzoj 3481 DZY loves math —— 反演+Pollard_rho分解质因数
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3481
推式子:xy % P = Q 的个数
由于 0 <= x,y < P,所以对于一个固定的 x,如果 (x,P) | Q,则有 (x,P) 个解;
所以个数为 ∑(0 <= x < P ) (x,P) * [ (x,P) | Q ] ( [...] 表示 ... 为真则为1,否则为0)
= ∑( d|P, d|Q ) d * φ( P/d )
令 Q = (P,Q),则
= ∑( d|Q ) d * φ( P/d )
单独考虑每个质因子的贡献,若P = p1 ^ t1 * p2 ^ t2 * ... pm ^ tm,Q = p1 ^ k1 * p2 ^ k2 * ... * pm ^ km,则原式
= ∏( 1 <= i <= m ) ∑( 0 <= x <= k[i] ) pi * φ(pi^ti-x)
而 φ(pi^ti) = pi^ti * ( pi - 1 ) / pi
所以原式最终变成 ∏( 1 <= i <= m ) pi^(ti-1) * ( pi - 1 ) * ( ki + 1 )
注意 t[i] = k[i] 时略有不同,求和的那一项 = pi , 所以如果有 t[i] = k[i],则再加一个 pi^(ti-1);
然后...质因数分解要用 Pollar_rho,Pollard_rho 内又用到 Miller-Rabin,参考了一下博客:
https://blog.csdn.net/sunshine_cfbsl/article/details/52512706
https://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html
https://blog.csdn.net/forever_shi/article/details/80547041
套上板子就可以了;
但要注意,如果把 P 和 Q 的所有质因子都提取出来会 TLE,因为令 Q = (P,Q),所以只有 Q 有的质因子就不用专门记录下来了;
直接 rand() 似乎只能得到 int,所以可以专门写一下 ll 的,虽然只有 int 好像也可以;
注意各处都是 ll 。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
using namespace std;
typedef long long ll;
int const xn=;
int m,S=,t[xn],k[xn];
ll pri[xn],ans;
ll rd()
{
ll ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=(ret<<3ll)+(ret<<1ll)+ch-'',ch=getchar();
return f?ret:-ret;
}
ll Rand(ll n)
{
return (((ll)rand()<<)|rand())%(n-)+;
// return rand()%(n-1)+1;//也可,但无ll
}
ll upt(ll x,ll mod){while(x>=mod)x-=mod; while(x<)x+=mod; return x;}
ll mul(ll a,ll b,ll mod)
{
ll ret=; a=a%mod; b=b%mod;
for(;b;b>>=1ll,a=upt(a+a,mod))
if(b&)ret=upt(ret+a,mod);
return ret;
}
ll pw(ll a,ll b,ll mod)
{
ll ret=; a=a%mod;
for(;b;b>>=1ll,a=mul(a,a,mod))
if(b&)ret=mul(ret,a,mod);
return ret;
}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
bool ck(ll a,ll u,ll t,ll p)
{
ll x=pw(a,u,p),lst=x;
for(int i=;i<=t;i++)
{
x=mul(x,x,p);
if(x==&&lst!=&&lst!=p-)return ; // a^2 % p = 1 -> a=1,a=p-1
lst=x;
}
if(x!=)return ; // a^p-1 % p = 1
return ;
}
bool MR(ll x)
{
if(x<=||x&==)return ;
if(x==)return ;
ll t=,u=x-;
while(u&==)u>>=1ll,t++;
for(int i=;i<=S;i++)
{
ll a=Rand(x)%(x-)+;
if(!ck(a,u,t,x))return ;
}
return ;
}
ll rho(ll n,ll c)
{
ll x=Rand(n)%n,y=x;
ll i=,k=;
while()
{
i++;
x=(mul(x,x,n)+c)%n;
ll d=gcd((x-y+n)%n,n);
if(d!=&&d!=n)return d;
if(x==y)return n;
if(i==k)y=x,k+=k;
}
}
void add(ll x,bool tp)//1:P-t[]
{
for(int i=;i<=m;i++)
if(pri[i]==x)
{
if(tp)t[i]++; else k[i]++;
return;
}
if(!tp)return;//否则TLE!!!
pri[++m]=x;
if(tp)t[m]++; else k[m]++;
}
void find(ll x,bool tp)
{
if(x==)return;//!
if(MR(x)){add(x,tp); return;}
ll p=x;
while(p>=x)p=rho(p,Rand(x)%(x-)+);
find(p,tp); find(x/p,tp);
}
int main()
{
srand();//或 time(0);
int mod=1e9+;
int n=rd(); ll x; bool fl=;
for(int i=;i<=n;i++)x=rd(),find(x,);//1:P-t[]
for(int i=;i<=n;i++)
{
x=rd();
if(!x){fl=; continue;}
else find(x,);
}
if(fl)for(int i=;i<=m;i++)k[i]=t[i];
else for(int i=;i<=m;i++)k[i]=min(k[i],t[i]);//(P,Q)
ans=;
for(int i=;i<=m;i++)
{
ll pp=pw(pri[i],t[i]-,mod);
ll mm=mul(pri[i]-,k[i]+,mod);
if(k[i]==t[i])mm++;
ans=mul(ans,mul(pp,mm,mod),mod);
}
printf("%lld\n",ans);
return ;
}
bzoj 3481 DZY loves math —— 反演+Pollard_rho分解质因数的更多相关文章
- 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).把 ...
- bzoj 3309 DZY Loves Math——反演+线性筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 像这种数据范围,一般是线性预处理,每个询问 sqrt (数论分块)做. 先反演一番.然 ...
- ●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 莫比乌斯反演
DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1303 Solved: 819[Submit][Status][Dis ...
- BZOJ 3309: DZY Loves Math
3309: DZY Loves Math Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 761 Solved: 401[Submit][Status ...
- 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 3462: DZY Loves Math II
3462: DZY Loves Math II Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 211 Solved: 103[Submit][Sta ...
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
随机推荐
- MVC4 上传图片并生成缩略图
Views @using (Html.BeginForm("Create","img",FormMethod.Post, new { enctype = &qu ...
- POJ 1273 Drainage Ditches【图论,网络流】
就是普通的网络流问题,想试试新学的dinic算法,这个算法暑假就开始看国家集训队论文了,之前一直都只用没效率的EK算法,真正学会这个算法还是开学后白书上的描述:dinic算法就是不断用BFS构建层次图 ...
- hdu 1879
#include<stdio.h> #include<stdlib.h> #define N 100 struct node { int x,y,dis; }road[N*N] ...
- mybatis查询返回null解决方案
mybatis查询返回null解决方案: 问题:查询出的列与javabean中的字段名不一致. 解决方案: 1.将javabean中的字段改为和查询出的列名一致: 2.将sql加入as改变列名,和ja ...
- numpy模块
NumPy简介: NumPy 是高性能科学计算和数据分析的基础包:它是pandas等其他工具的基础. NumPy的主要功能: 1. ndarray,一个多维数组结构,高效且节省空间 (最主要的功能) ...
- CodeIgniter与Zend Acl结合实现轻量级权限控制
CodeIgniter与Zend Acl结合实现轻量级权限控制 Tag :CodeIgniter Zend Acl 权限控制 1. Zend_Acl简介 Zend_Acl 为权限管理提供轻量并灵活的访 ...
- Linux 常用但较容易忘记的命令
看死循环 strace -p pid 查看系统版本 cat /etc/issue 设置内核启动版本 /etc/lilo.conf , /boot/grub/grub.conf 设置启动模式 /etc ...
- Spring + RMI
服务端: RmiServer.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...
- 【转】从头说catalan数及笔试面试里那些相关的问题
http://blog.csdn.net/han_xiaoyang/article/details/11938973#t6
- Visual Studio VS2010 如何修改默认的编辑语言
1 比如我要把默认是C++的配置改成C#,在工具-导入和导出设置中,重置所有设置 2 这里改成新的语言 3 重置完成