結果

問題 No.856 増える演算
ユーザー LayCurseLayCurse
提出日時 2019-07-26 23:27:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 14,283 bytes
コンパイル時間 2,422 ms
コンパイル使用メモリ 211,408 KB
実行使用メモリ 23,040 KB
最終ジャッジ日時 2024-07-02 09:56:38
合計ジャッジ時間 9,570 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
22,388 KB
testcase_01 AC 22 ms
15,564 KB
testcase_02 AC 23 ms
16,332 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 22 ms
14,928 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 22 ms
14,924 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 24 ms
15,052 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 TLE -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
In member function 'mint mint::inverse()',
    inlined from 'mint& mint::operator/=(mint)' at main.cpp:160:31,
    inlined from 'int main()' at main.cpp:626:10:
main.cpp:218:11: warning: 'tmp.mint::val' may be used uninitialized [-Wmaybe-uninitialized]
  218 |     int a=val, b=md, t, u=1, v=0;
      |           ^~~
main.cpp: In function 'int main()':
main.cpp:579:13: note: 'tmp.mint::val' was declared here
  579 |   mint res, tmp;
      |             ^~~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define MD 1000000007
void *wmem;
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
template<class T> void walloc1d(T **arr, int x, void **mem = &wmem){
  (*arr)=(T*)(*mem);
  (*mem)=((*arr)+x);
}
struct mint{
  static unsigned R, RR, Rinv, W, md, mdninv;
  unsigned val;
  mint(){
  }
  mint(int a){
    val = mulR(a);
  }
  mint(unsigned a){
    val = mulR(a);
  }
  mint(long long a){
    val = mulR(a);
  }
  mint(unsigned long long a){
    val = mulR(a);
  }
  int get_inv(long long a, int md){
    long long e, s=md, t=a, u=1, v=0;
    while(s){
      e=t/s;
      t-=e*s;
      u-=e*v;
      swap(t,s);
      swap(u,v);
    }
    if(u<0){
      u+=md;
    }
    return u;
  }
  void setmod(unsigned m){
    int i;
    unsigned t;
    W = 32;
    md = m;
    R = (1ULL << W) % md;
    RR = (unsigned long long)R*R % md;
    switch(m){
      case 104857601:
      Rinv = 2560000;
      mdninv = 104857599;
      break;
      case 998244353:
      Rinv = 232013824;
      mdninv = 998244351;
      break;
      case 1000000007:
      Rinv = 518424770;
      mdninv = 2226617417U;
      break;
      case 1000000009:
      Rinv = 171601999;
      mdninv = 737024967;
      break;
      case 1004535809:
      Rinv = 234947584;
      mdninv = 1004535807;
      break;
      case 1007681537:
      Rinv = 236421376;
      mdninv = 1007681535;
      break;
      case 1012924417:
      Rinv = 238887936;
      mdninv = 1012924415;
      break;
      case 1045430273:
      Rinv = 254466304;
      mdninv = 1045430271;
      break;
      case 1051721729:
      Rinv = 257538304;
      mdninv = 1051721727;
      break;
      default:
      Rinv = get_inv(R, md);
      mdninv = 0;
      t = 0;
      for(i=0;i<(int)W;i++){
        if(t%2==0){
          t+=md;
          mdninv |= (1U<<i);
        }
        t /= 2;
      }
    }
  }
  unsigned mulR(unsigned a){
    return (unsigned long long)a*R%md;
  }
  unsigned mulR(int a){
    if(a < 0){
      a = a%md+md;
    }
    return mulR((unsigned)a);
  }
  unsigned mulR(unsigned long long a){
    return mulR((unsigned)(a%md));
  }
  unsigned mulR(long long a){
    a %= md;
    if(a < 0){
      a += md;
    }
    return mulR((unsigned)a);
  }
  unsigned reduce(unsigned T){
    unsigned m=T * mdninv, t=(unsigned)((T + (unsigned long long)m*md) >> W);
    if(t >= md){
      t -= md;
    }
    return t;
  }
  unsigned reduce(unsigned long long T){
    unsigned m=(unsigned)T * mdninv, t=(unsigned)((T + (unsigned long long)m*md) >> W);
    if(t >= md){
      t -= md;
    }
    return t;
  }
  unsigned get(){
    return reduce(val);
  }
  mint &operator+=(mint a){
    val += a.val;
    if(val >= md){
      val -= md;
    }
    return *this;
  }
  mint &operator-=(mint a){
    if(val < a.val){
      val = val + md - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  mint &operator*=(mint a){
    val = reduce((unsigned long long)val*a.val);
    return *this;
  }
  mint &operator/=(mint a){
    return *this *= a.inverse();
  }
  mint operator+(mint a){
    return mint(*this)+=a;
  }
  mint operator-(mint a){
    return mint(*this)-=a;
  }
  mint operator*(mint a){
    return mint(*this)*=a;
  }
  mint operator/(mint a){
    return mint(*this)/=a;
  }
  mint operator+(int a){
    return mint(*this)+=mint(a);
  }
  mint operator-(int a){
    return mint(*this)-=mint(a);
  }
  mint operator*(int a){
    return mint(*this)*=mint(a);
  }
  mint operator/(int a){
    return mint(*this)/=mint(a);
  }
  mint operator+(long long a){
    return mint(*this)+=mint(a);
  }
  mint operator-(long long a){
    return mint(*this)-=mint(a);
  }
  mint operator*(long long a){
    return mint(*this)*=mint(a);
  }
  mint operator/(long long a){
    return mint(*this)/=mint(a);
  }
  mint operator-(void){
    mint res;
    if(val){
      res.val=md-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  operator bool(void){
    return val!=0;
  }
  operator int(void){
    return get();
  }
  operator long long(void){
    return get();
  }
  mint inverse(){
    int a=val, b=md, t, u=1, v=0;
    mint res;
    while(b){
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    if(u < 0){
      u += md;
    }
    res.val = (unsigned long long)u*RR % md;
    return res;
  }
  mint pw(unsigned long long b){
    mint a(*this), res;
    res.val = R;
    while(b){
      if(b&1){
        res *= a;
      }
      b >>= 1;
      a *= a;
    }
    return res;
  }
  bool operator==(int a){
    return mulR(a)==val;
  }
  bool operator!=(int a){
    return mulR(a)!=val;
  }
}
;
mint operator+(int a, mint b){
  return mint(a)+=b;
}
mint operator-(int a, mint b){
  return mint(a)-=b;
}
mint operator*(int a, mint b){
  return mint(a)*=b;
}
mint operator/(int a, mint b){
  return mint(a)/=b;
}
mint operator+(long long a, mint b){
  return mint(a)+=b;
}
mint operator-(long long a, mint b){
  return mint(a)-=b;
}
mint operator*(long long a, mint b){
  return mint(a)*=b;
}
mint operator/(long long a, mint b){
  return mint(a)/=b;
}
inline void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void wt_L(char a){
  putchar_unlocked(a);
}
inline void wt_L(int x){
  char f[10];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
inline void wt_L(mint x){
  int i;
  i = (int)x;
  wt_L(i);
}
template<class T, class S> inline T pow_L(T a, S b){
  T res=1;
  res = 1;
  while(b){
    if(b&1){
      res *= a;
    }
    b >>= 1;
    a *= a;
  }
  return res;
}
char memarr[96000000];
unsigned mint::R, mint::RR, mint::Rinv, mint::W, mint::md, mint::mdninv;
#define PI 3.141592653589793238462
struct pnt{
  double x, y;
  static double eps;
  pnt(void){
  }
  pnt(double a,double b){
    x=a;
    y=b;
  }
  void set(double a,double b){
    x=a;
    y=b;
  }
  pnt&operator+=(pnt a){
    x+=a.x;
    y+=a.y;
    return*this;
  }
  pnt&operator-=(pnt a){
    x-=a.x;
    y-=a.y;
    return*this;
  }
  pnt&operator*=(pnt a){
    pnt p=*this;
    x=p.x*a.x-p.y*a.y;
    y=p.x*a.y+p.y*a.x;
    return*this;
  }
  pnt operator+(pnt a){
    return pnt(*this)+=a;
  }
  pnt operator-(pnt a){
    return pnt(*this)-=a;
  }
  pnt operator*(pnt a){
    return pnt(*this)*=a;
  }
}
;
double pnt::eps = 1e-10;
unsigned long long pw(unsigned long long a, unsigned long long b, unsigned long long m){
  unsigned long long r=1;
  while(b){
    if(b&1){
      r=r*a%m;
    }
    b>>=1;
    a=a*a%m;
  }
  return r;
}
int get_inv(long long a, int md){
  long long e, s=md, t=a, u=1, v=0;
  while(s){
    e=t/s;
    t-=e*s;
    u-=e*v;
    swap(t,s);
    swap(u,v);
  }
  if(u<0){
    u+=md;
  }
  return u;
}
void fft(int n, pnt x[], void *mem){
  double p, t=2*PI/n;
  int I, J, K, i, j, s=1;
  pnt A, B, C, D, a, b, c, d, u, v, w, *y=(pnt*)mem;
  while(n>2){
    I=n/4;
    J=I+I;
    K=I+J;
    for(i=0;i<I;i++){
      w=pnt(cos(i*t),-sin(i*t));
      v=w*w;
      u=w*v;
      for(j=0;j<s;j++){
        a=x[j+s*i];
        b=x[j+s*(i+I)];
        c=x[j+s*(i+J)];
        d=x[j+s*(i+K)];
        A=a+c;
        B=a-c;
        C=b+d;
        D=b-d;
        p=D.y;
        D.y=D.x;
        D.x=-p;
        y[j+s*4*i]=A+C;
        y[j+s*(4*i+1)]=w*(B-D);
        y[j+s*(4*i+2)]=v*(A-C);
        y[j+s*(4*i+3)]=u*(B+D);
      }
    }
    n/=4;
    s*=4;
    t*=4;
    swap(x,y);
  }
  if(n==2){
    for(i=0;i<s;i++){
      y[i]=x[i]+x[i+s];
      y[i+s]=x[i]-x[i+s];
    }
    n/=2;
    s*=2;
    t*=2;
    swap(x,y);
  }
  for(i=0;i<s;i++){
    y[i]=x[i];
  }
}
void fftinv(int n, pnt x[], void *mem){
  double p, t=2*PI/n;
  int I, J, K, i, j, s=1;
  pnt A, B, C, D, a, b, c, d, u, v, w, *y=(pnt*)mem;
  while(n>2){
    I=n/4;
    J=I+I;
    K=I+J;
    for(i=0;i<I;i++){
      w=pnt(cos(i*t),sin(i*t));
      v=w*w;
      u=w*v;
      for(j=0;j<s;j++){
        a=x[j+s*i];
        b=x[j+s*(i+I)];
        c=x[j+s*(i+J)];
        d=x[j+s*(i+K)];
        A=a+c;
        B=a-c;
        C=b+d;
        D=b-d;
        p=D.y;
        D.y=D.x;
        D.x=-p;
        y[j+s*4*i]=A+C;
        y[j+s*(4*i+1)]=w*(B+D);
        y[j+s*(4*i+2)]=v*(A-C);
        y[j+s*(4*i+3)]=u*(B-D);
      }
    }
    n/=4;
    s*=4;
    t*=4;
    swap(x,y);
  }
  if(n==2){
    for(i=0;i<s;i++){
      y[i]=x[i]+x[i+s];
      y[i+s]=x[i]-x[i+s];
    }
    n/=2;
    s*=2;
    t*=2;
    swap(x,y);
  }
  for(i=0;i<s;i++){
    y[i]=x[i];
  }
}
void convolution(double A[], int As, double B[], int Bs, double res[], int Rs, void *mem){
  double m;
  int i, k, n;
  pnt *a, *b;
  n=max_L(As+Bs, Rs);
  for(k=1;k<n;k*=2){
    ;
  }
  a=(pnt*)mem;
  b=a+k;
  mem=b+k;
  for(i=0;i<As;i++){
    a[i].set(A[i],0);
  }
  for(i=As;i<k;i++){
    a[i].set(0,0);
  }
  for(i=0;i<Bs;i++){
    b[i].set(B[i],0);
  }
  for(i=Bs;i<k;i++){
    b[i].set(0,0);
  }
  fft(k,a,mem);
  fft(k,b,mem);
  for(i=0;i<k;i++){
    a[i]*=b[i];
  }
  fftinv(k,a,mem);
  m=1.0/k;
  for(i=0;i<Rs;i++){
    res[i]=a[i].x*m;
  }
}
void convolution(double A[], int As, double res[], int Rs, void *mem){
  double m;
  int i, k, n;
  pnt *a;
  n=max_L(As+As, Rs);
  for(k=1;k<n;k*=2){
    ;
  }
  a=(pnt*)mem;
  mem=a+k;
  for(i=0;i<As;i++){
    a[i].set(A[i],0);
  }
  for(i=As;i<k;i++){
    a[i].set(0,0);
  }
  fft(k,a,mem);
  for(i=0;i<k;i++){
    a[i]*=a[i];
  }
  fftinv(k,a,mem);
  m=1.0/k;
  for(i=0;i<Rs;i++){
    res[i]=a[i].x*m;
  }
}
int N;
int A[100000];
double c[100001];
double cnv[200001];
int s[100001];
int mn[100001];
int main(){
  double dmin, dtmp;
  int *ddd, i, j, k;
  mint res, tmp;
  wmem = memarr;
  {
    mint x;
    x.setmod(MD);
  }
  walloc1d(&ddd,1);
  rd(N);
  {
    int Q5VJL1cS;
    for(Q5VJL1cS=0;Q5VJL1cS<N;Q5VJL1cS++){
      rd(A[Q5VJL1cS]);
    }
  }
  res = 1;
  for(i=0;i<N;i++){
    c[A[i]]++;
  }
  convolution(c, 100001, cnv, 200001, wmem);
  for(i=0;i<N;i++){
    cnv[2*A[i]]--;
  }
  for(i=0;i<200001;i++){
    k = (int)(cnv[i]/2 + 0.5);
    if(k){
      res *= (pow_L(mint(i),k));
    }
  }
  for(i=N-1;i>=0;i--){
    s[i] = s[i+1] + A[i];
  }
  for(i=0;i<N;i++){
    res *= (pow_L(mint(A[i]),s[i+1]));
  }
  mn[N] = 1073709056;
  for(i=N-1;i>=0;i--){
    mn[i] =min_L(mn[i+1], A[i]);
  }
  dmin = 1e300;
  for(i=N-2;i>=0;i--){
    dtmp = log(A[i] + mn[i+1]) + log(A[i]) * mn[i+1];
    if(dtmp < dmin){
      dtmp = dmin;
      tmp = A[i] + mn[i+1];
      tmp *=pow_L(mint(A[i]),mn[i+1]);
    }
  }
  res /= tmp;
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20190721-1

// --- original code ---
// #define PI 3.141592653589793238462
// struct pnt{double x,y;static double eps;pnt(void){}pnt(double a,double b){x=a;y=b;}void set(double a,double b){x=a;y=b;}pnt&operator+=(pnt a){x+=a.x;y+=a.y;return*this;}pnt&operator-=(pnt a){x-=a.x;y-=a.y;return*this;}pnt&operator*=(pnt a){pnt p=*this;x=p.x*a.x-p.y*a.y;y=p.x*a.y+p.y*a.x;return*this;}pnt operator+(pnt a){return pnt(*this)+=a;}pnt operator-(pnt a){return pnt(*this)-=a;}pnt operator*(pnt a){return pnt(*this)*=a;}};
// double pnt::eps = 1e-10;
// 
// ull pw(ull a, ull b, ull m){ull r=1;while(b){if(b&1)r=r*a%m;b>>=1;a=a*a%m;}return r;}
// int get_inv(ll a, int md){ll t=a,s=md,u=1,v=0,e;while(s){e=t/s;t-=e*s;u-=e*v;swap(t,s);swap(u,v);}if(u<0)u+=md;return u;}
// 
// void fft(int n, pnt x[], void *mem){int i,j,I,J,K,s=1;double t=2*PI/n,p;pnt w,v,u,a,b,c,d,A,B,C,D,*y=(pnt*)mem;while(n>2){I=n/4;J=I+I;K=I+J;rep(i,I){w=pnt(cos(i*t),-sin(i*t));v=w*w;u=w*v;rep(j,s)a=x[j+s*i],b=x[j+s*(i+I)],c=x[j+s*(i+J)],d=x[j+s*(i+K)],A=a+c,B=a-c,C=b+d,D=b-d,p=D.y,D.y=D.x,D.x=-p,y[j+s*4*i]=A+C,y[j+s*(4*i+1)]=w*(B-D),y[j+s*(4*i+2)]=v*(A-C),y[j+s*(4*i+3)]=u*(B+D);}n/=4;s*=4;t*=4;swap(x,y);}if(n==2){rep(i,s)y[i]=x[i]+x[i+s],y[i+s]=x[i]-x[i+s];n/=2;s*=2;t*=2;swap(x,y);}rep(i,s)y[i]=x[i];}
// void fftinv(int n, pnt x[], void *mem){int i,j,I,J,K,s=1;double t=2*PI/n,p;pnt w,v,u,a,b,c,d,A,B,C,D,*y=(pnt*)mem;while(n>2){I=n/4;J=I+I;K=I+J;rep(i,I){w=pnt(cos(i*t),sin(i*t));v=w*w;u=w*v;rep(j,s)a=x[j+s*i],b=x[j+s*(i+I)],c=x[j+s*(i+J)],d=x[j+s*(i+K)],A=a+c,B=a-c,C=b+d,D=b-d,p=D.y,D.y=D.x,D.x=-p,y[j+s*4*i]=A+C,y[j+s*(4*i+1)]=w*(B+D),y[j+s*(4*i+2)]=v*(A-C),y[j+s*(4*i+3)]=u*(B-D);}n/=4;s*=4;t*=4;swap(x,y);}if(n==2){rep(i,s)y[i]=x[i]+x[i+s],y[i+s]=x[i]-x[i+s];n/=2;s*=2;t*=2;swap(x,y);}rep(i,s)y[i]=x[i];}
// void convolution(double A[], int As, double B[], int Bs, double res[], int Rs, void *mem){int i,n,k;double m;pnt*a,*b;n=max(As+Bs,Rs);for(k=1;k<n;k*=2);a=(pnt*)mem;b=a+k;mem=b+k;rep(i,As)a[i].set(A[i],0);REP(i,As,k)a[i].set(0,0);rep(i,Bs)b[i].set(B[i],0);REP(i,Bs,k)b[i].set(0,0);fft(k,a,mem);fft(k,b,mem);rep(i,k)a[i]*=b[i];fftinv(k,a,mem);m=1.0/k;rep(i,Rs)res[i]=a[i].x*m;}
// void convolution(double A[], int As, double res[], int Rs, void *mem){int i,n,k;double m;pnt*a;n=max(As+As,Rs);for(k=1;k<n;k*=2);a=(pnt*)mem;mem=a+k;rep(i,As)a[i].set(A[i],0);REP(i,As,k)a[i].set(0,0);fft(k,a,mem);rep(i,k)a[i]*=a[i];fftinv(k,a,mem);m=1.0/k;rep(i,Rs)res[i]=a[i].x*m;}
// 
// 
// int N, A[1d5];
// 
// double c[100001], cnv[200001];
// int s[100001];
// int mn[100001];
// {
//   int i, j, k;
//   mint res, tmp;
//   double dmin, dtmp;
//   int *ddd;
// 
//   walloc1d(&ddd,1);
// 
//   rd(N,A(N));
// 
//   res = 1;
//   
//   rep(i,N) c[A[i]]++;
//   convolution(c, 100001, cnv, 200001, wmem);
//   rep(i,N) cnv[2A[i]]--;
// 
//   rep(i,200001){
//     k = (int)(cnv[i]/2 + 0.5);
//     if(k) res *= (mint(i) ** k);
//   }
// 
//   for(i=N-1;i>=0;i--) s[i] = s[i+1] + A[i];
//   rep(i,N) res *= (mint(A[i]) ** s[i+1]);
// 
//   mn[N] = int_inf;
//   for(i=N-1;i>=0;i--) mn[i] = min(mn[i+1], A[i]);
// 
//   dmin = 1e300;
//   for(i=N-2;i>=0;i--){
//     dtmp = log(A[i] + mn[i+1]) + log(A[i]) * mn[i+1];
//     if(dtmp < dmin){
//       dtmp = dmin;
//       tmp = A[i] + mn[i+1];
//       tmp *= mint(A[i]) ** mn[i+1];
//     }
//   }
// 
//   res /= tmp;
// 
//   wt(res);
// }
0