Submission #1296969

#TimeUsernameProblemLanguageResultExecution timeMemory
1296969Bui_Quoc_CuongAsceticism (JOI18_asceticism)C++20
49 / 100
107 ms9752 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

template <class A, class B>
    bool maximize(A &a, const B b) {
        if (a < b) {
            a = b;
            return true;
        } return false;
    }

template <class A, class B>
    bool minimize(A &a, const B b) {
        if(a > b) {
            a = b;
            return true;
        } return false;
    }

using pII = pair <int, int>;
using vI = vector <int>;
using vL = vector <long long>;
using pLI = pair <long long, int>;

#define BIT(mask, i) ((mask >> (i)) & 1)
#define MASK(a) (1LL<<(a))
#define FOR(i, a, b) for(int i = a; i <= (int)b; i++)
#define FORD(i, a, b) for(int i = a; i >= (int)b; i--)
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()

const int mod = 1e9 + 7;
const int maxn = 2e5 + 5;

void add (int &x, const int y) {
    x+= y;
    if (x >= mod) x-= mod;
}

int n, k;

int fact[maxn], inv_fact[maxn];

int iPow (int x, int y) {
    if (y == 0) return 1;
    if (y == 1) return x;
    int c = iPow (x, y / 2);
    if (y & 1) return 1LL * c * c % mod * x % mod;
    else return 1LL * c * c % mod;
}

int C (int n, int k) {
    if (k > n) return 0;
    return 1LL * fact[n] * inv_fact[k] % mod * inv_fact[n - k] % mod;
}

namespace sub1 {
    int a[15];

    void solve () {
        FOR(i, 1, n) a[i] = i;
        int ans = 0;

        do {
            int cnt = 0;
            vector <int> dd(n + 2, 0);
            FOR(i, 1, n) {
                if (dd[a[i]] == 0) {
                    cnt++;
                    int last = - 1;
                    FOR(j, i, n) if (!dd[a[j]] && a[j] >= last) {
                        dd[a[j]] = 1;
                        last = a[j];
                    } else break;
                }
            }
            if (cnt == k) ans++;
        } while (next_permutation(a + 1, a + 1 + n));

        cout << ans;
    }
}

namespace sub23 {
    int dp[3005][3005];

    void solve () {
        dp[1][1] = 1;
        FOR(i, 1, n) FOR(j, 1, k) {
            /// create new group
            if (j < k) {
                add(dp[i + 1][j + 1], dp[i][j]);
                add(dp[i + 1][j + 1], 1LL * dp[i][j] * (i - j) % mod);
            }
            /// join group
            add (dp[i + 1][j], 1LL * dp[i][j] * j % mod);
        }
        cout << dp[n][k];
    }
}

void process () {
    cin >> n >> k;
    if (n <= 10) {
        return sub1::solve();
    }
    if (n <= 3000) {
        return sub23::solve();
    }
}

int main () {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    #define taskname "kieuoanh"
    if (fopen(taskname".inp", "r")) {
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }

    fact[0] = 1;
    FOR(i, 1, maxn - 5) fact[i] = 1LL * fact[i - 1] * i % mod;
    inv_fact[maxn - 5] = iPow(fact[maxn - 5], mod - 2);
    FORD(i, maxn - 6, 0) inv_fact[i] = 1LL * inv_fact[i + 1] * (i + 1) % mod;

    int tc = 1; /// cin >> tc;
    while (tc--) process();

    return 0;
}

Compilation message (stderr)

asceticism.cpp: In function 'int main()':
asceticism.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
asceticism.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...