Submission #4311320


Source Code Expand

Copy
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

const long long int MOD = 1000000007;
//const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>v(N + 1);
	vector<int>dir(N + 1);
	vector<int>used(N + 1);
	vector<int>leg(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> v[i];
		dir[v[i]]++;
		if (dir[v[i]] > 2) {
			cout << 0 << endl;
			return 0;
		}
	}
	vector<int>loop(N + 1, -1);
	queue<int>Q;
	for (int i = 1; i <= N; i++) {
		if (!dir[i])Q.push(i);
	}
	while (!Q.empty()) {
		int cn = Q.front();
		Q.pop();
		int nx = v[cn];
		used[nx]++;
		leg[nx] = leg[cn] + 1;
		leg[cn] = 0;
		if (dir[nx] - used[nx])continue;
		Q.push(nx);
	}
	for (int i = 1; i <= N; i++) {
		loop[i] = dir[i] - used[i];
	}
	for (int i = 1; i <= N; i++) {
		if (!loop[i] && (dir[i] - used[i] || dir[i]>1)) {
			cout << 0 << endl;
			return 0;
		}
	}
	vector<int>num(N + 1);
	vector<int>dist(N + 1);
	for (int i = 1; i <= N; i++) {
		if (loop[i] != 1)continue;
		if (dir[i] < 2)continue;
		int node = i;
		int cnt = 0;
		do {
			cnt++;
			node = v[node];
			loop[node] = 2;
			if (dir[node] == 2) {
				dist[node] = cnt;
				cnt = 0;
			}
		} while (node != i);
	}
	for (int i = 1; i <= N; i++) {
		if (loop[i] != 1)continue;
		int node = i;
		int cnt = 0;
		int sz = 0;
		do {
			cnt++;
			node = v[node];
			loop[node] = 2;
			sz++;
		} while (node != i);
		num[sz]++;
	}
	vector<long long int>by(N + 1, 1);
	vector<long long int>rev_by(N + 1, 1);
	vector<long long int>two_by(N + 1, 1);
	for (int i = 1; i <= N; i++) {
		by[i] = by[i - 1] * i;
		by[i] %= MOD;
		two_by[i] = two_by[i - 1] * 2;
		two_by[i] %= MOD;
		rev_by[i] = power(by[i], MOD - 2, MOD);
	}
	long long int ans = 1;
	for (int i = 1; i <= N; i++) {
		if (!leg[i])continue;
		if (leg[i] > dist[i]) {
			cout << 0 << endl;
			return 0;
		}
		if (leg[i] < dist[i]) {
			ans <<= 1;
			ans %= MOD;
		}
	}
	for (int i = 1; i <= N; i++) {
		if (!num[i])continue;
		//	cout << i << " " << num[i] << endl;
		if (num[i] == 1) {
			if ((i & 1) && i > 2)ans *= 2;
			ans %= MOD;
			continue;
		}

		long long int cnt = 1;
		if ((i & 1) && i > 2) {
			cnt *= two_by[num[i]];
			cnt %= MOD;
		}
		long long int add = by[num[i]];
		add *= rev_by[2];
		add %= MOD;
		add *= rev_by[num[i] - 2];
		add %= MOD;
		add *= i;
		add %= MOD;
		if ((i & 1) && i > 2) {
			add *= two_by[num[i] - 2];
			add %= MOD;
		}
		//cout << i << " " << num[i] << endl;
		//cout << add << endl;
		for (int j = 2; j <= num[i]; j += 2) {
			cnt += add;
			cnt %= MOD;
			if (num[i] - j - 2 >= 0) {
				add *= by[num[i] - j];
				add %= MOD;
				add *= rev_by[num[i] - j - 2];
				add %= MOD;
				add *= rev_by[2];
				add %= MOD;
				if ((i & 1) && i > 2) {
					add *= power(4, MOD - 2, MOD);
					add %= MOD;
				}
				add *= i;
				add %= MOD;
				add *= by[j / 2];
				add %= MOD;
				add *= rev_by[j / 2 + 1];
				add %= MOD;
			}
			//cout << add << endl;
		}
		ans *= cnt;
		ans %= MOD;
		//	cout <<i<<" " << ans << endl;
	}
	cout << ans << endl;
	return 0;
}

Submission Info

Submission Time
Task E - Next or Nextnext
User olphe
Language C++14 (GCC 5.4.1)
Score 1400
Code Size 3844 Byte
Status
Exec Time 98 ms
Memory 5504 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 0_04.txt
All 1400 / 1400 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 1 ms 256 KB
0_01.txt 1 ms 256 KB
0_02.txt 1 ms 256 KB
0_03.txt 1 ms 256 KB
0_04.txt 1 ms 256 KB
1_00.txt 1 ms 256 KB
1_01.txt 88 ms 5376 KB
1_02.txt 87 ms 5376 KB
1_03.txt 98 ms 5376 KB
1_04.txt 88 ms 5376 KB
1_05.txt 88 ms 5376 KB
1_06.txt 88 ms 5376 KB
1_07.txt 87 ms 5376 KB
1_08.txt 88 ms 5376 KB
1_09.txt 8 ms 1792 KB
1_10.txt 88 ms 5376 KB
1_11.txt 11 ms 2176 KB
1_12.txt 89 ms 5376 KB
1_13.txt 89 ms 5376 KB
1_14.txt 89 ms 5376 KB
1_15.txt 89 ms 5376 KB
1_16.txt 88 ms 5376 KB
1_17.txt 89 ms 5376 KB
1_18.txt 87 ms 5248 KB
1_19.txt 87 ms 5248 KB
1_20.txt 87 ms 5248 KB
1_21.txt 88 ms 5376 KB
1_22.txt 89 ms 5504 KB
1_23.txt 89 ms 5376 KB
1_24.txt 88 ms 5376 KB
1_25.txt 89 ms 5504 KB
1_26.txt 87 ms 5248 KB
1_27.txt 88 ms 5376 KB
1_28.txt 11 ms 2176 KB
1_29.txt 11 ms 2176 KB
1_30.txt 87 ms 5248 KB
1_31.txt 86 ms 5248 KB
1_32.txt 88 ms 5376 KB
1_33.txt 88 ms 5376 KB
1_34.txt 87 ms 5376 KB
1_35.txt 88 ms 5376 KB
1_36.txt 86 ms 5248 KB
1_37.txt 88 ms 5376 KB
1_38.txt 88 ms 5376 KB
1_39.txt 88 ms 5376 KB
1_40.txt 88 ms 5376 KB
1_41.txt 12 ms 2176 KB
1_42.txt 88 ms 5248 KB
1_43.txt 89 ms 5376 KB
1_44.txt 89 ms 5376 KB
1_45.txt 89 ms 5376 KB
1_46.txt 89 ms 5504 KB
1_47.txt 89 ms 5376 KB
1_48.txt 89 ms 5504 KB
1_49.txt 89 ms 5376 KB
1_50.txt 90 ms 5376 KB
1_51.txt 87 ms 5248 KB
1_52.txt 88 ms 5376 KB
1_53.txt 90 ms 5376 KB