結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー char134217728
提出日時 2017-08-07 03:56:01
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 867 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 158,188 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-11 23:04:18
合計ジャッジ時間 2,676 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:25:1: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
   25 | 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