ABC239Ex Dice Product 2 题解
ABC239H
简单题。
令
容易得到
将
提出
使用 umap 后,时间复杂度为
代码:
#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,m;
unordered_map<int,int> f;
int power(int a,int b){
int res=1;
for(;b>0;a=1ll*a*a%mod,b>>=1)if(b&1)res=1ll*res*a%mod;
return res;
}
int dfs(int x){
if(x==1)return 0;
if(f[x])return f[x];
for(int l=2,r;l<=n;l=r+1){
if((x-1)/l==0)r=n;
else r=min(n,(x-1)/((x-1)/l));
f[x]=(f[x]+(r-l+1)*1ll*dfs((x-1)/l+1)%mod)%mod;
}
f[x]=(1ll*f[x]%mod+n)*power(n-1,mod-2)%mod;
return f[x];
}
int main(){
cin>>n>>m;
cout<<dfs(m+1)<<"\n";
return 0;
}