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) ...
随机推荐
- Java中的BigInteger在ACM中的应用
Java中的BigInteger在ACM中的应用 在ACM中的做题时,常常会遇见一些大数的问题.这是当我们用C或是C++时就会认为比較麻烦.就想有没有现有的现有的能够直接调用的BigInter,那样就 ...
- 关于C语言中二维数组传參————————【Badboy】
直接上代码: #include void Fun(int *a[],int m,int n)// { printf("%d\t",*a);//[0][0] /* int e[2][ ...
- javascript 怎么操纵OGNL标签
吧ONGL标签放到html标签中,来操作html的标签就能够了 样例代码: html <div id="categoryid" style="display:non ...
- leetcode 题解 || Remove Nth Node From End of List 问题
problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- xpages很不错的demo
之前有上传了xpages的样例,如今统一把地址发出来,希望对学习xpages的朋友有帮助 1)这是主要的教程,在没有扩展库之前的教程,假设能熟练使用这个样例已经够了,加上你有html,js,css的功 ...
- HTML+CSS要点
1.td占据多行 / 列时,其挤开的 td 不写(但是包裹 td 的 tr 要写) 2. display:td 的元素中的文本默认垂直不居中(table中的td中的文本是垂直居中的) 3.th虽然定义 ...
- unittest相关文档
文档链接: http://blog.csdn.net/wangst4321/article/details/8454118
- 安卓版本6.0打开uiautomator报错
可能是appium打开了,被占用:或者是找不到手机
- React Native 学习(三)之 FlexBox 布局
React Native 学习(三)之 FlexBox 布局
- Lua_第17 章 数学库
第17 章 数学库 在这一章中(以下关于标准库的几章中相同)我的主要目的不是对每个函数给出完整地说明,而是告诉你标准库可以提供什么功能.为了可以清楚地说明问题,我可能 会忽略一些小的选项或者行为.基本 ...