QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#210189#5487. Movie Nighthagry#WA 0ms3840kbC++142.1kb2023-10-11 08:21:472023-10-11 08:21:48

Judging History

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

  • [2023-10-11 08:21:48]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3840kb
  • [2023-10-11 08:21:47]
  • 提交

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 = st.top();
        int start = st.top();
        if(vis[st.top()] == i){
            st.pop();
            ++cycNum;
        }
        while(!st.empty() && vis[st.top()] == i){
            cycID[st.top()] = cycNum;
            prv = st.top();
            st.pop();
            if(prv == start) break;
        }

        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
2
3
4
3

output:

3

result:

ok single line: '3'

Test #2:

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

input:

5
2
3
1
5
4

output:

3

result:

ok single line: '3'

Test #3:

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

input:

6
2
4
2
6
1
3

output:

4

result:

wrong answer 1st lines differ - expected: '3', found: '4'