結果

問題 No.1080 Strange Squared Score Sum
ユーザー tko919tko919
提出日時 2020-05-02 23:45:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 740 ms / 5,000 ms
コード長 7,727 bytes
コンパイル時間 3,282 ms
コンパイル使用メモリ 215,664 KB
実行使用メモリ 82,460 KB
最終ジャッジ日時 2024-06-10 14:05:10
合計ジャッジ時間 13,809 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
64,692 KB
testcase_01 AC 73 ms
64,692 KB
testcase_02 AC 377 ms
73,824 KB
testcase_03 AC 723 ms
82,460 KB
testcase_04 AC 225 ms
69,216 KB
testcase_05 AC 220 ms
69,424 KB
testcase_06 AC 109 ms
65,848 KB
testcase_07 AC 144 ms
67,040 KB
testcase_08 AC 381 ms
73,256 KB
testcase_09 AC 373 ms
73,460 KB
testcase_10 AC 105 ms
65,804 KB
testcase_11 AC 703 ms
82,400 KB
testcase_12 AC 379 ms
73,432 KB
testcase_13 AC 740 ms
82,048 KB
testcase_14 AC 377 ms
73,648 KB
testcase_15 AC 71 ms
64,748 KB
testcase_16 AC 724 ms
82,064 KB
testcase_17 AC 381 ms
73,532 KB
testcase_18 AC 380 ms
73,660 KB
testcase_19 AC 372 ms
73,648 KB
testcase_20 AC 705 ms
82,352 KB
testcase_21 AC 709 ms
82,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<typename T=int>inline T get(){
   char c=getchar(); bool neg=(c=='-');
   T res=neg?0:c-'0'; while(isdigit(c=getchar()))res=res*10+(c-'0');
   return neg?-res:res;
}
template<typename T=int>inline void put(T x,char c='\n'){
   if(x<0)putchar('-'),x*=-1; int d[20],i=0;
   do{d[i++]=x%10;}while(x/=10); while(i--)putchar('0'+d[i]);
   putchar(c);
}
//end


template<unsigned mod=1000000009>struct fp {
   unsigned v;
   static unsigned get_mod(){return mod;}
   unsigned inv() const{
      int tmp,a=v,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0)x+=mod; return x;
   }
   fp():v(0){}
   fp(ll x):v(x>=0?x%mod:mod+(x%mod)){}
   fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;}
   fp& operator+=(const fp& x){if((v+=x.v)>=mod)v-=mod;return *this;}
   fp& operator-=(const fp& x){if((v+=mod-x.v)>=mod)v-=mod; return *this;}
   fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
   fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
   fp operator+(const fp& x)const{return fp(*this)+=x;}
   fp operator-(const fp& x)const{return fp(*this)-=x;}
   fp operator*(const fp& x)const{return fp(*this)*=x;}
   fp operator/(const fp& x)const{return fp(*this)/=x;}
   bool operator==(const fp& x)const{return v==x.v;}
   bool operator!=(const fp& x)const{return v!=x.v;}
}; using Fp=fp<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1; unsigned mod=Fp::get_mod();
      rep(i,2,maxx){
         Fact[i]=Fact[i-1]*i;
         Inv[i]=Inv[mod%i]*(mod-mod/i);
         Finv[i]=Finv[i-1]*Inv[i];
      }
   }
   T fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}
   T inv(int n){return Inv[n];}
   T nPr(int n,int r){if(n<0||n<r||r<0)return Fp(0);else return Fact[n]*Finv[n-r];}
   T nCr(int n,int r){if(n<0||n<r||r<0)return Fp(0);else return Fact[n]*Finv[r]*Finv[n-r];}
};

template<typename T,unsigned p>struct NTT{
   vector<T> rt,irt;
   NTT(int lg=21){
      const unsigned m=T(-1).v; T prt=p;
      rt.resize(1<<lg,1); irt.resize(1<<lg,1);
      for(int w=0;w<lg;w++){
         int mask=1<<w; T g=prt.pow(m>>w),ig=g.inv();
         for(int i=0;i<mask-1;i++){
            rt[mask+i+1]=g*rt[mask+i];
            irt[mask+i+1]=ig*irt[mask+i];
         }
      }
   }
   void ntt(vector<T>& f,bool inv=0){
      int n=f.size();
      if(inv){
         for(int i=1;i<n;i<<=1)for(int j=0;j<n;j+=i*2)for(int k=0;k<i;k++){
            f[i+j+k]*=irt[i*2+k]; const T tmp=f[j+k]-f[i+j+k];
            f[j+k]+=f[i+j+k]; f[i+j+k]=tmp;
         } T mul=T(n).inv(); rep(i,0,n)f[i]*=mul;
      }else{
         for(int i=n>>1;i;i>>=1)for(int j=0;j<n;j+=i*2)for(int k=0;k<i;k++){
            const T tmp=f[j+k]-f[i+j+k];
            f[j+k]+=f[i+j+k]; f[i+j+k]=tmp*rt[i*2+k];
         }
      }
   }
   vector<T> conv(vector<T> a,vector<T> b,bool same){
      int n=a.size()+b.size()-1,m=1; while(m<n)m<<=1;
      a.resize(m); ntt(a);
      if(same)rep(i,0,m)a[i]*=a[i];
      else{b.resize(m); ntt(b); rep(i,0,m)a[i]*=b[i];}
      ntt(a,1); a.resize(n); return a;
   }
};
using M1=fp<1045430273>; using M2=fp<1051721729>; using M3=fp<1053818881>;
NTT<fp<1045430273>,3> N1; NTT<fp<1051721729>,6> N2; NTT<fp<1053818881>,7> N3;
inline vector<Fp> multiply(vector<Fp> a,vector<Fp> b,bool same=0){
   int n=a.size()+b.size()-1; vector<Fp> res(n); vector<int> vals[3];
   vector<int> aa(a.size()),bb(b.size());
   rep(i,0,a.size())aa[i]=a[i].v; rep(i,0,b.size())bb[i]=b[i].v;
   vector<M1> a1(ALL(aa)),b1(ALL(bb)),c1=N1.conv(a1,b1,same);
   vector<M2> a2(ALL(aa)),b2(ALL(bb)),c2=N2.conv(a2,b2,same);
   vector<M3> a3(ALL(aa)),b3(ALL(bb)),c3=N3.conv(a3,b3,same);
   for(M1 x:c1)vals[0].push_back(x.v);
   for(M2 x:c2)vals[1].push_back(x.v);
   for(M3 x:c3)vals[2].push_back(x.v);
   M2 r_12=175287122;
   M3 r_13=395182206,r_23=526909943,r_1323=461108887;
   Fp w1=1045430273; Fp w2=372986501;
   rep(i,0,n){
      ll a=vals[0][i];
      ll b=(vals[1][i]+M2::get_mod()-a)*r_12.v%M2::get_mod();
      ll c=((vals[2][i]+M3::get_mod()-a)*r_1323.v+
         (M3::get_mod()-b)*r_23.v)%M3::get_mod();
      res[i]=(a+b*w1.v+c*w2.v);
   } return res;
}

