QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#445720#8526. Polygon IIucup-team1231#AC ✓1627ms428548kbC++146.0kb2024-06-16 10:32:162024-06-16 10:32:16

Judging History

This is the latest submission verdict.

  • [2024-06-16 10:32:16]
  • Judged
  • Verdict: AC
  • Time: 1627ms
  • Memory: 428548kb
  • [2024-06-16 10:32:16]
  • Submitted

answer

#pragma GCC optimize("unroll-loops","omit-frame-pointer","inline","rounding-math")//,"signaling-nans")
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
// typedef float16_t fp16;
typedef float fp16;
typedef float fp32;
typedef double fp64;
typedef long long ll;
typedef long double fp128;
typedef double ld;
#define SZ 150003
int n,a[SZ];
const int MOD=1e9+7;
ll qp(ll a,ll b) {
    ll x=1; a%=MOD;
    while(b) {
        if(b&1) x=x*a%MOD;
        a=a*a%MOD; b>>=1;
    }
    return x;
}
const int M=50;
int m,cnt[SZ];
int g[2][1005][1005],t[1005][1005];
#define W 2200
ll fac[SZ],rfac[SZ];
// typedef ld pt[W][W];
typedef complex<ld> cld;
typedef cld ct[W][W];
ct A0,A1,B0,B1,C0,C1,C2,TMP;
template<class T>
void transp(T&s,int k1,int k2) {
    int m=min(k1,k2);
    for(int i=0;i<k1;++i)
        for(int j=0;j<k2;++j) {
            if(i<m&&j<m) {
                if(i<j) swap(s[i][j],s[j][i]);
                continue;
            }
            s[j][i]=s[i][j];
        }
}
int K;
cld w[2][W];
void fftinit() {
    w[0][0]=w[0][K]=1;
    ld ang=2*acos(-1)/K;
    for(int i=1;i<K;++i) w[0][i]=cld(cos(i*ang),sin(i*ang));
    for(int i=0;i<=K;++i) w[1][i]=w[0][K-i];
}
void dft_1d(cld*x,bool v=0) {
    for(int i=0,j=0;i<K;++i) {
        if(i>j) swap(x[i],x[j]);
        for(int l=(K>>1);(j^=l)<l;l>>=1);
    }
    for(int i=2;i<=K;i<<=1) {
        for(int j=0;j<K;j+=i)
            for(int l=0;l<(i>>1);++l) {
                auto t=x[j+l+(i>>1)]*w[v][K/i*l];
                x[j+l+(i>>1)]=x[j+l]-t;
                x[j+l]=x[j+l]+t;
            }
    }
    if(!v) return;
    ld r=ld(1)/K;
    for(int i=0;i<K;++i) x[i]=x[i]*r;
}
void idft_1d(cld*x) {
    dft_1d(x,1);
}
void dft_1d(ct&A,int k1,int k2) {
    K=k2;fftinit();
    for(int i=0;i<k1;++i) dft_1d(A[i]);
}
void idft_1d(ct&A,int k1,int k2) {
    K=k2;fftinit();
    for(int i=0;i<k1;++i) idft_1d(A[i]);
}
void dft(ct&A,int k1,int k2) {
    dft_1d(A,k1,k2);
    transp(A,k1,k2);
    dft_1d(A,k2,k1);
    transp(A,k2,k1);
}
void idft(ct&A,int k1,int k2) {
    idft_1d(A,k1,k2);
    transp(A,k1,k2);
    idft_1d(A,k2,k1);
    transp(A,k2,k1);
}
int A[W][W],B[W][W],X[W][W];
const int BB=32768;
template<class T>
void decomp(T&a,ct&a0,ct&a1,int K1,int K2) {
    for(int i=0;i<K1;++i)
        for(int j=0;j<K2;++j) {
            a[i][j]=(a[i][j]%MOD+MOD)%MOD;
            a0[i][j]=a[i][j]%BB;
            a1[i][j]=a[i][j]/BB;
        }
}
void work(int K1,int K2) {
    decomp(A,A0,A1,K1,K2);
    decomp(B,B0,B1,K1,K2);
    dft(A0,K1,K2);
    dft(A1,K1,K2);
    dft(B0,K1,K2);
    dft(B1,K1,K2);
    for(int i=0;i<K1;++i)
        for(int j=0;j<K2;++j) {
            C0[i][j]=A0[i][j]*B0[i][j];
            C1[i][j]=A0[i][j]*B1[i][j]+A1[i][j]*B0[i][j];
            C2[i][j]=A1[i][j]*B1[i][j];
        }
    idft(C0,K1,K2);
    idft(C1,K1,K2);
    idft(C2,K1,K2);
    for(int i=0;i<K1;++i)
        for(int j=0;j<K2;++j) {
            auto rr=[&](cld u) {
                ll s=round(u.real());
                return s;
            };
            X[i][j]=(rr(C0[i][j])+(rr(C1[i][j])+rr(C2[i][j])*BB)%MOD*BB)%MOD;
        }
    // for(int i=0;i<K1;++i)
    //     for(int j=0;j<K1;++j)
    //         for(int x=0;x<K2;++x)
    //             for(int y=0;x+y<K2;++y)
    //                 (X[i+j][x+y]+=(ll)A[i][x]*B[j][y]%MOD)%=MOD;
}
int getKlog(int s) {
    int u=0;
    while((1<<u)<s) ++u;
    return u;
}
int getK(int s) {
    return 1<<getKlog(s);
}
int main() {
    cin>>n;
    fac[0]=1;
    for(int i=1;i<SZ;++i) fac[i]=fac[i-1]*i%MOD;
    rfac[SZ-1]=qp(fac[SZ-1],MOD-2);
    for(int i=SZ-1;i;--i) rfac[i-1]=rfac[i]*i%MOD;
    // cerr<<rfac[0]<<"+\n";
    for(int i=0;i<n;++i) cin>>a[i],++cnt[a[i]];
    m=n-2;
    ll fc=1;
    for(int i=1;i<=m;++i) fc=fc*i%MOD;
    fc = qp(fc*(m+1),MOD-2)-qp(fc*(m+2),MOD-2);
    ll gg=1;
    for(int i=0;i<n;++i) gg=gg*qp(qp(2,a[i]),MOD-2)%MOD;
    ll aa=0;
    // for(int i=0;i<n;++i) {
    //     for(int j=0;j<(1<<n);++j) {
    //         ll s=0;
    //         for(int k=0;k<n;++k) if(j&(1<<k)) s+=1LL<<a[k];
    //         if(s<=(1LL<<a[i])) aa+=qp(-1,__builtin_popcount(j))*qp((1LL<<a[i])-s,n),aa%=MOD;
    //     }
    // }
    int cu=0,C=0;
    g[0][0][0]=1;
    for(int i=0;i<=50;++i) {
        C^=1;
        for(int j=0;j<=(cu+1)/2;++j) memset(g[C][j],0,sizeof g[0][0]);
        for(int j=0;j<=cu;++j) {
            for(int k=0;k<=n;++k)
                (g[C][(j+1)/2][k]+=g[!C][j][k])%=MOD;
        }
        cu=(cu+1)/2;
        if(!cnt[i]) continue;
        int K1=getK(cnt[i]+cu+1);
        int K2=getK(n+n+1);
        for(int x=0;x<K1;++x)
        for(int y=0;y<K2;++y) A[x][y]=0;
        for(int x=0;x<K1;++x)
        for(int y=0;y<K2;++y) B[x][y]=0;
        for(int x=0;x<=cnt[i];++x) {
            for(int j=0;j<=n;++j)
                A[x][j]=qp((1LL<<i)*x,j)*qp(-1,x)%MOD*rfac[j]%MOD*fac[cnt[i]]%MOD*rfac[x]%MOD*rfac[cnt[i]-x]%MOD;
        }
        for(int x=0;x<=cu;++x) {
            for(int j=0;j<=n;++j)
                B[x][j]=g[C][x][j]*rfac[j]%MOD;
        }
        // for(int x=0;x<=1&&x<=cu;++x)
        //     for(int y=0;y<=n;++y)
        //         cout<<i<<","<<x<<","<<y<<":"<<g[C][x][y]<<"ST+\n";
        work(K1,K2);
        cu+=cnt[i];
        for(int x=0;x<=cu;++x)
            for(int y=0;y<=n;++y) g[C][x][y]=X[x][y]*(ll)fac[y]%MOD;
        ll ca=0;
        for(int x=0;x<=1&&x<=cu;++x)
            for(int y=0;y<=n;++y) {
                ca+=qp(-1,n)*qp(-(1LL<<i),y)*g[C][x][n-y]%MOD*fac[n]%MOD*rfac[y]%MOD*rfac[n-y]%MOD,ca%=MOD;
                // aa+=cnt[i]*qp((1LL<<i)-s,n),aa%=MOD;
            }
        // for(int x=0;x<=1&&x<=cu;++x)
        //     for(int y=0;y<=n;++y)
        //         cout<<i<<","<<x<<","<<y<<":"<<g[C][x][y]<<"+\n";
        aa+=ca*cnt[i]; aa%=MOD;
    }
    aa=aa*fc%MOD;
    aa=aa*gg%MOD;
    aa=(1-aa)%MOD;
    aa=(aa%MOD+MOD)%MOD;
    cout<<aa<<"\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 6640kb

input:

3
0 2 0

output:

166666668

result:

ok 1 number(s): "166666668"

Test #2:

score: 0
Accepted
time: 0ms
memory: 6448kb

input:

3
0 0 0

output:

500000004

result:

ok 1 number(s): "500000004"

Test #3:

score: 0
Accepted
time: 0ms
memory: 6644kb

input:

3
5 6 7

output:

208333335

result:

ok 1 number(s): "208333335"

Test #4:

score: 0
Accepted
time: 3ms
memory: 6444kb

input:

3
0 25 50

output:

889268532

result:

ok 1 number(s): "889268532"

Test #5:

score: 0
Accepted
time: 3ms
memory: 7340kb

input:

10
39 11 25 1 12 44 10 46 27 15

output:

913863330

result:

ok 1 number(s): "913863330"

Test #6:

score: 0
Accepted
time: 7ms
memory: 10068kb

input:

57
43 22 3 16 7 5 24 32 25 16 41 28 24 30 28 10 32 48 41 43 34 37 48 34 3 9 21 41 49 25 2 0 36 45 34 33 45 9 42 29 43 9 38 34 44 33 44 6 46 39 22 36 40 37 19 34 3

output:

400729664

result:

ok 1 number(s): "400729664"

Test #7:

score: 0
Accepted
time: 4ms
memory: 15588kb

input:

100
44 32 6 6 6 44 12 32 6 9 23 12 14 23 12 14 23 49 6 14 32 23 49 9 32 24 23 6 32 6 49 23 12 44 24 9 14 6 24 44 24 23 44 44 49 32 49 12 49 49 24 49 12 23 3 14 6 3 3 6 12 3 49 24 49 24 24 32 23 32 49 14 3 24 49 3 32 14 44 24 49 3 32 23 49 44 44 9 23 14 49 9 3 6 44 24 3 3 12 44

output:

32585394

result:

ok 1 number(s): "32585394"

Test #8:

score: 0
Accepted
time: 845ms
memory: 274728kb

input:

1000
2 27 0 0 27 0 2 0 27 0 27 27 0 0 0 0 0 2 0 27 0 2 2 0 27 27 0 0 0 27 2 2 2 27 0 2 27 2 0 2 27 0 0 27 0 27 0 0 27 2 27 2 2 27 2 27 0 0 27 0 27 0 2 27 2 2 0 27 27 27 27 0 27 0 27 0 2 2 0 2 2 27 0 0 27 0 0 27 0 2 27 27 2 27 2 0 0 2 27 27 27 27 27 27 2 2 0 2 2 0 2 2 0 27 0 27 2 2 0 27 27 0 0 27 2 2...

output:

94588769

result:

ok 1 number(s): "94588769"

Test #9:

score: 0
Accepted
time: 925ms
memory: 175836kb

input:

1000
40 14 47 3 32 18 3 49 22 23 32 18 23 24 18 32 23 39 32 27 49 49 22 50 50 22 23 47 14 47 50 32 22 24 49 49 18 22 18 22 50 3 32 47 40 3 39 22 24 47 32 49 49 22 32 39 14 49 39 3 32 22 24 18 39 49 24 18 40 23 23 49 39 39 18 39 27 49 14 27 27 14 18 24 39 22 40 50 18 18 18 39 39 18 23 23 22 3 49 47 2...

output:

626481946

result:

ok 1 number(s): "626481946"

Test #10:

score: 0
Accepted
time: 1050ms
memory: 121688kb

input:

1000
28 32 35 9 21 11 43 23 45 15 23 2 8 3 39 41 31 9 45 35 27 14 40 28 31 9 31 9 9 40 8 6 27 43 3 27 23 49 27 6 28 25 11 9 15 27 38 27 12 28 25 2 15 27 45 6 27 1 21 38 1 25 27 21 49 31 31 14 39 39 8 39 40 28 15 31 21 14 43 38 11 8 8 23 9 11 15 2 11 39 32 14 28 15 40 49 27 9 23 9 9 6 21 2 2 1 14 11 ...

output:

644443122

result:

ok 1 number(s): "644443122"

Test #11:

score: 0
Accepted
time: 1504ms
memory: 93156kb

input:

972
39 15 23 0 40 29 43 47 6 9 30 9 2 8 19 9 45 25 26 38 33 18 6 33 44 48 24 8 4 16 33 42 33 31 36 33 13 16 3 12 21 19 1 30 24 23 43 35 0 33 31 32 23 31 36 12 26 0 29 48 28 33 28 28 3 49 9 5 29 8 29 28 49 41 33 49 5 49 6 9 50 25 39 11 1 36 6 44 10 34 32 31 25 31 36 36 3 9 50 35 47 43 25 46 30 18 5 2...

output:

684920840

result:

ok 1 number(s): "684920840"

Test #12:

score: 0
Accepted
time: 40ms
memory: 22836kb

input:

147
34 47 42 23 46 3 41 9 15 42 21 32 24 1 19 46 29 35 38 20 2 43 36 47 19 23 20 9 6 28 48 46 45 21 19 41 31 36 50 7 11 25 0 43 38 46 21 2 26 40 32 14 45 35 47 21 13 26 26 30 3 36 35 45 36 21 21 25 2 40 35 50 23 3 16 44 40 42 6 37 36 19 20 14 30 47 13 49 47 45 26 12 15 21 42 30 19 5 21 9 28 8 3 34 4...

output:

972735235

result:

ok 1 number(s): "972735235"

Test #13:

score: 0
Accepted
time: 1627ms
memory: 93220kb

input:

1000
36 15 9 5 35 37 17 30 24 13 18 32 14 35 36 26 23 7 21 15 43 15 21 11 33 33 9 16 5 26 1 45 48 27 20 20 20 48 42 27 22 7 39 35 11 38 33 47 22 34 43 4 32 0 47 35 48 8 9 3 40 3 27 22 20 43 12 37 30 18 2 37 37 35 44 3 42 14 20 24 44 5 17 38 46 41 28 23 21 7 13 15 35 38 21 14 6 37 37 6 13 34 32 13 23...

output:

179933029

result:

ok 1 number(s): "179933029"

Test #14:

score: 0
Accepted
time: 1572ms
memory: 93196kb

input:

1000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7...

output:

540327646

result:

ok 1 number(s): "540327646"

Test #15:

score: 0
Accepted
time: 1588ms
memory: 93160kb

input:

1000
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 47 46 46 46 46 46 46 46 46 46 46 46 46 46 4...

output:

169647494

result:

ok 1 number(s): "169647494"

Test #16:

score: 0
Accepted
time: 657ms
memory: 426124kb

input:

1000
11 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 40 50 50 50 50 50 21 50 12 50 50 50 50 50 0 50 50 50 38 50 50 50 50 50 50 25 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 7 50 50 50 50 50 50 50 50 ...

output:

862643524

result:

ok 1 number(s): "862643524"

Test #17:

score: 0
Accepted
time: 595ms
memory: 426288kb

input:

1000
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 5...

output:

819612372

result:

ok 1 number(s): "819612372"

Test #18:

score: 0
Accepted
time: 636ms
memory: 426428kb

input:

1000
50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 5...

output:

18215579

result:

ok 1 number(s): "18215579"

Test #19:

score: 0
Accepted
time: 4ms
memory: 8100kb

input:

16
0 2 24 1 23 9 14 17 28 29 25 27 15 19 11 20

output:

115090079

result:

ok 1 number(s): "115090079"

Test #20:

score: 0
Accepted
time: 609ms
memory: 428548kb

input:

1000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...

output:

819612372

result:

ok 1 number(s): "819612372"

Test #21:

score: 0
Accepted
time: 0ms
memory: 8264kb

input:

18
9 4 21 5 22 6 9 16 3 14 11 2 0 12 6 3 7 21

output:

0

result:

ok 1 number(s): "0"

Extra Test:

score: 0
Extra Test Passed