Submission #1004965

# Submission time Handle Problem Language Result Execution time Memory
1004965 2024-06-22T05:12:04 Z whatthemomooofun1729 Gondola (IOI14_gondola) C++17
Compilation error
0 ms 0 KB
#include "gondola.h"
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <sstream>
#include <chrono>
#include <random>

#define ff first
#define ss second
#define PB push_back
#define sz(x) int(x.size())
#define rsz resize
#define fch(xxx, yyy) for (auto xxx : yyy) // abusive notation
#define all(x) (x).begin(),(x).end()
#define eps 1e-9

// more abusive notation (use at your own risk):
// #define int ll

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;

// debugging
void __print(int x) {std::cerr << x;}
void __print(ll x) {std::cerr << x;} /* remember to uncomment this when not using THE MACRO */
void __print(unsigned x) {std::cerr << x;}
void __print(ull x) {std::cerr << x;}
void __print(float x) {std::cerr << x;}
void __print(double x) {std::cerr << x;}
void __print(ld x) {std::cerr << x;}
void __print(char x) {std::cerr << '\'' << x << '\'';}
void __print(const char *x) {std::cerr << '\"' << x << '\"';}
void __print(const string& x) {std::cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void __print(const pair<T, V> &x) {std::cerr << '{'; __print(x.ff); std::cerr << ", "; __print(x.ss); std::cerr << '}';}
template<typename T> void __print(const T& x) {int f = 0; std::cerr << '{'; for (auto &i: x) std::cerr << (f++ ? ", " : ""), __print(i); std::cerr << "}";}
void _print() {std::cerr << "]\n";}
template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) std::cerr << ", "; _print(v...);}
void println() {std::cerr << ">--------------------<" << endl;}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

// templates
template <class T> bool ckmin(T &a, const T &b) {return b<a ? a = b, 1 : 0;}
template <class T> bool ckmax(T &a, const T &b) {return b>a ? a = b, 1 : 0;}
template <class T> using gr = greater<T>;
template <class T> using vc = vector<T>;
template <class T> using p_q = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vc<T>, gr<T>>;
template <class T1, class T2> using pr = pair<T1, T2>;
mt19937_64 rng_ll(chrono::steady_clock::now().time_since_epoch().count());
int rng(int M) {return (int)(rng_ll()%M);} /*returns any random number in [0, M) */

// const variables
constexpr int INF = (int)2e9;
constexpr int MOD = 1e9 + 9;
constexpr ll LL_INF = (ll)3e18;
constexpr int mod = (int)1e9 + 7;
constexpr ll inverse = 500000004LL; // inverse of 2 modulo 1e9 + 7

void setIO(const string& str) {// fast input/output
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (str.empty()) return;
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
}

namespace math {
    

    // Perfect Squares
    ll root(ll num) { // for any long longs, this will give precise answer in O(Log(N))
        auto k = (ll)sqrtl((long double)num);
        while (k * k < num) ++k;
        while (k * k > num) --k;
        return k;
    }

    // Binary Exponentiation, with or without modulo
    ll binpow(ll a, ll b) {
        ll res = 1;
        while (b > 0) {
            if (b & 1) res *= a;
            a *= a;
            b >>= 1;
        }
        return res;
    }
    ll binpow(ll a, ll b, ll m) {
        a%=m;
        ll res = 1;
        while (b > 0) {
            if (b & 1) res = (res * a)%m;
            a = (a * a)%m;
            b >>= 1;
        }
        return res;
    }

    // Catalan Numbers, supports N <= 5e6/2 (set bounds as you like)
    ll ctln(int N, int mod) { // CALL PRECALC FIRST!!!!!!!
        ll ans = choose(2 * N, N, mod);
        ans = (ans * nt::inv(N+1, mod))%mod;
        return ans;
    }
}

void mult(ll& x, ll y) {
    y %= MOD;
    x = (x * y)%MOD;
}

int valid(int n, int inputSeq[]) {
    int N = n;
    vi v(N);
    int n1 = 0, mn = N;
    set<int> st;
    for (int i = 0; i < N; ++i) {
        v[i] = inputSeq[i] - 1;
        st.insert(v[i]);
        if (v[i] <= N - 1) {
            n1++;
            ckmin(mn, v[i]);
        }
    }
    if (sz(st) < N) return 0;
    if (n1 == 0) return 1;
    int idx = -1;
    for (int i = 0; i < N; ++i) {
        if (v[i] == mn) idx = i;
    }
    assert(idx != -1);
    idx -= v[idx];
    if (idx < 0) idx += N;
    rotate(v.begin(), v.begin() + idx, v.end());
    for (int i = 0; i < N; ++i) {
        if (v[i] <= N - 1 && i != v[i]) {
            return 0;
        }
    }
    return 1;
}

