QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#831598#8905. Ультра mexthomaswmy15 1574ms41924kbC++204.3kb2024-12-25 15:37:342024-12-25 15:37:36

Judging History

This is the latest submission verdict.

  • [2024-12-25 15:37:36]
  • Judged
  • Verdict: 15
  • Time: 1574ms
  • Memory: 41924kb
  • [2024-12-25 15:37:34]
  • Submitted

answer

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

namespace PolyNTT{
    int PMod=998244353,PG=3;
    typedef long long ll;
    
    int qpow(int x,ll y) {
        int z=1;
        for(;y;y>>=1) {
            if(y&1) z=1ll*z*x%PMod;
            x=1ll*x*x%PMod;
        }
        return z;
    }
    
    void NTT(vector<int> &a,int len,int op) {
        static vector<int> rev,W;
        rev.resize(len),W.resize(len);
        for(int i=1;i<len;i++) rev[i]=(rev[i>>1]>>1)|(i&1? len>>1:0);
        for(int i=1;i<len;i++) if(i<rev[i]) swap(a[i],a[rev[i]]);
        for(int h=2,p=1;h<=len;p=h,h<<=1) {
            W[0]=1,W[1]=qpow(PG,(PMod-1)/h);
            if(op==-1) W[1]=qpow(W[1],h-1);
            for(int i=2;i<p;i++) W[i]=1ll*W[i-1]*W[1]%PMod;
            for(int i=0;i<len;i+=h) {
                for(int j=i;j<i+p;j++) {
                    int u=a[j],v=1ll*W[j-i]*a[j+p]%PMod;
                    a[j]=(u+v)%PMod,a[j+p]=(u+PMod-v)%PMod;
                }
            }
        }
        if(op==-1) {
            int inv=qpow(len,PMod-2);
            for(int i=0;i<len;i++) a[i]=1ll*a[i]*inv%PMod;
        }
    }
    
    typedef vector<int> Poly;
    
    Poly operator+(Poly x,Poly y) {
        x.resize(max(x.size(),y.size()));
        for(int i=0;i<y.size();i++) x[i]=(x[i]+y[i])%PMod;
        return x;
    }
    
    Poly operator-(Poly x,Poly y) {
        x.resize(max(x.size(),y.size()));
        for(int i=0;i<y.size();i++) x[i]=(x[i]+PMod-y[i])%PMod;
        return x;
    }
    
    Poly operator*(int x,Poly y) {
        for(int i=0;i<y.size();i++) y[i]=1ll*y[i]*x%PMod;
        return y;
    }
    
    Poly operator*(Poly x,int y) {
        for(int i=0;i<x.size();i++) x[i]=1ll*x[i]*y%PMod;
        return x;
    }
    
    Poly operator*(Poly x,Poly y) {
        int l=1,ll=x.size()+y.size()-1;
        while(l<ll) l<<=1;
        x.resize(l),y.resize(l);
        NTT(x,l,1),NTT(y,l,1);
        for(int i=0;i<l;i++) x[i]=1ll*x[i]*y[i]%PMod;
        NTT(x,l,-1);
        x.resize(ll);
        return x;
    }

    Poly Shift(Poly x,int s) {
        if(x.size()+s<=0) return {};
        if(!s) return x;
        if(s>0) {
            x.resize(x.size()+s);
            for(int i=x.size()-1;i>=0;i--) x[i]=i<s? 0:x[i-s];
        }
        else {
            for(int i=0;i<x.size();i++) x[i]=i-s<x.size()? x[i-s]:0;
            x.resize(x.size()+s);
        }
        return x;
    }
}using namespace PolyNTT;

const int N=20;
const int M=1<<17|10;

int T,Mod;
int lg[M];
Poly f[N][N],g[N][N];
int fac[M],inv[M];

int lowbit(int x) {
    return x&-x;
}

int C(int x,int y) {
    if(x<y || y<0) return 0;
    return 1ll*fac[x]*inv[y]%Mod*inv[x-y]%Mod;
}

bool chkg(int g) {
    int x=Mod-1;
    for(int i=2;i*i<=x;i++) {
        if(x%i) continue;
        if(qpow(g,(Mod-1)/i)==1) return 0;
        while(x%i==0) x/=i;
    }
    if(x>1) {
        if(qpow(g,(Mod-1)/x)==1) return 0;
    }
    return 1;
}

