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

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

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

  2. bzoj 3481 DZY loves math —— 反演+Pollard_rho分解质因数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3481 推式子:xy % P = Q 的个数 由于 0 <= x,y < P,所以 ...

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

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

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

  5. bzoj 3309 DZY Loves Math —— 莫比乌斯反演+数论分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3309 凭着上课所讲和与 Narh 讨论推出式子来: 竟然是第一次写数论分块!所以迷惑了半天: ...

  6. BZOJ 3309: DZY Loves Math 莫比乌斯反演+打表

    有一个神奇的技巧——打表 code: #include <bits/stdc++.h> #define N 10000007 #define ll long long #define se ...

  7. ●BZOJ 3309 DZY Loves Math

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

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

  9. 【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) ...

随机推荐

  1. LeetCode – Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  2. 混合背包 hdu5410 CRB and His Birthday

    传送门:点击打开链接 题意:你有M块钱,如今有N件商品 第i件商品要Wi块,假设你购买x个这种商品.你将得到Ai*x+Bi个糖果 问能得到的最多的糖果数 思路:很好的一道01背包和全然背包结合的题目 ...

  3. JobClient

    /** * <code>JobClient</code> is the primary interface for the user-job to interact * wit ...

  4. 负载均衡之F5设备

    http://xjsunjie.blog.51cto.com/999372/666672 目前全球范围内应用比较广泛的负载均衡设备为美国的F5.F5于2000年底进驻中国,在国内业界,F5负载均衡产品 ...

  5. Android - Activity定制横屏(landscape)显示

    Activity定制横屏(landscape)显示 本文地址: http://blog.csdn.net/caroline_wendy Android横屏(landscape)显示:  android ...

  6. windows下taskkill命令简介

    1.简介 使用该工具可以按照进程 ID (PID) 或映像名称终止任务. 2.语法 TASKKILL [/S system [/U username [/P [password]]]]        ...

  7. 【程序猿联盟】官网上线啦!coderunity.com

    wx_fmt=jpeg" alt="" style="max-width:100%; height:auto!important"> 内容简单介 ...

  8. java 回文字符串

    package string.string1_5; public class Palindrome { /** * 从两头向中间移动 * @param str * @return */ public ...

  9. JAVA 数据筛选(第一笔数据与第二笔数据比较)

    第一笔数据与第二笔数据比较 Map<String, Object> jHpictureMap = new HashMap<String, Object>(); // 存放照片S ...

  10. Appium python自动化测试系列之认识Appium(四)

    ​4.1界面认识 在之前安装appium的时候说过我们有两种方法安装,也就有两种结果,一种是有界面的,一种是没有界面的,首先我们先讲一下有界面的,以及界面有哪些东西. 首先看第一幅图,如果你的是win ...