結果
問題 | No.261 ぐるぐるぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2015-08-01 00:09:55 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2,025 ms / 5,000 ms |
コード長 | 2,645 bytes |
コンパイル時間 | 1,153 ms |
コンパイル使用メモリ | 106,008 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-18 00:11:37 |
合計ジャッジ時間 | 7,240 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:118:38: warning: ‘maxy’ may be used uninitialized in this function [-Wmaybe-uninitialized] 118 | for (int n = maxy; n <= 223092800 * 2; n += maxi) { | ~~^~~~~~~~~~~~~~~~
ソースコード
#define _USE_MATH_DEFINES#include <algorithm>#include <cstdio>#include <functional>#include <iostream>#include <cfloat>#include <climits>#include <complex>#include <cstdlib>#include <cstring>#include <cmath>#include <map>#include <queue>#include <random>#include <set>#include <sstream>#include <stack>#include <string>#include <time.h>#include <vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> i_i;typedef pair<ll, int> ll_i;typedef pair<double, int> d_i;typedef pair<ll, ll> ll_ll;typedef pair<double, double> d_d;struct edge { int u, v; ll w; };ll MOD = 1000000007;ll _MOD = 1000000009;int main() {int N, K; cin >> N >> K;vector<int> X(K), Y(K);for (int k = 0; k < K; k++) {cin >> X[k] >> Y[k];X[k]--; Y[k]--;}vector<int> a(N);for (int i = 0; i < N; i++)a[i] = i;for (int k = K - 1; k >= 0; k--)swap(a[X[k]], a[Y[k]]);int Q; cin >> Q;while (Q--) {vector<int> A(N);for (int i = 0; i < N; i++) {cin >> A[i];A[i]--;}vector<bool> vis(N);vector<int> x, y;bool ok = true;for (int i = 0; i < N; i++) {if (vis[i]) continue;vis[i] = true;int z = 1;for (int _i = a[i]; _i != i; _i = a[_i]) {z++;vis[_i] = true;}x.push_back(z);z = 0;for (int _i = i; A[_i] != i && z <= 100; _i = a[_i]) z++;if (z > 100) {ok = false;continue;}y.push_back(z);for (int _i = a[i]; _i != i; _i = a[_i]) {int j = _i;for (int t = 0; t < z; t++)j = a[j];if (A[j] != _i) ok = false;}}if (!ok) {cout << -1 << endl;continue;}map<int, int> mp;for (int k = 0; k < x.size(); k++) {if (mp.count(x[k]) && mp[x[k]] != y[k])ok = false;mp[x[k]] = y[k];}if (!ok) {cout << -1 << endl;continue;}x.clear(); y.clear();for (auto it = mp.begin(); it != mp.end(); it++) {x.push_back(it->first);y.push_back(it->second);}bool hoge = false;/*for (int n = 1; n <= 2230928 + 1; n++) {bool ok = true;for (int k = 0; k < x.size(); k++)if (n % x[k] != y[k])ok = false;if (ok) {cout << n << endl;hoge = true;break;}}*/if (hoge) continue;int maxi = 0, maxy;for (int k = 0; k < x.size(); k++)if (x[k] > maxi) {maxi = x[k];maxy = y[k];}for (int n = maxy; n <= 223092800 * 2; n += maxi) {if (!n) continue;bool ok = true;for (int k = 0; k < x.size(); k++)if (n % x[k] != y[k])ok = false;if (ok) {cout << n << endl;hoge = true;break;}}if (!hoge) cout << -1 << endl;}}