結果
問題 | No.590 Replacement |
ユーザー | e869120 |
提出日時 | 2018-06-09 21:32:46 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,810 bytes |
コンパイル時間 | 1,635 ms |
コンパイル使用メモリ | 109,460 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-06-30 13:02:59 |
合計ジャッジ時間 | 3,667 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 1 ms
5,376 KB |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 2 ms
5,376 KB |
testcase_39 | AC | 2 ms
5,376 KB |
testcase_40 | WA | - |
testcase_41 | WA | - |
testcase_42 | AC | 2 ms
5,376 KB |
testcase_43 | AC | 2 ms
5,376 KB |
testcase_44 | AC | 2 ms
5,376 KB |
testcase_45 | WA | - |
testcase_46 | WA | - |
コンパイルメッセージ
main.cpp: In function 'long long int solve(long long int, long long int, long long int, long long int&, long long int&)': main.cpp:44:1: warning: control reaches end of non-void function [-Wreturn-type] 44 | } | ^
ソースコード
#include <iostream> #include <vector> #include <tuple> #include <map> #include <algorithm> using namespace std; #pragma warning (disable: 4996) int N, A[100009], B[100009], col1[100009], cnt1[100009], S1[100009], col2[100009], cnt2[100009], S2[100009], cnts1, cnts2; vector<tuple<int, int, int>>G; map<tuple<int, int, int>, vector<long long>>M; map<tuple<int, int, int>, long long>MM; long long extgcd(long long a, long long b, long long &x, long long &y) { long long d = a; if (b != 0) { d = extgcd(b, a%b, y, x); y -= (a / b) * x; } else { x = 1; y = 0; } return d; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a%b); } long long solve(long long s1, long long s2, long long v, long long &x, long long &y) { if (gcd(s1, s2) >= 2) { solve(s1 / gcd(s1, s2), s2 / gcd(s1, s2), v / gcd(s1, s2), x, y); } else if (v != 1) { solve(s1, s2, 1, x, y); x *= v; y *= v; x %= (s1 * s2 / gcd(s1, s2)); y %= (s1 * s2 / gcd(s1, s2)); } else { return extgcd(s1, s2, x, y); } } long long division(long long p, long long s1, long long q, long long s2) { long long x, y; solve(s1, s2, q - p, x, y); long long G = (s1 * s2 / gcd(s1, s2)); return (x * s1 + p + G * 100000000LL) % G; } void dfs1(int pos, int v) { col1[pos] = cnts1; cnt1[pos] = v; S1[cnts1]++; if (col1[A[pos]] == 0) { dfs1(A[pos], v + 1); } } void dfs2(int pos, int v) { col2[pos] = cnts2; cnt2[pos] = v; S2[cnts2]++; if (col2[B[pos]] == 0) { dfs2(B[pos], v + 1); } } int main() { FILE *in = freopen("in1.txt", "r", stdin); cin >> N; for (int i = 1; i <= N; i++) cin >> A[i]; for (int i = 1; i <= N; i++) cin >> B[i]; for (int i = 1; i <= N; i++) { if (col1[i] == 0) { cnts1++; dfs1(i, 0); } } for (int i = 1; i <= N; i++) { if (col2[i] == 0) { cnts2++; dfs2(i, 0); } } for (int i = 1; i <= N; i++) { long long F1 = S1[col1[i]], F2 = S2[col2[i]], K = F1*F2 / gcd(F1, F2), KK = gcd(F1, F2); long long U = (cnt2[i] - cnt1[i] + K) % KK; if (K == 1720) { K += 0; } long long TT = division(cnt1[i], F1, cnt2[i] - U, F2); //cout << "A = " << cnt1[i] << ", B = " << cnt2[i] << ", K = " << K << ", Z = " << TT << endl; G.push_back(make_tuple(col1[i], col2[i], U)); M[make_tuple(col1[i], col2[i], U)].push_back(TT); MM[make_tuple(col1[i], col2[i], U)] = K; } sort(G.begin(), G.end()); G.erase(unique(G.begin(), G.end()), G.end()); long long ans = 0; for (int i = 0; i < G.size(); i++) { vector<long long> Z = M[G[i]]; sort(Z.begin(), Z.end()); long long B = Z[0]; for (int j = 0; j < Z.size(); j++) Z[j] -= B; for (int j = 0; j < Z.size(); j++) { long long GG = Z[(j + 1) % Z.size()] - Z[j]; if (j == Z.size() - 1) GG += MM[G[i]]; GG %= 1000000007; ans += GG * (GG - 1) / 2; ans %= 1000000007; } } cout << ans << endl; return 0; }