Submission #730234

#TimeUsernameProblemLanguageResultExecution timeMemory
730234becaidoGondola (IOI14_gondola)C++17
100 / 100
33 ms6204 KiB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifndef WAIMAI
#include "gondola.h"
#endif

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int MAX = 2.5e5 + 5;
bitset<MAX> is;
int valid(int n, int a[]) {
    int shift = -1;
    FOR (i, 0, n - 1) {
        if (a[i] <= n) {
            if (shift == -1) shift = (a[i] - 1 - i + n) % n;
            else if ((a[i] - 1 - i + n) % n != shift) return 0;
        }
        if (is[a[i]]) return 0;
        is[a[i]] = 1;
    }
    return 1;
}

int p[MAX];
int replacement(int n, int a[], int b[]) {
    int mx = *max_element(a, a + n);
    fill(p + 1, p + mx + 1, -1);
    int shift = -1;
    FOR (i, 0, n - 1) {
        p[a[i]] = i;
        if (a[i] <= n && shift == -1) shift = (a[i] - 1 - i + n) % n;
    }
    if (shift == -1) shift = 0;
    FOR (i, 1, n) p[i] = (i - 1 - shift + n) % n;
    FOR (i, n + 1, mx) if (p[i] == -1) p[i] = p[mx];
    FOR (i, 1, n) a[p[i]] = i;
    int sz = 0;
    FOR (i, n + 1, mx) b[sz++] = a[p[i]], a[p[i]] = i;
    return sz;
}

const int MOD = 1e9 + 9;
inline int power(int d, int up) {
    int re = 1;
    while (up) {
        if (up & 1) re = 1ll * re * d % MOD;
        d = 1ll * d * d % MOD;
        up >>= 1;
    }
    return re;
}
int countReplacement(int n, int a[]) {
    int shift = -1;
    unordered_set<int> us;
    FOR (i, 0, n - 1) {
        if (us.count(a[i])) return 0;
        if (a[i] <= n) {
            if (shift == -1) shift = (a[i] - 1 - i + n) % n;
            else if ((a[i] - 1 - i + n) % n != shift) return 0;
        }
        us.insert(a[i]);
    }
    int ans = (shift == -1 ? n : 1), cnt = 0;
    sort(a, a + n);
    FOR (i, 0, n - 1) if (a[i] > n) {
        if (i == 0 || a[i - 1] <= n) {
            cnt = n - i;
            ans = 1ll * ans * power(cnt, a[i] - n - 1) % MOD;
        } else {
            ans = 1ll * ans * power(cnt, a[i] - a[i - 1] - 1) % MOD;
        }
        cnt--;
    }
    return ans;
}

#ifdef WAIMAI
int gondolaSequence[100001];
int replacementSequence[250001];

int main() {
    int i, n, tag;
    int nr;
    assert (scanf ("%d", &tag) == 1);
    assert (scanf ("%d", &n) == 1);
    for (i = 0; i < n; i++)
        assert ( scanf ("%d", &gondolaSequence[i]) == 1);

    switch (tag) {
    case 1:
    case 2:
    case 3:
        printf ("%d\n", valid (n, gondolaSequence) );
        break;

    case 4:
    case 5:
    case 6:
        nr = replacement (n, gondolaSequence, replacementSequence);
        printf ("%d ", nr);
        if (nr > 0) {
            for (i = 0; i < nr - 1; i++)
                printf ("%d ", replacementSequence[i]);
            printf ("%d\n", replacementSequence[nr - 1]);
        } else printf ("\n");
        break;

    case 7:
    case 8:
    case 9:
    case 10:
        printf ("%d\n",  countReplacement (n, gondolaSequence) );
        break;
    }

    return 0;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...