题解:AT_arc182_c [ARC182C] Sum of Number of Divisors of Product
wangshulin · · 题解
前言
没有前言。
思路
考虑 dp,带点推公式。
首先,众所周知,计算正因数个数是有公式的:设
接着,假设在原来正因数个数为
这样,我们就只需要记录对于所有的
那么,dp 状态就出来了:
dp 转移即为:
但是,朴素的 dp 时间复杂度为
想到矩阵乘法优化 dp:设
然后对比上方 dp 转移式,发现对于集合
然后你发现还没做完——它要求所有 dp 值的和吧!?
其实我们可以在矩阵
而,展开矩阵式,又有:
对比上方式子,可以推出:
所以,
对矩阵
预处理时间复杂度:
矩阵快速幂时间复杂度:
可以通过本题。
代码
#include<stdio.h>
#include<string.h>
#define ll long long
const int M=17,K=6,MO=998244353;
int m,ans,pri[]={2,3,5,7,11,13},g[M][K];
ll n;
struct Matrix{
int a[(1<<K)+1][(1<<K)+1];
Matrix(){memset(a,0,sizeof(a));}
Matrix operator*(const Matrix &o)const{
Matrix t;
for(int i=0;i<=(1<<K);i++){
for(int j=0;j<=(1<<K);j++){
for(int k=0;k<=(1<<K);k++){
t.a[i][k]+=1ll*a[i][j]*o.a[j][k]%MO,t.a[i][k]%=MO;
}
}
}
return t;
}
};
Matrix R;
void get_g(){
for(int i=1;i<=m;i++){
int x=i;
for(int j=0;j<6;j++){
while(x%pri[j]==0) g[i][j]++,x/=pri[j];
}
}
}
void get_R(){
for(int S=0;S<(1<<K);S++){
for(int T=S;;T=(T-1)&S){
for(int j=1;j<=m;j++){
int p=1;
for(int k=0;k<6;k++){
if((S&(1<<k))&&!(T&(1<<k))) p=1ll*p*g[j][k]%MO;
}
R.a[T][S]+=p,R.a[T][S]%=MO;
}
R.a[T][1<<K]+=R.a[T][S];
if(!T) break;
}
}
R.a[1<<K][1<<K]=1;
}
Matrix fp(Matrix x,ll y){
Matrix t=x;
y--;
while(y){
if(y&1) t=t*x;
x=x*x,y>>=1;
}
return t;
}
int main(){
scanf("%lld%d",&n,&m);
get_g();
get_R();
R=fp(R,n);
printf("%d\n",R.a[0][1<<K]);
return 0;
}