int main() {
    scanf("%d%d",&Mod,&T);
    PMod=Mod;
    PG=2;
    while(!chkg(PG)) PG++;
    int n=M-10;
    fac[0]=1;
    for(int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%Mod;
    inv[n]=qpow(fac[n],Mod-2);
    for(int i=n-1;i>=0;i--) inv[i]=1ll*inv[i+1]*(i+1)%Mod;
    for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1;
    f[1][0]=g[1][0]={0,1};
    for(int i=2;i<=17;i++) {
        for(int j=0;j<i;j++) f[i][j].resize(1<<i),g[i][j].resize(1<<i);
        Poly p;
        p.resize((1<<i-1)+1);
        for(int j=0;j<=1<<i-1;j++) p[j]=C(1<<i-1,j);
        for(int j=0;j<i-1;j++) f[i][j]=f[i][j]+f[i-1][j]*p;
        for(int j=0;j<=1<<i-1;j++) p[j]=C((1<<i-1)-1,j);
        for(int j=0;j<i-1;j++) g[i][j]=g[i][j]+g[i-1][j]*p;
        for(int j=0;j<i;j++) f[i][j]=f[i][j]+Shift(f[i-1][j],1<<i-1),g[i][j]=g[i][j]+Shift(g[i-1][j],1<<i-1);
        for(int j=0;j<(1<<i-1)-1;j++) f[i][i-1][j+(1<<i-1)]=(f[i][i-1][j+(1<<i-1)]+C((1<<i-1)-2,j))%Mod,g[i][i-1][j+(1<<i-1)]=(g[i][i-1][j+(1<<i-1)]+C((1<<i-1)-2,j))%Mod;
        for(int j=0;j<i;j++) f[i][j]=f[i][j]+Shift(g[i-1][j],1<<i-1);
    }
    while(T--) {
        int k,n,m;
        scanf("%d%d%d",&k,&n,&m);
        if(lowbit(m)!=m) printf("0\n");
        else if(n==(1<<k)) printf("%d\n",m==(1<<k));
        else printf("%d\n",n<f[k][lg[m]].size()? f[k][lg[m]][n]:0);
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 3
Accepted

Test #1:

score: 3
Accepted
time: 1565ms
memory: 41608kb

input:

118751233
10
1 2 2
1 2 1
1 2 2
1 2 2
1 2 2
1 1 1
1 1 2
1 1 1
1 1 1
1 1 2

output:

1
0
1
1
1
1
0
1
1
0

result:

ok 10 numbers

Test #2:

score: 3
Accepted
time: 1550ms
memory: 41612kb

input:

64749569
10
1 1 1
1 1 2
1 1 2
1 2 2
1 1 2
1 2 1
1 1 2
1 2 1
1 2 1
1 1 2

output:

1
0
0
1
0
0
0
0
0
0

result:

ok 10 numbers

Test #3:

score: 3
Accepted
time: 1561ms
memory: 41708kb

input:

5767169
10
1 2 1
1 1 1
1 2 2
1 1 1
1 1 2
1 1 1
1 1 1
1 1 2
1 2 1
1 1 1

output:

0
1
1
1
0
1
1
0
0
1

result:

ok 10 numbers

Subtask #2:

score: 5
Accepted

Dependency #1:

100%
Accepted

Test #4:

score: 5
Accepted
time: 1566ms
memory: 41724kb

input:

120586241
10
2 4 1
2 4 1
2 4 4
2 1 2
2 1 3
2 4 1
2 4 4
2 1 1
2 1 1
2 2 2

output:

0
0
1
0
0
0
1
1
1
1

result:

ok 10 numbers

Test #5:

score: 5
Accepted
time: 1574ms
memory: 41628kb

input:

434896897
10
1 2 2
2 2 1
2 2 2
2 2 1
1 2 2
2 2 2
2 1 1
2 3 1
1 1 1
2 4 4

output:

1
2
1
2
1
1
1
3
1
1

result:

ok 10 numbers

Test #6:

score: 5
Accepted
time: 1555ms
memory: 41924kb

input:

103284737
10
2 4 4
2 3 1
2 3 1
2 1 1
2 2 2
2 4 4
2 2 2
2 1 1
2 2 1
2 2 1

output:

1
3
3
1
1
1
1
1
2
2

result:

ok 10 numbers

Test #7:

score: 5
Accepted
time: 1553ms
memory: 41612kb

input:

120586241
10
2 3 1
2 1 1
2 4 4
2 1 1
2 2 1
2 4 4
2 3 1
2 2 2
2 1 1
2 2 2

output:

3
1
1
1
2
1
3
1
1
1

result:

ok 10 numbers

Test #8:

score: 5
Accepted
time: 1548ms
memory: 41860kb

input:

478937089
10
2 2 1
2 2 1
2 2 1
2 4 1
2 3 1
2 4 1
2 4 1
2 3 1
2 2 1
2 3 1

output:

2
2
2
0
3
0
0
3
2
3

result:

ok 10 numbers

Test #9:

score: 5
Accepted
time: 1554ms
memory: 41720kb

input:

23068673
10
2 1 1
2 1 1
2 2 1
2 4 1
2 3 1
2 2 1
2 3 1
2 4 1
2 1 1
2 2 1

output:

1
1
2
0
3
2
3
0
1
2

result:

ok 10 numbers

Test #10:

score: 5
Accepted
time: 1563ms
memory: 41716kb

input:

786432001
10
1 1 2
1 1 2
2 1 4
2 1 1
2 3 1
1 2 1
2 4 2
1 2 2
2 1 1
1 1 2

output:

0
0
0
1
3
0
0
1
1
0

result:

ok 10 numbers

Subtask #3:

score: 7
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #11:

score: 7
Accepted
time: 1526ms
memory: 41840kb

input:

244842497
10
3 4 1
3 4 3
3 3 4
3 6 4
3 2 6
3 2 1
3 4 4
3 3 6
3 3 8
3 8 5

output:

28
0
0
1
0
6
1
0
0
0

result:

ok 10 numbers

Test #12:

score: 7
Accepted
time: 1562ms
memory: 41856kb

input:

288882689
10
3 4 2
3 2 2
3 6 2
3 3 1
1 1 1
3 8 8
3 1 1
2 2 1
2 3 1
3 2 1

output:

6
1
3
17
1
1
1
2
3
6

result:

ok 10 numbers

Test #13:

score: 7
Accepted
time: 1562ms
memory: 41840kb

input:

483655681
10
3 4 1
3 3 1
3 6 1
3 6 2
3 2 1
3 5 2
3 6 4
3 4 2
3 5 1
3 3 2

output:

28
17
17
3
6
4
1
6
29
4

result:

ok 10 numbers

Test #14:

score: 7
Accepted
time: 1551ms
memory: 41728kb

input:

244842497
10
3 3 1
3 4 1
3 1 1
3 6 2
3 5 1
3 7 1
3 2 1
3 4 2
3 6 1
3 5 2

output:

17
28
1
3
29
7
6
6
17
4

result:

ok 10 numbers

Test #15:

score: 7
Accepted
time: 1569ms
memory: 41684kb

input:

404226049
10
3 4 1
3 8 1
3 6 1
3 7 1
3 5 1
3 8 1
3 5 1
3 4 1
3 8 1
3 7 1

output:

28
0
17
7
29
0
29
28
0
7

result:

ok 10 numbers

Test #16:

score: 7
Accepted
time: 1560ms
memory: 41728kb

input:

935329793
10
3 2 1
3 3 1
3 3 1
3 1 1
3 2 1
3 2 1
3 7 1
3 2 1
3 7 1
3 7 1

output:

6
17
17
1
6
6
7
6
7
7

result:

ok 10 numbers

Test #17:

score: 7
Accepted
time: 1551ms
memory: 41724kb

input:

23068673
10
1 2 2
3 8 1
1 2 2
2 2 2
3 7 4
1 2 1
1 1 2
3 6 2
2 4 2
1 2 2

output:

1
0
1
1
0
0
0
3
0
1

result:

ok 10 numbers

Subtask #4:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #18:

score: 8
Accepted
time: 1561ms
memory: 41708kb

input:

263454721
10
4 5 2
4 11 12
4 6 8
4 12 6
4 14 6
4 12 12
4 8 12
4 13 10
4 2 14
4 5 4

output:

220
0
0
0
0
0
0
0
0
10

result:

ok 10 numbers

Test #19:

score: 8
Accepted
time: 1554ms
memory: 41608kb

input:

302252033
10
4 6 4
4 16 16
4 8 1
3 6 4
4 9 4
4 12 8
4 8 4
4 13 2
4 13 4
2 1 1

output:

45
1
5244
1
252
15
210
33
14
1

result:

ok 10 numbers

Test #20:

score: 0
Wrong Answer
time: 1553ms
memory: 41664kb

input:

983826433
10
4 13 1
4 16 16
4 8 8
4 12 8
4 10 2
4 12 2
4 15 1
4 13 2
4 10 8
4 2 2

output:

395
1
1
15
637
131
15
33
15
1

result:

wrong answer 1st numbers differ - expected: '402', found: '395'

Subtask #5:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #9:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #10:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #11:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #12:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #13:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #14:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #15:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #16:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #17:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #18:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #19:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #20:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #21:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #22:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #23:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #24:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #25:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #26:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #27:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #28:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #29:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%

Subtask #30:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

0%