QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#328509#8047. DFS Order 4QingyuAC ✓853ms8768kbC++232.4kb2024-02-15 20:36:492024-02-15 20:36:50

Judging History

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

  • [2024-02-15 20:36:50]
  • 评测
  • 测评结果:AC
  • 用时:853ms
  • 内存:8768kb
  • [2024-02-15 20:36:49]
  • 提交

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-j;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-j;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;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

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

input:

4 114514199

output:

2

result:

ok 1 number(s): "2"

Test #2:

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

input:

10 998244353

output:

11033

result:

ok 1 number(s): "11033"

Test #3:

score: 0
Accepted
time: 2ms
memory: 4172kb

input:

100 1000000007

output:

270904395

result:

ok 1 number(s): "270904395"

Test #4:

score: 0
Accepted
time: 707ms
memory: 8424kb

input:

756 1001338769

output:

901942543

result:

ok 1 number(s): "901942543"

Test #5:

score: 0
Accepted
time: 831ms
memory: 8468kb

input:

793 1009036033

output:

301770320

result:

ok 1 number(s): "301770320"

Test #6:

score: 0
Accepted
time: 725ms
memory: 8252kb

input:

759 1005587659

output:

846376219

result:

ok 1 number(s): "846376219"

Test #7:

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

input:

773 1007855479

output:

1398019

result:

ok 1 number(s): "1398019"

Test #8:

score: 0
Accepted
time: 692ms
memory: 8136kb

input:

751 1006730639

output:

321287237

result:

ok 1 number(s): "321287237"

Test #9:

score: 0
Accepted
time: 781ms
memory: 8344kb

input:

778 1007760653

output:

430322899

result:

ok 1 number(s): "430322899"

Test #10:

score: 0
Accepted
time: 853ms
memory: 8440kb

input:

798 1007543827

output:

688720826

result:

ok 1 number(s): "688720826"

Test #11:

score: 0
Accepted
time: 846ms
memory: 8468kb

input:

796 1004841413

output:

258829347

result:

ok 1 number(s): "258829347"

Test #12:

score: 0
Accepted
time: 775ms
memory: 8280kb

input:

775 1005185189

output:

744278608

result:

ok 1 number(s): "744278608"

Test #13:

score: 0
Accepted
time: 829ms
memory: 8768kb

input:

800 1006012831

output:

508549367

result:

ok 1 number(s): "508549367"

Test #14:

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

input:

1 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #15:

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

input:

2 1001338769

output:

1

result:

ok 1 number(s): "1"

Test #16:

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

input:

9 1009036033

output:

1780

result:

ok 1 number(s): "1780"

Test #17:

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

input:

14 1001338769

output:

43297358

result:

ok 1 number(s): "43297358"

Extra Test:

score: 0
Extra Test Passed