【HDU 5628】—Clarke and math(狄利克雷卷积快速幂)
题意:给定,求
做法:
由于狄利克雷卷积满足结合律
快速幂就完了
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define re register
#define pb push_back
#define cs const
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?(a+=mod):0;}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){
for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));return res;
}
inline void chemx(ll &a,ll b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;};
#define poly vector<int>
inline poly operator *(cs poly &a,cs poly &b){
int deg=a.size();
poly c(deg,0);
for(int i=1;i<deg;i++){
for(int j=1;i*j<deg;j++){
Add(c[i*j],mul(a[i],b[j]));
}
}
return c;
}
inline poly ksm(poly a,int b){
poly e=a;b--;
for(;b;b>>=1,a=a*a)if(b&1)e=a*e;
return e;
}
int n,k;
poly f,I;
int main(){
int T=read();
while(T--){
f.clear(),I.clear();
n=read(),k=read();
f.pb(0),I.pb(0);
for(int i=0;i<n;i++)f.pb(read());
for(int i=0;i<n;i++)I.pb(1);
I=ksm(I,k);
f=f*I;
for(int i=1;i<n;i++)cout<<f[i]<<" ";cout<<f[n]<<'\n';
}
}
做法
显然是一个积性函数
只用考虑怎么算
发现就是一个阶前缀
的答案就是
线筛的时候维护一下几次幂就可以了
#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0,f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define cs const
#define poly vector<int>
#define db double
#define bg begin
cs int mod=1e9+7,G=3;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=100005;
int inv[N];
int n,k;
inline void init(cs int len=N-5){
inv[0]=inv[1]=1;
for(int i=2;i<=len;i++)inv[i]=mul(mod-mod/i,inv[mod%i]);
}
bitset<N> vis;
int pr[N],tot,f[N],c[N],p[N],tmp[N];
int main(){
int T=read();
init();
while(T--){
n=read(),k=read();
memset(f,0,sizeof(f));
memset(c,0,sizeof(c));
memset(tmp,0,sizeof(tmp));
tot=0,vis.reset();
f[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){
pr[++tot]=i;
f[i]=k,c[i]=1;
}
for(int j=1;j<=tot&&i*pr[j]<=n;j++){
vis[i*pr[j]]=1;
if(i%pr[j]==0){
c[i*pr[j]]=c[i]+1;
f[i*pr[j]]=mul(f[i],mul(inv[c[i]+1],k+c[i]));
break;
}
f[i*pr[j]]=mul(f[i],f[pr[j]]);
c[i*pr[j]]=1;
}
}
for(int i=1;i<=n;i++)p[i]=read();
for(int i=1;i<=n;i++)
for(int j=1;i*j<=n;j++)
Add(tmp[i*j],mul(f[i],p[j]));
for(int i=1;i<n;i++)cout<<tmp[i]<<" ";
cout<<tmp[n]<<'\n';
}
}