【UR #5】怎样跑得更快
SOL:
vfk说的很详细,就不说了。
#include<bits/stdc++.h> #define mo 998244353 #define LL long long #define int LL inline LL qsm(LL x,LL y=mo-2){ static LL anw; for (anw=1,x%=mo;y;y>>=1,x=x*x%mo) if (y&1) anw=anw*x%mo; return anw; } using namespace std; #define sight(x) ('0'<=x&&x<='9') #define Tap template <class T> Tap inline void read(T &x){ static char c; for (c=getchar();!sight(c);c=getchar()); for (x=0;sight(c);c=getchar())x=x*10+c-48; } void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);} inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); } inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); } #define N 100007 inline void Mo(LL &x) { if (x<0) x=x+mo; } int n,c,d,q; LL f[N],dp[N],b[N]; void rr() { for (int i=1;i<=n;i++) read(b[i]),b[i]=b[i]*dp[i]%mo; for (int i=1;i<=n;i++) for (int j=2*i;j<=n;j+=i) b[j]-=b[i],Mo(b[j]); for (int i=1;i<=n;i++) if (b[i]!=0&&f[i]==0){ puts("-1"); return; }else { b[i]=b[i]*f[i]%mo; } for (int i=n;i;i--) for (int j=2*i;j<=n;j+=i) b[i]-=b[j],Mo(b[i]); for (int i=1;i<=n;i++) writel(b[i]=b[i]*dp[i]%mo); putchar(10); } signed main () { // freopen("a.in","r",stdin); // freopen("a.txt","w",stdout); read(n); read(c); read(d); read(q); c%=mo-1; d%=mo-1; c=c-d; for (int i=1;i<=n;i++) { f[i]=qsm(i,(LL)mo-1+c); assert(0<=f[i]&&f[i]<mo);} for (int i=1;i<=n;i++) for (int j=2*i;j<=n;j+=i) f[j]-=f[i],Mo(f[j]); // for (int i=1;i<=n;i++) writel(f[i]); putchar(10); for (int i=1;i<=n;i++) { f[i]=qsm(f[i]),dp[i]=qsm(i,d),dp[i]=qsm(dp[i]); assert(0<=dp[i]&&dp[i]<mo); } // for (int i=1;i<=n;i++) writel(f[i]); while (q--) rr(); return 0; }