Submission #1694529
Source Code Expand
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <cmath> using namespace std; const int MAXN = 100000 + 1000; const int MOD = 1000000000 + 7; const int INF = 0x1f1f1f1f; int n, p[MAXN]; int ans = 1; vector<int> redg[MAXN]; vector<int> vc, vl; bool vis[MAXN], onc[MAXN]; int cnt[MAXN], f[MAXN]; int get_len(int x) { vis[x] = true; int ret = 0; for(int i = 0; i < redg[x].size(); i++) if(!onc[redg[x][i]]) ret = ret == 0 ? get_len(redg[x][i]) + 1 : -INF; return ret; } int find_circle(int x) { vis[x] = true; int y = p[x], t = vis[y] ? y : find_circle(y); if(t) onc[x] = true, vc.push_back(x); return t == x ? 0 : t; } void work() { for(int i = 1; i <= n; i++) redg[p[i]].push_back(i); for(int i = 1; i <= n; i++) if(!vis[i]) { vc.clear(), vl.clear(); find_circle(i); reverse(vc.begin(), vc.end()); int last = -1; for(int j = 0; j < vc.size(); j++) { int x = vc[j], t = get_len(x); if(t < 0) { ans = 0; return ; } else if(t > 0) last = j; vl.push_back(t); // cerr << vc[j] << ' ' << vl[j] << endl; } // cerr << endl; if(last < 0) { cnt[vc.size()]++; continue; } else { for(int j = 0; j < vc.size(); j++) if(vl[j] > 0) { int l1 = vl[j], l2 = (j - last + vc.size() - 1) % vc.size() + 1; // cerr << l1 << ' ' << l2 << endl; if(l1 < l2) ans = (ans * 2) % MOD; else if(l1 == l2) ans = (ans * 1) % MOD; else ans = 0; last = j; } } } for(int i = 1; i <= n; i++) if(cnt[i]) { int lam = (i == 1 || i % 2 == 0 ? 1 : 2); f[0] = 1; for(int j = 1; j <= cnt[i]; j++) { f[j] = (long long)f[j - 1] * lam % MOD; if(j - 2 >= 0) f[j] = (f[j] + (long long)f[j - 2] * (j - 1) % MOD * i) % MOD; } ans = (long long)ans * f[cnt[i]] % MOD; } } int main() { ios::sync_with_stdio(false); // freopen("1.in", "r", stdin); // freopen("1.out", "w", stdout); cin >> n; for(int i = 1; i <= n; i++) cin >> p[i]; work(); cout << ans << endl; return 0; }
Submission Info
Submission Time | |
---|---|
Task | E - Next or Nextnext |
User | ct123098 |
Language | C++14 (GCC 5.4.1) |
Score | 1400 |
Code Size | 2294 Byte |
Status | AC |
Exec Time | 26 ms |
Memory | 10620 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 1400 / 1400 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt |
All | 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt, 1_36.txt, 1_37.txt, 1_38.txt, 1_39.txt, 1_40.txt, 1_41.txt, 1_42.txt, 1_43.txt, 1_44.txt, 1_45.txt, 1_46.txt, 1_47.txt, 1_48.txt, 1_49.txt, 1_50.txt, 1_51.txt, 1_52.txt, 1_53.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
0_00.txt | AC | 3 ms | 2688 KB |
0_01.txt | AC | 3 ms | 2688 KB |
0_02.txt | AC | 3 ms | 2688 KB |
0_03.txt | AC | 3 ms | 2688 KB |
0_04.txt | AC | 3 ms | 2688 KB |
1_00.txt | AC | 3 ms | 2688 KB |
1_01.txt | AC | 19 ms | 6784 KB |
1_02.txt | AC | 21 ms | 6528 KB |
1_03.txt | AC | 21 ms | 6528 KB |
1_04.txt | AC | 23 ms | 7932 KB |
1_05.txt | AC | 24 ms | 8444 KB |
1_06.txt | AC | 26 ms | 10616 KB |
1_07.txt | AC | 23 ms | 6400 KB |
1_08.txt | AC | 25 ms | 10616 KB |
1_09.txt | AC | 22 ms | 10108 KB |
1_10.txt | AC | 25 ms | 10616 KB |
1_11.txt | AC | 24 ms | 10360 KB |
1_12.txt | AC | 25 ms | 9340 KB |
1_13.txt | AC | 25 ms | 9596 KB |
1_14.txt | AC | 24 ms | 8444 KB |
1_15.txt | AC | 25 ms | 9464 KB |
1_16.txt | AC | 25 ms | 9464 KB |
1_17.txt | AC | 25 ms | 9464 KB |
1_18.txt | AC | 25 ms | 10492 KB |
1_19.txt | AC | 26 ms | 10620 KB |
1_20.txt | AC | 25 ms | 10492 KB |
1_21.txt | AC | 26 ms | 10616 KB |
1_22.txt | AC | 25 ms | 8696 KB |
1_23.txt | AC | 25 ms | 8696 KB |
1_24.txt | AC | 25 ms | 8692 KB |
1_25.txt | AC | 25 ms | 8696 KB |
1_26.txt | AC | 23 ms | 8316 KB |
1_27.txt | AC | 24 ms | 8448 KB |
1_28.txt | AC | 23 ms | 8320 KB |
1_29.txt | AC | 23 ms | 8448 KB |
1_30.txt | AC | 23 ms | 8064 KB |
1_31.txt | AC | 22 ms | 7040 KB |
1_32.txt | AC | 23 ms | 7932 KB |
1_33.txt | AC | 23 ms | 7932 KB |
1_34.txt | AC | 23 ms | 6400 KB |
1_35.txt | AC | 23 ms | 6656 KB |
1_36.txt | AC | 23 ms | 6272 KB |
1_37.txt | AC | 23 ms | 6528 KB |
1_38.txt | AC | 23 ms | 7168 KB |
1_39.txt | AC | 24 ms | 6784 KB |
1_40.txt | AC | 23 ms | 6912 KB |
1_41.txt | AC | 21 ms | 7040 KB |
1_42.txt | AC | 22 ms | 6272 KB |
1_43.txt | AC | 23 ms | 6400 KB |
1_44.txt | AC | 23 ms | 6400 KB |
1_45.txt | AC | 23 ms | 6400 KB |
1_46.txt | AC | 23 ms | 5632 KB |
1_47.txt | AC | 23 ms | 5504 KB |
1_48.txt | AC | 24 ms | 5632 KB |
1_49.txt | AC | 23 ms | 5632 KB |
1_50.txt | AC | 22 ms | 6272 KB |
1_51.txt | AC | 22 ms | 6272 KB |
1_52.txt | AC | 24 ms | 6272 KB |
1_53.txt | AC | 23 ms | 6272 KB |