QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210255 | #5487. Movie Night | hagry# | WA | 0ms | 3876kb | C++14 | 2.7kb | 2023-10-11 10:05:08 | 2023-10-11 10:05:08 |
Judging History
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 ll MOD = 1e9 + 7;
ll powmod(ll x, ll y) {
x %= MOD;
ll ans = 1;
while (y) {
if (y & 1) ans = ans * x % MOD;
x = x * x % MOD;
y >>= 1;
}
return ans;
}
ll add(ll a, ll b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
ll sub(ll a, ll b) {
a -= b;
if (a < 0) a += MOD;
return a;
}
ll mul(ll a, ll b) { return a * b % MOD; }
ll inv(ll a) { return powmod(a, MOD - 2); }
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) {
assert(cycID[i]);
if (in[i] == 0)
ways[cycID[i]] = mul(ways[cycID[i]], add(len[i], 1));
}
for (int i = 1; i <= cycNum; ++i)
ans = mul(ans, add(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: 3876kb
input:
4 2 3 4 3
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
5 2 3 1 5 4
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
6 2 4 2 6 1 3
output:
3
result:
ok single line: '3'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3680kb
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: 3640kb
input:
4 3 3 4 3
output:
4
result:
ok single line: '4'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3876kb
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: 3640kb
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: 3548kb
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: 3616kb
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'