QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#210194#5487. Movie Nighthagry#WA 1ms3428kbC++142.3kb2023-10-11 08:33:302023-10-11 08:33:31

Judging History

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

  • [2023-10-11 08:33:31]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3428kb
  • [2023-10-11 08:33:30]
  • 提交

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()]);
        }

        if (vis[st.top()] == i) {
            ++cycNum;
            int prv;
            int start = 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()) {
            int 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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3428kb

input:

4
2
3
4
3

output:

1

result:

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