题目: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分解质因数的更多相关文章

  1. 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).把 ...

  2. bzoj 3309 DZY Loves Math——反演+线性筛

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 像这种数据范围,一般是线性预处理,每个询问 sqrt (数论分块)做. 先反演一番.然 ...

  3. ●BZOJ 3309 DZY Loves Math

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3309 题解: 莫比乌斯反演,线筛 化一化式子: f(x)表示x的质因子分解中的最大幂指数 $ ...

  4. 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 ...

  5. bzoj 3309 DZY Loves Math 莫比乌斯反演

    DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1303  Solved: 819[Submit][Status][Dis ...

  6. BZOJ 3309: DZY Loves Math

    3309: DZY Loves Math Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 761  Solved: 401[Submit][Status ...

  7. 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较小 ...

  8. bzoj 3462: DZY Loves Math II

    3462: DZY Loves Math II Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 211  Solved: 103[Submit][Sta ...

  9. BZOJ 3309 DZY Loves Math ——莫比乌斯反演

    枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...

随机推荐

  1. poj 3417 Network 题解

    题意: 先给出一棵树,然后再给出m条边,把这m条边连上,然后剪掉两条边,一条是原边,一条是新边,问有多少种方案能使图不连通. 思路: 从原边的角度看 1.树加边,一定成环,加一条(u,v)边就有u-& ...

  2. [BZOJ4207]Can

    [BZOJ4207]Can 试题描述 这个问题是源于一个在棋盘上玩的,由Sid Sackson设计的名叫Can't stop的游戏的.这个问题与Can't stop有一定的相似之处,但是不需要玩过Ca ...

  3. SpringBoot 配置 @PropertySource、@ImportResource、@Bean

    一.@PropertySource @PropertySource:加载指定的配置文件 @PropertySource(value = {"classpath:person.properti ...

  4. [Usaco2006 Nov] Fence Repair 切割木板

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1356  Solved: 714[Submit][Status][Discuss] Description ...

  5. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  6. 洛谷—— P1714 切蛋糕

    https://www.luogu.org/problem/show?pid=1714 题目描述 今天是小Z的生日,同学们为他带来了一块蛋糕.这块蛋糕是一个长方体,被用不同色彩分成了N个相同的小块,每 ...

  7. 学习日常笔记<day11>cookie及session

    1.会话管理 1.1会话管理定义 会话管理:管理浏览器客户端和服务端之间的会话过程中产生的会话数据 域对象:实现资源之间的数据共享 request 域对象 context 域对象 1.2.会话技术 C ...

  8. c++ static const

    static 是c++中很常用的修饰符,它被用来控制变量的存储方式和可见性,下面我将从 static 修饰符的产生原因.作用谈起,全面分析static 修饰符的实质. static 的两大作用: 一. ...

  9. Sublime3 Preference, Settings-User

    {"font_face": "Consolas","font_size": 15,"ignored_packages": ...

  10. zmq.error.ZMQError: Address already in use

    1.如下代码,启动的时候python app.py会报如题的错误 app.py #!/user/bin python # -*- coding:utf-8 -*- import os from dat ...