题目: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. PTA 02-线性结构4 Pop Sequence (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/665 5-3 Pop Sequence   (25分) Given a stack wh ...

  2. bzoj1059:[ZJOI2007]矩阵游戏【二分图匹配】

    Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白方阵进行(如同国际象棋一般,只是颜色是随意的).每次可以对该矩阵进行两 ...

  3. hdu4352 XHXJ's LIS(数位DP + LIS + 状态压缩)

    #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire ...

  4. 【设计模式】GOF设计模式趣解(23种设计模式)

    创建型模式                   1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯德基,只管向服务员说 ...

  5. TYVJ P 1214 硬币问题

    TYVJ  P 1214 硬币问题 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述   有n种硬币,面值为别为a[1],a[2],a[3]……a[n],每种都 ...

  6. hdu6110:路径交

    $n \leq 500000$的树给$m \leq 500000$个路径,$q \leq 500000$个询问每次问一个区间的路径交. 路径交口诀:(前方高能) 判有交,此链有彼祖: 取其交,最深两两 ...

  7. 深入理解计算机操作系统——第11章:CS模型,网络

    网络编程: 11.1 客户端-服务器编程模型 (1)一个应用是由一个服务器进程和一个或多个客户端进程组成. (2)服务器管理某种资源,并且操纵这种资源来为客户端服务. CS模型: CS的基本操作是事务 ...

  8. poj 1236+hdu2767 有向图 缩点+看度数(tarjan)

    1236题意:一个有向图,1,求至少从几个点出发可以遍历该图,2:,求至少添加多少边,使强连通.而,HDU的只有后面一问. 解;先缩点,第一问只需找所有入度为0的点即可.,第2问,max(入度为0的点 ...

  9. HDU 6333 莫队+组合数

    Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

  10. SGU 104 Little shop of flowers【DP】

    浪(吃)了一天,水道题冷静冷静.... 题目链接: http://acm.sgu.ru/problem.php?contest=0&problem=104 题意: 给定每朵花放在每个花盆的值, ...