QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#210195#5487. Movie Nighthagry#WA 0ms3880kbC++142.3kb2023-10-11 08:40:112023-10-11 08:40:11

Judging History

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

  • [2023-10-11 08:40:11]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3880kb
  • [2023-10-11 08:40:11]
  • 提交

answer

#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define MP make_pair
#define all(x) x.begin(),x.end()
#define Hagry ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);

using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vector<int>>;

const int OO = 1e9 + 5;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;

ll mul(ll a, ll b) {
    return a * b % MOD;
}

ll sub(ll a, ll b) {
    a -= b;
    if (a < 0) a += MOD;
    return a;
}

void TC() {
    int n;
    cin >> n;

    int nxt[n + 1];
    nxt[0] = 0;
    for (int i = 1; i <= n; ++i)
        cin >> nxt[i];

    vi len(n + 1), vis(n + 1), in(n + 1), cycID(n + 1);
    ll ans = 1;
    int cycNum = 0;
    for (int i = 1; i <= n; ++i) {
        ++in[nxt[i]];
        if (vis[i])continue;

        stack<int> st;
        st.push(i);
        while (!vis[st.top()]) {
            vis[st.top()] = i;
            st.push(nxt[st.top()]);
        }

        int prv = -1;
        if (vis[st.top()] == i) {
            ++cycNum;
            int start = st.top();
            prv = st.top();
            st.pop();
            while (!st.empty() && vis[st.top()] == i) {
                cycID[st.top()] = cycNum;
                prv = st.top();
                st.pop();
                if (prv == start) break;
            }
        }
        if (!st.empty()) {
            if (prv == -1) {
                prv = st.top();
                st.pop();
            }
            while (!st.empty()) {
                cycID[st.top()] = cycID[prv];
                len[st.top()] = len[prv] + 1;
                prv = st.top();
                st.pop();
            }
        }

    }

    vector<ll> ways(cycNum + 1, 1);
    for (int i = 1; i <= n; ++i) {
        if (in[i] == 0)
            ways[cycID[i]] = mul(ways[cycID[i]], len[i] + 1);
    }
    for (int i = 1; i <= cycNum; ++i)
        ans = mul(ans, ways[i] + 1);

    ans = sub(ans, 1);
    cout << ans;
}

int32_t main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#endif
    Hagry
    int t = 1;
//    cin >> t;
    while (t--) {
        TC();
        cout << '\n';
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

input:

4
2
3
4
3

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3640kb

input:

5
2
3
1
5
4

output:

3

result:

ok single line: '3'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

6
2
4
2
6
1
3

output:

3

result:

ok single line: '3'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

8
2
3
4
1
3
3
1
4

output:

16

result:

ok single line: '16'

Test #5:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

4
3
3
4
3

output:

4

result:

ok single line: '4'

Test #6:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

8
8
6
8
1
3
3
3
6

output:

24

result:

ok single line: '24'

Test #7:

score: 0
Accepted
time: 0ms
memory: 3880kb

input:

8
6
6
6
1
8
7
1
7

output:

24

result:

ok single line: '24'

Test #8:

score: 0
Accepted
time: 0ms
memory: 3648kb

input:

7
2
3
1
5
4
7
6

output:

7

result:

ok single line: '7'

Test #9:

score: -100
Wrong Answer
time: 0ms
memory: 3584kb

input:

11
2
5
2
2
6
7
5
5
10
11
10

output:

164

result:

wrong answer 1st lines differ - expected: '56', found: '164'