学了一种新套路,倒序打表函数的逆元可以直接线性完成

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define LL long long
const long long mod = 1e9+;
#define maxn 1000001 LL Pow(LL a,LL b){
if (!b)
return 1LL;
LL x=Pow(a,b/);
x=x*x%mod;
if (b&1LL)
x=x*a%mod;
return x;
}
/*ll Pow(ll a,ll b){
ll res=1;
while(b){
if(b%2)
res=res*a%mod;
b>>=1;a=a*a%mod;
}
return res;
}*/
ll n,m;
ll F[maxn],pre[maxn],invF[maxn];
void init1(){
F[]=;F[]=;
for(int i=;i<maxn;i++)
F[i]=(F[i-]+F[i-])%mod;
pre[]=;
for(int i=;i<maxn;i++)//临时数组算累乘
pre[i]=pre[i-]*F[i]%mod; ll tmp=Pow(pre[maxn-],mod-);
for(int i=maxn-;i>=;i--)
invF[i]=tmp*pre[i-]%mod,tmp=tmp*F[i]%mod;
} bool vis[maxn];
ll prime[maxn],mm,mu[maxn];
void init2(){
mu[]=;
for(int i=;i<maxn;i++){
if(!vis[i]){
mu[i]=-;
prime[++mm]=i;
}
for(int j=;j<=mm;j++){
if(i*prime[j]>=maxn)break;
vis[i*prime[j]]=;
if(i%prime[j]==){
mu[i*prime[j]]=;
break;
}
else mu[i*prime[j]]=-mu[i];
}
}
} ll mul[maxn],invmul[maxn],g[maxn];
void init3(){
for(int i=;i<maxn;i++)g[i]=;
for(int i=;i<maxn;i++)
for(int j=;j*i<maxn;j++){
if(mu[j]==)
g[i*j]=g[i*j]*F[i]%mod;
else if(mu[j]==-)
g[i*j]=g[i*j]*invF[i]%mod;
}
mul[]=;
for(int i=;i<maxn;i++)
mul[i]=mul[i-]*g[i]%mod;
invmul[maxn-]=Pow(mul[maxn-],mod-);
for(int i=maxn-;i>=;i--)
invmul[i]=invmul[i+]*g[i+]%mod;
} int main(){
int t;cin>>t;
init1();
init2();
init3();
while(t--){
cin>>n>>m;
if(n>m)swap(n,m);
ll ans=;
for(int l=,r;l<=n;l=r+){
r=min(n/(n/l),m/(m/l));
ll tmp=mul[r]*invmul[l-]%mod;
ans=ans*Pow(tmp,(n/l)*(m/l)%(mod-))%mod;
}
cout<<ans<<'\n';
}
}

累乘函数线性逆元打表,阶乘反演——bzoj4816的更多相关文章

  1. hdu6397 Character Encoding 隔板法+容斥原理+线性逆元方程

    题目传送门 题意:给出n,m,k,用m个0到n-1的数字凑出k,问方案数,mod一个值. 题目思路: 首先如果去掉数字范围的限制,那么就是隔板法,先复习一下隔板法. ①k个相同的小球放入m个不同的盒子 ...

  2. 求组合数、求逆元、求阶乘 O(n)

    在O(n)的时间内求组合数.求逆元.求阶乘.·.· #include <iostream> #include <cstdio> #define ll long long ;// ...

  3. 【bzoj2401】陶陶的难题I “高精度”+欧拉函数+线性筛

    题目描述 求 输入 第一行包含一个正整数T,表示有T组测试数据.接下来T<=10^5行,每行给出一个正整数N,N<=10^6. 输出 包含T行,依次给出对应的答案. 样例输入 7 1 10 ...

  4. 左神算法第五节课:认识哈希函数和哈希表,设计RandomPool结构,布隆过滤器,一致性哈希,岛问题,并查集结构

    认识哈希函数和哈希表 MD5Hash值的返回范围:0~9+a~f,是16位,故范围是0~16^16(2^64)-1, [Hash函数],又叫散列函数: Hash的性质: 1)  输入域无穷大: 2)  ...

  5. [SDOI2008]沙拉公主的困惑 线性筛_欧拉函数_逆元_快速幂

    Code: #include<cstdio> using namespace std; typedef long long ll; const int maxn=10000000+1; l ...

  6. bzoj2693--莫比乌斯反演+积性函数线性筛

    推导: 设d=gcd(i,j) 利用莫比乌斯函数的性质 令sum(x,y)=(x*(x+1)/2)*(y*(y+1)/2) 令T=d*t 设f(T)= T可以分块.又由于μ是积性函数,积性函数的约束和 ...

  7. COGS2531. [HZOI 2016]函数的美 打表+欧拉函数

    题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...

  8. 欧拉函数 cojs 2181. 打表

    cojs 2181. 打表 ★☆   输入文件:sendtable.in   输出文件:sendtable.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 有一道比赛题 ...

  9. bzoj 2186 [Sdoi2008]沙拉公主的困惑(欧拉函数,逆元)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2186 [题意] 若干个询问,求1..n!中与m!互质的个数. [思路] 首先有gcd( ...

随机推荐

  1. 【leetcode】954. Array of Doubled Pairs

    题目如下: Given an array of integers A with even length, return true if and only if it is possible to re ...

  2. mongodb4.0数据库权限配置

    今天给大家分享一个关于mongodb数据库权限配置的小知识点,这里呢,我用的是mongodb4.0版本,下载地址:https://www.mongodb.com/download-center/com ...

  3. delphi 打印 PDevMode 说明

    //PDevMode = _devicemodeW;// _devicemodeW = record// dmDeviceName: array[0..CCHDEVICENAME - 1] of Wi ...

  4. 批量新增数据(BuckCopy)

    批量新增数据(BuckCopy) 使用webService传输数据时要注意,Datatable中的数据类型,以及科学计数 /// <summary> /// 批量新增数据 /// < ...

  5. SCP-bzoj-1078

    项目编号:bzoj-1078 项目等级:Safe 项目描述: 戳这里 特殊收容措施: 斜堆有一个著名的性质:如果一个节点没有左子树,则它一定没有右子树,这也是它“斜堆”名称的由来. 此题通过给出斜堆来 ...

  6. iOS 几种定时器

    //第一种 每一秒执行一次(重复性) double delayInSeconds = 1.0; timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_ ...

  7. Jquery的Ready方法加载为什么两次?

    Ready方法会调用两次? 查看对应的页面是否存在<iframe src="#" --> 存在iframe加载这个页面的时候,页面就会加载两次. $(document) ...

  8. 10. Python面向对象

    Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.如果接触过java语言同学应该都知道,Java面向对象三大特征是:封装.继承.多态.Pytho ...

  9. centos7下搭建nfs服务

    现在有3台服务器 s1(主),s2(从), s3(从)需要实现文件实时同步,我们可以安装Nfs服务端和客户端来实现! 一.安装 NFS 服务器所需的软件包: yum install -y nfs-ut ...

  10. [已解决]报错run `npm audit fix` to fix them, or `npm audit` for details

    问题: added 246 packages from 681 contributors and audited 382 packages in 17.509s found 13 vulnerabil ...