답안 #535126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535126 2022-03-09T13:19:34 Z Lobo Tents (JOI18_tents) C++17
48 / 100
2000 ms 55728 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

#define maxn 303

int n, m, dp[maxn][maxn][maxn], pw[50], inv[maxn];
int mod = 1e9+7;

int modinv(int x) {
    int ans = 1;
    pw[0] = x%mod;
    for(int i = 1; i <= 31; i++) {
        pw[i] = pw[i-1]*pw[i-1];
        pw[i]%= mod;
    }
    for(int i = 0; i <= 31; i++) {
        if(((mod-2)&(1LL<<i)) != 0) {
            ans*= pw[i];
            ans%= mod;
        }
    }
    return ans;
}

void solve() {
    cin >> n >> m;

    dp[0][0][0] = 1;

    for(int i = 0; i <= min(n,m); i++) {
        inv[i] = modinv(i);
    }




    int ans = 0;
    for(int q1 = 0; q1 <= min(n,m); q1++) {
        for(int q2 = 0; q2 <= min(n,m); q2++) {
            for(int q3 = 0; q3 <= min(n,m); q3++) {
                int i = n-q1-q2-2*q3;
                int j = m-q1-2*q2-q3;
                if(i < 0 || j < 0) continue;

                // cout << q1 << " " << q2 << " " << q3 << " " << endl;
                // cout << " " << i << " " << j << endl;

                i++,j++;
                if(q1!=0 && q2 == 0 && q3 == 0) dp[q1][q2][q3]+= ((4*dp[q1-1][q2][q3]*i*j)%mod * inv[q1])%mod;
                // if(q1!=0) cout << "  q1 " << 4*dp[q1-1][q2][q3]*i*j * inv[q1] << endl;
                i--,j--;

                i+=1,j+=2;
                if(q2!=0 && q3 == 0) dp[q1][q2][q3]+= (((dp[q1][q2-1][q3]*(i*j*(j-1))/2)%mod) * inv[q2])%mod;
                // if(q2!=0) cout << "  q2 " << dp[q1][q2-1][q3]*(i*j*(j-1))/2 * inv[q2] << endl;
                i-=1,j-=2;

                i+=2,j+=1;
                if(q3!=0) dp[q1][q2][q3]+= ((dp[q1][q2][q3-1]*(j*i*(i-1))/2)%mod * inv[q3])%mod;
                // if(q3!=0) cout << "  q3 " << dp[q1][q2][q3-1]*(j*i*(i-1))/2 * inv[q3] << endl;
                i-=2,j-=1;

                dp[q1][q2][q3]%= mod;
                // cout << "   " << dp[q1][q2][q3] << endl;
                ans+= dp[q1][q2][q3];
                ans%= mod;
            }
        }
    }

    cout << (ans-1+mod)%mod << endl;


}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);

    int tt = 1;
    // cin >> tt;
    while(tt--) solve();

}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 3 ms 5324 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 6 ms 10188 KB Output is correct
8 Correct 2 ms 2508 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 14 ms 12492 KB Output is correct
11 Correct 0 ms 460 KB Output is correct
12 Correct 86 ms 55084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 3 ms 5324 KB Output is correct
6 Correct 5 ms 5196 KB Output is correct
7 Correct 6 ms 10188 KB Output is correct
8 Correct 2 ms 2508 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 14 ms 12492 KB Output is correct
11 Correct 0 ms 460 KB Output is correct
12 Correct 86 ms 55084 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Execution timed out 2082 ms 55728 KB Time limit exceeded
16 Halted 0 ms 0 KB -