結果
問題 | No.101 ぐるぐる!あみだくじ! |
ユーザー |
|
提出日時 | 2016-12-13 17:47:15 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 5,000 ms |
コード長 | 1,728 bytes |
コンパイル時間 | 780 ms |
コンパイル使用メモリ | 87,192 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-30 01:10:16 |
合計ジャッジ時間 | 1,906 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
ソースコード
#include <iostream>#include <vector>#include <algorithm>#include <numeric>#include <array>#include <set>#include <map>#include <queue>#include <tuple>#include <unordered_set>#include <unordered_map>#include <functional>#include <cassert>#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= int(m); --(i))#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)typedef long long ll;using namespace std;template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }template <class T> void setmin(T & a, T const & b) { if (b < a) a = b; }template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }ll gcd(ll a, ll b) { while (a) { b %= a; swap(a, b); } return b; }ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); }int main() {int n, k; cin >> n >> k;vector<int> f_inv(n);whole(iota, f_inv, 0);while (k --) {int x, y; cin >> x >> y; -- x; -- y;swap(f_inv[x], f_inv[y]);}vector<int> f(n); repeat (i,n) f[f_inv[i]] = i;vector<int> cycle(n);repeat (i,n) {int j = f[i];int k = 1;while (j != i) {j = f[j];++ k;}cycle[i] = k;}ll s = 1;for (int k : cycle) s = lcm(s, k);cout << s << endl;return 0;}