結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
![]() |
提出日時 | 2017-05-26 14:02:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,705 bytes |
コンパイル時間 | 1,473 ms |
コンパイル使用メモリ | 166,420 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-19 20:05:03 |
合計ジャッジ時間 | 2,724 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } //--------------------------------------------------------------------------------------------------- typedef long long ll; ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } /*--------------------------------------------------------------------------------------------------- ∧_∧ ∧_∧ (´<_` ) Welcome to My Coding Space! ( ´_ゝ`) / ⌒i / \ | | / / ̄ ̄ ̄ ̄/ | __(__ニつ/ _/ .| .|____ \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, K; int E[101]; //--------------------------------------------------------------------------------------------------- int dfs(int cu, int to, int d = 0) { if (d == 0) return dfs(E[cu], to, 1); if (1010 < d) return -1; if (cu == to) return d; else return dfs(E[cu], to, d + 1); } //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> K; rep(i, 1, N + 1) E[i] = i; rep(i, 0, K) { int x, y; cin >> x >> y; swap(E[x], E[y]); } ll ans = 1; rep(i, 1, N + 1) { int loop = dfs(i, i); if (loop < 0) { ans = -1; break; } ans = lcm(ans, loop); } cout << ans << endl; }