int replacement(int n, int gondolaSeq[], int replacementSeq[]) {
    int N = n;
    vi v(N), order;
    int n1 = 0, mn = N;
    for (int i = 0; i < N; ++i) {
        v[i] = gondolaSeq[i] - 1;
        if (v[i] <= N - 1) {
            n1++;
            ckmin(mn, v[i]);
        }
    }
    vc<pii> pairs;
    if (n1 == 0) {
        for (int i = 0; i < N; ++i) {
            pairs.PB({v[i], i});
        }
    } else {
        int idx = -1;
        for (int i = 0; i < N; ++i) {
            if (v[i] == mn) idx = i;
        }
        assert(idx != -1);
        idx -= v[idx];
        if (idx < 0) idx += N;
        rotate(v.begin(), v.begin() + idx, v.end());
        for (int i = 0; i < N; ++i) {
            if (v[i] >= N) {
                pairs.PB({v[i], i});
            }
        }
    }
    sort(all(pairs));
    int cur_replace = N - 1;
    for (int i = 0; i < sz(pairs); ++i) {
        order.PB(pairs[i].ss);
        cur_replace++;
        while (cur_replace < pairs[i].ff) {
            order.PB(cur_replace);
            cur_replace++;
        }
    }
    for (int i = 0; i < sz(order); ++i) {
        replacementSeq[i] = order[i] + 1;
    }
    return sz(order);
}

int countReplacement(int n, int inputSeq[]) {
    if (!valid(n, inputSeq)) {
        return 0;
    }
    int N = n;
    vi v(N);
    int n1 = 0, mn = N;
    for (int i = 0; i < N; ++i) {
        v[i] = inputSeq[i] - 1;
        if (v[i] <= N - 1) {
            n1++;
            ckmin(mn, v[i]);
        }
    }
    vc<pii> pairs;
    if (n1 == 0) {
        for (int i = 0; i < N; ++i) {
            pairs.PB({v[i], i});
        }
    } else {
        int idx = -1;
        for (int i = 0; i < N; ++i) {
            if (v[i] == mn) idx = i;
        }
        assert(idx != -1);
        idx -= v[idx];
        if (idx < 0) idx += N;
        rotate(v.begin(), v.begin() + idx, v.end());
        for (int i = 0; i < N; ++i) {
            if (v[i] >= N) {
                pairs.PB({v[i], i});
            }
        }
    }
    sort(all(pairs));
    if (pairs.empty()) {
        return 1;
    }
    vc<pii> sections;
    int consec = 1, small = pairs[0].ff;
    for (int i = 1; i < sz(pairs); ++i) {
        if (pairs[i].ff > pairs[i-1].ff + 1) {
            sections.PB({consec, small});
            consec = 1;
            small = pairs[i].ff;
        } else {
            consec++;
        }
    }
    if (consec >= 1) sections.PB({consec, small});
    int cur = N;
    ll prod = 1;
    vc<pii> exponen;
    for (int i = 0; i < sz(sections); ++i) {
        int dist = sections[i].ss - cur;
        cur = sections[i].ss + sections[i].ff;
        if (dist == 0) continue;
        exponen.PB({dist, sections[i].ff});
    }
    for (int i = sz(exponen)-2; i >= 0; --i) {
        exponen[i].ss += exponen[i+1].ss;
    }
    for (int i = 0; i < sz(exponen); ++i) {
        ll term = math::binpow(exponen[i].ss, exponen[i].ff, MOD);
        mult(prod, term);
    }
    if (n1 == 0) mult(prod, N);
    return prod;
}

// TLE -> TRY NOT USING DEFINE INT LONG LONG
// CE -> CHECK LINE 45
// 5000 * 5000 size matrices are kinda big (potential mle)
// Do something, start simpler

Compilation message

gondola.cpp: In function 'll math::ctln(int, int)':
gondola.cpp:127:18: error: 'choose' was not declared in this scope
  127 |         ll ans = choose(2 * N, N, mod);
      |                  ^~~~~~
gondola.cpp:128:22: error: 'nt' has not been declared
  128 |         ans = (ans * nt::inv(N+1, mod))%mod;
      |                      ^~
gondola.cpp: In function 'void setIO(const string&)':
gondola.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gondola.cpp:90:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~