結果
問題 | No.590 Replacement |
ユーザー | ats5515 |
提出日時 | 2017-11-04 00:35:11 |
言語 | C++11 (gcc 11.4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,241 bytes |
コンパイル時間 | 1,156 ms |
コンパイル使用メモリ | 109,548 KB |
実行使用メモリ | 29,652 KB |
最終ジャッジ日時 | 2024-11-23 15:33:08 |
合計ジャッジ時間 | 5,415 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 1 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 7 ms
5,248 KB |
testcase_08 | AC | 5 ms
5,248 KB |
testcase_09 | AC | 8 ms
5,248 KB |
testcase_10 | AC | 4 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 7 ms
5,248 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | AC | 4 ms
5,248 KB |
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,248 KB |
testcase_34 | AC | 2 ms
5,248 KB |
testcase_35 | AC | 1 ms
5,248 KB |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | AC | 66 ms
9,544 KB |
testcase_39 | AC | 65 ms
9,544 KB |
testcase_40 | AC | 210 ms
20,736 KB |
testcase_41 | AC | 206 ms
20,736 KB |
testcase_42 | AC | 66 ms
9,308 KB |
testcase_43 | AC | 66 ms
9,588 KB |
testcase_44 | AC | 138 ms
29,652 KB |
testcase_45 | WA | - |
testcase_46 | WA | - |
ソースコード
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <string>#include <iomanip>#include <algorithm>#include <cmath>#include <stdio.h>using namespace std;#define int long longint MOD = 1000000007;using lll = __int128_t;std::pair<lll, lll> crt(lll a1, lll m1, lll a2, lll m2) {auto normal = [](lll x, lll m) { return x >= -x ? x % m : m - (-x) % m; };auto modmul = [&normal](lll a, lll b, lll m) { return normal(a, m) * normal(b, m) % m; };auto extgcd = [](lll a, lll b, lll &x, lll &y) {for (lll u = y = 1, v = x = 0; a;) {lll q = b / a;std::swap(x -= q * u, u);std::swap(y -= q * v, v);std::swap(b -= q * a, a);}return b;};lll k1, k2;lll g = extgcd(m1, m2, k1, k2);if (normal(a1, g) != normal(a2, g))return std::make_pair(-1, -1);else {lll l = m1 / g * m2;lll x = a1 + modmul(modmul((a2 - a1) / g, k1, l), m1, l);return std::make_pair(x, l);}}std::pair<lll, lll> crt(std::vector<lll> a, std::vector<lll> m) {lll mod = 1, ans = 0;int n = a.size();for (int i = 0; i < n; i++) {std::tie(ans, mod) = crt(ans, mod, a[i], m[i]);if (ans == -1) return std::make_pair(-1, -1);}return std::make_pair(ans, mod);}int gcd(int a, int b){int c;while (a != 0) {c = a; a = b%a; b = c;}return b;}int f(int a) {if (a <= 0) {return 0;}return ((a%MOD)*((a - 1 + MOD) % MOD) * ((MOD + 1) / 2))%MOD;}signed main() {cin.tie(0);ios::sync_with_stdio(false);int N;cin >> N;vector<int> A(N);vector<int> B(N);vector<int> nA(N, -1);vector<int> nB(N, -1);vector<int> indA(N);vector<int> indB(N);vector<int> szA;vector<int> szB;int res = 0;for (int i = 0; i < N; i++) {cin >> A[i];A[i]--;}for (int i = 0; i < N; i++) {cin >> B[i];B[i]--;}int t;int c = 0;int cc = 0;for (int i = 0; i < N; i++) {if (nA[i] == -1) {cc = 0;t = i;while (true) {nA[t] = c;indA[t] = cc;cc++;t = A[t];if (i == t)break;}szA.push_back(cc);c++;}}c = 0;for (int i = 0; i < N; i++) {if (nB[i] == -1) {cc = 0;t = i;while (true) {nB[t] = c;indB[t] = cc;cc++;t = B[t];if (i == t)break;}szB.push_back(cc);c++;}}map<pair<pair<int, int>, int >, vector<int> > mp;map<pair<pair<int, int>, int >, int > mp2;int g;int k;pair<pair<int, int>, int > pp;for (int i = 0; i < N; i++) {//cerr << nA[i] << " " << nB[i] << " " << indA[i] << " " << indB[i] << endl;g = gcd(szA[nA[i]], szB[nB[i]]);k = (indB[i] - indA[i] + g * 1000000) % g;pp.first.first = nA[i];pp.first.second = nB[i];pp.second = k;mp2[pp] = (szA[nA[i]] / g) * (szB[nB[i]]);mp[pp].push_back(crt(indA[i], szA[nA[i]], (indB[i] - k + 10 * szB[nB[i]]) % szB[nB[i]], szB[nB[i]]).first);}int sz;for (auto m : mp) {sz = mp2[m.first];sort(m.second.begin(), m.second.end());/*for (int i = 0; i < (int)m.second.size(); i++) {cerr << m.second[i] << " ";}cerr << endl;*/for (int i = 0; i < (int)m.second.size() - 1; i++) {res = (res + f(m.second[i + 1] - m.second[i])) % MOD;}res = (res + f(sz + m.second[0] - m.second.back())) % MOD;}cout << res << endl;}