結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー char134217728char134217728
提出日時 2017-08-07 03:56:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 867 bytes
コンパイル時間 1,725 ms
コンパイル使用メモリ 144,836 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-02 03:55:58
合計ジャッジ時間 3,568 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:25:6: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
 main(){
      ^

ソースコード

diff #

#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(a);i>=(b);i--)
#define pb push_back

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef set<int> si;
const int inf = 1e9;
const int mod = 1e9+7;

int n, k;
int a[101];
bool b[101];
int gcd(int a, int b){
  if(a > b)swap(a, b);
  if(a < 1) return b;
  return gcd(b % a, a);
}
int lcm(int a, int b){
  return (int)((ll)a * (ll)b / gcd(a, b));
}
main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  FOR(i, 1, n+1) a[i] = i;
  FOR(i, 0, k){
    int x, y;
    cin >> x >> y;
    swap(a[x], a[y]);
  }
  int ans = 1;
  FOR(i, 1, n+1){
    if(b[i])continue;
    int c = 0, m = i;
    while(!b[m]){
      c++;
      b[m] = true;
      m = a[m];
    }
    ans = lcm(ans, c);
  }
  cout << ans << endl;
}
0