QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#577649#9353. Interesting PermutationplutosTL 22ms6796kbC++173.9kb2024-09-20 13:44:142024-09-20 13:44:14

Judging History

你现在查看的是最新测评结果

  • [2024-09-20 13:44:14]
  • 评测
  • 测评结果:TL
  • 用时:22ms
  • 内存:6796kb
  • [2024-09-20 13:44:14]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MOD = 1e9+7;
template<long long P>
struct ModInt {
public:
    ModInt() : v(0) {}

    template<typename T>
    ModInt(T x) { v = ((long long) x % P + P) % P; }

    long long val() const { return v; }

    ModInt &operator++() {
        ++v;
        if (v == P)
            v = 0;
        return *this;
    }

    ModInt &operator--() {
        if (v == 0)
            v = P;
        --v;
        return *this;
    }

    ModInt operator++(int) {
        ModInt oldVal = *this;
        ++*this;
        return oldVal;
    }

    ModInt operator--(int) {
        ModInt oldVal = *this;
        --*this;
        return oldVal;
    }

    ModInt &operator+=(const ModInt &rhs) {
        v += rhs.v;
        v %= P;
        return *this;
    }

    ModInt &operator-=(const ModInt &rhs) {
        v -= rhs.v;
        v = (v + P) % P;
        return *this;
    }

    ModInt &operator*=(const ModInt &rhs) {
        v *= rhs.v;
        v %= P;
        return *this;
    }

    ModInt &operator/=(const ModInt &rhs) { return *this = *this * rhs.inv(); }

    ModInt operator+() const { return *this; }

    ModInt operator-() const { return ModInt() - *this; }

    ModInt pow(long long exp) const {
        // assert(exp>=0);
        ModInt res = 1;
        ModInt base = *this;
        while (exp) {
            if (exp & 1)
                res *= base;
            base *= base;
            exp >>= 1;
        }
        return res;
    }

    ModInt inv() const {
        // assert(v);
        return pow(P - 2);
    }

    friend ModInt operator+(const ModInt &lhs, const ModInt &rhs) {
        ModInt res = lhs;
        res += rhs;
        return res;
    }

    friend ModInt operator-(const ModInt &lhs, const ModInt &rhs) {
        ModInt res = lhs;
        res -= rhs;
        return res;
    }

    friend ModInt operator*(const ModInt &lhs, const ModInt &rhs) {
        ModInt res = lhs;
        res *= rhs;
        return res;
    }

    friend ModInt operator/(const ModInt &lhs, const ModInt &rhs) {
        ModInt res = lhs;
        res /= rhs;
        return res;
    }

    friend bool operator==(const ModInt &lhs, const ModInt &rhs) { return lhs.v == rhs.v; }

    friend bool operator!=(const ModInt &lhs, const ModInt &rhs) { return lhs.v != rhs.v; }

    friend std::istream &operator>>(std::istream &is, ModInt &aim) {
        long long tmp;
        is >> tmp;
        aim = ModInt(tmp);
        return is;
    }

    friend std::ostream &operator<<(std::ostream &os, const ModInt &aim) { return os << aim.val(); }

private:
    long long v;
};

using mint = ModInt<MOD>;
mint fact[200005];
mint infact[200005];

void init() {
    infact[0] = fact[0] = 1;
    for (int i = 1; i <= 200002; i++)
        fact[i] = fact[i - 1] * i, infact[i] = infact[i - 1] / i;
}

mint C(int a, int b) {
    if (a < b || a < 0 || b < 0) return 0;
    return fact[a] * infact[b] * infact[a - b];
}
int a[200005];
void Solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (a[1] != 0 || a[n] != n - 1) {
        cout<<0<<"\n";
        return;
    }
    if (n == 1) {
        cout<<1<<"\n";
        return;
    }
    if (n == 2) {
        cout<<2<<"\n";
        return;
    }
    mint ans = 2;
    int rem = 0;
    for (int i = 2; i <= n; i++) {
        int num = 0;
        if(a[i] != n - 1)
            ans *= 2;
        for(int j = i + 1; j <= n ; j++ ) {
            if(a[j] == a[i])
                num++;
            else {
                i = j - 1;
                break;
            }
        }
        rem += (a[i] - a[i-1] - 1) ;
        ans *= fact[num];
        ans *= C(rem,num);

    }
    cout<<ans<<"\n";

}

int main() {
    init();
    int t;
    cin >> t;
    while(t--) {
        Solve();
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 22ms
memory: 6796kb

input:

3
3
0 2 2
3
0 1 2
3
0 2 3

output:

2
4
0

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

input:

10039
14
5 6 7 8 9 10 11 12 13 13 13 13 13 13
14
0 5 6 7 8 9 10 11 12 13 14 14 14 14
1
1
14
0 5 4 8 9 10 11 12 13 13 13 13 13 13
45
0 1 1 2 2 3 5 5 6 6 8 9 11 13 15 17 18 18 20 22 22 24 26 26 26 26 27 27 27 28 30 32 32 33 34 34 34 36 36 38 38 38 39 39 44
24
0 2 3 5 7 9 9 10 11 12 13 14 14 14 14 15 1...

output:

0
0
0
8847360
0
0
0
0
0
0
0
0
0
0
0
8
0
0
2
0
0
0
0
2
0
0
0
0
0
0
0
0
0
0
1536
0
0
0
0
0
2
0
0
0
0
2
0
2
0
0
0
0
0
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
0
384
0
0
0
0
0
2
0
0
0
234854077
0
0
0
0
0
0
294912
0
0
0
0
0
4
0
0
0
0
0
0
0
16
0
0
0
0
0
4
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
7...

result: