結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー たこしたこし
提出日時 2015-11-04 15:26:23
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,483 bytes
コンパイル時間 499 ms
コンパイル使用メモリ 67,308 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-11 13:41:09
合計ジャッジ時間 1,932 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 1 ms
4,352 KB
testcase_39 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>
#include <queue>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;

#define MAX_N 101
#define MAX_K 1001

int N, K;
int X[MAX_K], Y[MAX_K];


inline LL gcd(LL a, LL b){
  if(a == 0 || b == 0)
    return 0;
  LL c;
  while(a != 0){
    c = a; a = b%a; b = c;
  }
  return b;
}

inline LL lcm(LL m, LL n){

  if(0 == m || 0 == n)
    return 0;

  return m/gcd(m,n) * n;

}

void ExecAmida(vector<int>& AmidaList){

  for(int k = 0; k < K; k++){
    int x = X[k], y = Y[k];
    x--; y--;
    swap(AmidaList[x], AmidaList[y]);
  }

}

void Input(){

  cin >> N;
  cin >> K;
  
  for(int i = 0; i < K; i++){
    cin >> X[i] >> Y[i];
  }

}

LL CalcAmida(int pos, vector<int> NextList){

  int prePos = pos;
  LL ret = 0;

  while(true){
    ret++;
    pos = NextList[pos];
    if(pos == prePos)
      break;
  }

  return ret;
  
}

LL Solve(){

  vector<int> AmidaList;

  for(int i = 1; i <= N; i++){
    AmidaList.push_back(i);
  }

  //MakeAmidaExec
  ExecAmida(AmidaList);

  vector<int> NextList;
  for(int i = 1; i <= N; i++){
    for(int j = 0; j < N; j++){
      if(AmidaList[j] == i){
	NextList.push_back(j);
	break;
      }
    }
  }

  LL ret = 1;
  bool used[MAX_N];
  memset(used, 0, sizeof(used));

  for(int i = 0; i < N; i++){
    ret = lcm(ret, CalcAmida(i, NextList));
  }

  return ret;

}

int main(){

  Input();

  cout << Solve() << endl;

  return 0;

}
0