factorial<Fp> fact(1048576);
template<typename T>struct Poly{
   vector<T> f;
   Poly(){}
   Poly(int _n):f(_n){}
   Poly(vector<T> _f){f=_f;}
   T& operator[](const int i){return f[i];}
   T eval(T x){T res,w=1; for(T v:f)res+=w*v,w*=x; return res;}
   int size()const{return f.size();}
   Poly resize(int n){Poly res=*this; res.f.resize(n); return res;}
   void shrink(){while(!f.empty() and f.back()==0)f.pop_back();}
   Poly inv()const{
      assert(f[0]!=0); int n=f.size(); Poly res(1); res[0]=f[0].inv();
      for(int k=1;k<n;k<<=1){
         Poly g=res,h=*this; h=h.resize(k*2); res=res.resize(k*2);
         g=(g.square()*h).resize(k*2); rep(i,k,min(k*2,n))res[i]-=g[i];
      } return res;
   }
   Poly square(){return Poly(multiply(f,f,1));}
   Poly operator+(const Poly& g)const{return Poly(*this)+=g;}
   Poly operator-(const Poly& g)const{return Poly(*this)-=g;}
   Poly operator*(const Poly& g)const{return Poly(*this)*=g;}
   Poly operator/(const Poly& g)const{return Poly(*this)/=g;}
   Poly operator%(const Poly& g)const{return Poly(*this)%=g;}
   Poly& operator+=(Poly g){
      if(g.size()>f.size())f.resize(g.size());
      rep(i,0,g.size())f[i]+=g[i]; shrink(); return *this;
   }
   Poly& operator-=(Poly g){
      if(g.size()>f.size())f.resize(g.size());
      rep(i,0,g.size())f[i]-=g[i]; shrink(); return *this;
   }
   Poly& operator*=(Poly g){f=multiply(f,g.f); shrink(); return *this;}
   Poly& operator/=(Poly g){
      if(g.size()>f.size())return *this=Poly();
      reverse(ALL(f)); reverse(ALL(g.f));
      int n=f.size()-g.size()+1;
      f.resize(n); g.f.resize(n);
      *this*=g.inv(); f.resize(n);
      reverse(ALL(f)); shrink(); return *this;
   }
   Poly& operator%=(Poly g){*this-=*this/g*g; shrink(); return *this;}
   Poly diff(){Poly res(f.size()-1); rep(i,0,res.size())res[i]=f[i+1]*(i+1); return res;}
   Poly inte(){Poly res(f.size()+1); for(int i=res.size()-1;i;i--)res[i]=f[i-1]*fact.inv(i); return res;}
   Poly log(){
      assert(f[0]==1); int n=f.size(); Poly res=diff()*inv(); 
      res=res.inte(); return res.resize(n);
   }
   Poly exp(){
      assert(f[0]==0); int n=f.size();
      Poly res(1),g(1); res[0]=g[0]=1;
      for(int k=1;k<n;k<<=1){
         g=(g+g-g.square()*res).resize(k);
         Poly q=resize(k).diff();
         Poly w=(q+g*(res.diff()-res*q)).resize(2*k-1);
         res=(res+res*(resize(k*2)-w.inte())).resize(2*k);
      } return res.resize(n);
   }
   Poly shift(int c){
      int n=f.size(); Poly res=*this,mul(n); mul[1]=c; mul=mul.exp();
      rep(i,0,n)res[i]*=fact.fact(i); reverse(ALL(res.f)); 
      res*=mul; res=res.resize(n); reverse(ALL(res.f));
      rep(i,0,n)res[i]*=fact.fact(i,1); return res;
   }
};

constexpr int I=430477711;

int main(){
   int n=get();
   Poly<Fp> f(n+1); rep(i,1,n+1)f[i]=Fp(1LL*(i+1)*(i+1))*I;
   Poly<Fp> s=f.exp(),t=s.inv();
   Fp c1=Fp(I*2).inv(),c2=Fp(2).inv();
   rep(i,1,n+1){
      Fp res=(s[i]-t[i])*c1+(s[i]+t[i])*c2;
      res*=fact.fact(n); put(res.v);
   }
   return 0;
}
0