답안 #696534

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
696534 2023-02-06T17:55:38 Z lucri 유적 3 (JOI20_ruins3) C++17
58 / 100
3297 ms 524288 KB
#include <bits/stdc++.h>
#define MOD  1000000007
using namespace std;
unordered_map<int,int>ma;
long long putere(long long a,long long b)
{
    if(b==0)
        return 1;
    long long p=putere(a,b/2);
    p*=p;
    p%=MOD;
    if(b%2)
    {
        p*=a;
        p%=MOD;
    }
    return p;
}
int fact[610],comb[610][610];
void factorial()
{
    fact[0]=1;
    for(long long i=1;i<=600;++i)
        fact[i]=fact[i-1]*i%MOD;
}
long long combinari(long long n,long long m)
{
    long long ans=1;
    ans=fact[n];
    ans*=putere(fact[m],MOD-2);
    ans%=MOD;
    ans*=putere(fact[n-m],MOD-2);
    ans%=MOD;
    return ans;
}
short n,v[610];
int cod(short step,short nrstack,short ppoz,bool exista)
{
    return ((1LL*step*601+nrstack)*600+ppoz-1)*2+exista-1;
}
long long raspunde(short step,short nrstack,short ppoz,bool exista)
{
    if(ma.find(cod(step,nrstack,ppoz,exista))!=ma.end())
        return ma[cod(step,nrstack,ppoz,exista)];
    if(step==n)
    {
        ma[cod(step,nrstack,ppoz,exista)]=1;
        return 1;
    }
    int ans=0;
    short ramas=n-step-nrstack;
    short posibileInceput=v[ppoz]-1-(2*step-step+ppoz-1-nrstack);
    if(exista==false)
    {
        if(ramas>=3)
            ans=(1LL*ans+1LL*comb[ramas-1][2]*raspunde(step+1,nrstack+1,ppoz,false))%MOD;
        if(ramas>=2)
            ans=(1LL*ans+1LL*(ramas-1)*raspunde(step+1,nrstack+1,ppoz,true))%MOD;
        if(posibileInceput>=2&&nrstack>=1)
            ans=(1LL*ans+1LL*comb[posibileInceput][2]*raspunde(step+1,nrstack-1,ppoz,false))%MOD;
        if(posibileInceput&&nrstack>=1&&ramas>=1)
        {
            if(ramas-1)
                ans=(1LL*ans+1LL*posibileInceput*(ramas-1)%MOD*raspunde(step+1,nrstack,ppoz,false))%MOD;
            ans=(1LL*ans+1LL*posibileInceput*raspunde(step+1,nrstack,ppoz,true))%MOD;
        }
        else if(posibileInceput&&ramas>=1)
        {
            if(step==n-1)
            {
                ma[cod(step,nrstack,ppoz,exista)]=1;
                return 1;
            }
            if(ramas-1)
                ans=(1LL*ans+1LL*posibileInceput*(ramas-1)%MOD*raspunde(step+1,nrstack,ppoz,false))%MOD;
            int q=putere(comb[n-ppoz][step+1-ppoz],MOD-2);
            for(int newppoz=ppoz+1;newppoz<=step+2;++newppoz)
                ans=(1LL*ans+1LL*comb[n-newppoz][step+2-newppoz]*q%MOD*posibileInceput%MOD*raspunde(step+1,nrstack,newppoz,false))%MOD;
        }
    }
    else
    {
        if(ramas>=2)
            ans=(1LL*ans+1LL*comb[ramas][2]*raspunde(step+1,nrstack+1,ppoz,true))%MOD;
        if(posibileInceput>=2)
        {
            if(nrstack>=2)
                ans=(1LL*ans+1LL*comb[posibileInceput][2]*raspunde(step+1,nrstack-1,ppoz,true))%MOD;
            else
            {
                if(step==n-1)
                {
                    ma[cod(step,nrstack,ppoz,exista)]=1;
                    return 1;
                }
                int q=putere(comb[n-ppoz][step+1-ppoz],MOD-2);
                for(int newppoz=ppoz+1;newppoz<=step+2;++newppoz)
                    ans=(1LL*ans+1LL*comb[n-newppoz][step+2-newppoz]*q%MOD*comb[posibileInceput][2]%MOD*raspunde(step+1,nrstack-1,newppoz,false))%MOD;
            }
        }
        if(posibileInceput&&ramas>=1)
            ans=(1LL*ans+1LL*posibileInceput*ramas%MOD*raspunde(step+1,nrstack,ppoz,true))%MOD;
    }
    ma[cod(step,nrstack,ppoz,exista)]=ans;
    return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    factorial();
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i];
    for(int i=0;i<=n;++i)
        for(int j=0;j<=i;++j)
            comb[i][j]=combinari(i,j);
    cout<<raspunde(0,0,1,false);
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 1108 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 4 ms 1108 KB Output is correct
14 Correct 3 ms 980 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 9 ms 1968 KB Output is correct
19 Correct 12 ms 1836 KB Output is correct
20 Correct 8 ms 1960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 4 ms 1108 KB Output is correct
12 Correct 3 ms 980 KB Output is correct
13 Correct 4 ms 1108 KB Output is correct
14 Correct 3 ms 980 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 2 ms 596 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 9 ms 1968 KB Output is correct
19 Correct 12 ms 1836 KB Output is correct
20 Correct 8 ms 1960 KB Output is correct
21 Correct 714 ms 163576 KB Output is correct
22 Correct 541 ms 113080 KB Output is correct
23 Correct 550 ms 112908 KB Output is correct
24 Correct 1090 ms 226640 KB Output is correct
25 Correct 75 ms 1748 KB Output is correct
26 Correct 77 ms 1780 KB Output is correct
27 Correct 78 ms 1704 KB Output is correct
28 Correct 124 ms 21144 KB Output is correct
29 Correct 74 ms 1812 KB Output is correct
30 Runtime error 3297 ms 524288 KB Execution killed with signal 9
31 Halted 0 ms 0 KB -