QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328507#8047. DFS Order 4QingyuTL 1851ms8696kbC++232.4kb2024-02-15 20:36:242024-02-15 20:36:25

Judging History

你现在查看的是最新测评结果

  • [2024-02-15 20:36:25]
  • 评测
  • 测评结果:TL
  • 用时:1851ms
  • 内存:8696kb
  • [2024-02-15 20:36:24]
  • 提交

answer

#include<bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

template<typename T> bool chkmax(T &x, T y){return x<y?x=y,1:0;}
template<typename T> bool chkmin(T &x, T y){return x>y?x=y,1:0;}

int readint(){
    int x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int n,cys;
ll fac[805],inv[805],d[805][805],f[805],ds[805];

typedef unsigned long long ull;
ull V;

void init() {
    V = ((__int128)1<<64)/cys;
}
ll mod(ll x){return x>=cys?x-cys:x;}
ll reduce(ll x){
    // return x % cys;
    ull t = ((unsigned __int128) x * V) >> 64;
    x -= t * cys;
    return mod(x);
}

ll qpow(ll x,ll p){
    ll ret=1;
    for(;p;p>>=1,x=x*x%cys) if(p&1) ret=ret*x%cys;
    return ret;
}


int main(){
    n=readint(); cys=readint();
    init();
    fac[0]=inv[0]=1;
    for(int i=1;i<=n;i++) fac[i]=fac[i-1]*i%cys;
    inv[n]=qpow(fac[n],cys-2);
    for(int i=n-1;i>=1;i--) inv[i]=inv[i+1]*(i+1)%cys;
    for(int i=1;i<=n;i++) ds[i]=fac[i-1]*inv[i]%cys;
    f[1]=1;
    d[1][0]=1;
    for(int i=1;i<=n;i++){
        if(i>1){
            f[i]=d[i-1][0];
            d[i][0]=reduce(d[i][0]+f[i]*ds[i]);
        }
        for(int j=0;j<=i;j++){
            // if(!d[i][j]) continue;
            for(int k=2;k<=i;k++)
                d[i][j]=reduce(d[i][j]+reduce(d[i-k][j]*f[k])*ds[i]);
            // for(int k=2;k<=n-i;k++){
            //     d[i+k][j]=reduce(d[i+k][j]+reduce(d[i][j]*f[k])*ds[i+k]);
            // }
            for(int k=1;k<=i;k++){
                d[i][j+1]=mod(d[i][j+1]+cys-reduce(reduce(d[i-k][j]*f[k])*ds[i]));
                // cout<<"??? "<<i<<' '<<j<<' '<<k<<' '<<d[i-k][j]*f[k]%cys*ds[i]%cys<<endl;
            }
            // for(int k=1;k<=n-i;k++){
            //     d[i+k][j+1]=mod(d[i+k][j+1]+cys-reduce(reduce(d[i][j]*f[k])*ds[i+k]));
            // }
            if(j) d[i][j-1]=reduce(d[i][j-1]+d[i-1][j]*ds[i]);
        }
    }
    // for(int i=1;i<=n;i++) cout<<f[i]%cys<<' ';
    // cout<<endl;
    // for(int i=1;i<=n;i++){
    //     for(int j=0;j<=n;j++) cout<<d[i][j]*fac[i]%cys<<' ';
    //     cout<<endl;
    // }
    printf("%lld\n",f[n]*fac[n-1]%cys);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 4068kb

input:

4 114514199

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

10 998244353

output:

11033

result:

ok 1 number(s): "11033"

Test #3:

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

input:

100 1000000007

output:

270904395

result:

ok 1 number(s): "270904395"

Test #4:

score: 0
Accepted
time: 1722ms
memory: 8464kb

input:

756 1001338769

output:

901942543

result:

ok 1 number(s): "901942543"

Test #5:

score: 0
Accepted
time: 1851ms
memory: 8696kb

input:

793 1009036033

output:

301770320

result:

ok 1 number(s): "301770320"

Test #6:

score: 0
Accepted
time: 1521ms
memory: 8276kb

input:

759 1005587659

output:

846376219

result:

ok 1 number(s): "846376219"

Test #7:

score: -100
Time Limit Exceeded

input:

773 1007855479

output:

1398019

result: