結果
| 問題 | No.101 ぐるぐる!あみだくじ! |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2014-12-11 23:44:51 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 783 bytes |
| コンパイル時間 | 588 ms |
| コンパイル使用メモリ | 42,496 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-11 20:47:21 |
| 合計ジャッジ時間 | 1,614 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:9:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
9 | scanf("%d%d",&N,&K);
| ~~~~~^~~~~~~~~~~~~~
main.cpp:15:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
15 | scanf("%d%d",&a,&b),a--,b--;
| ~~~~~^~~~~~~~~~~~~~
ソースコード
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdio>
using namespace std;
int gcd(int x,int y){return y?gcd(y,x%y):x;}
int main(){
int N,K;
scanf("%d%d",&N,&K);
vector<int>shuf(N);
iota(shuf.begin(),shuf.end(),0);
vector<int>orig=shuf;
for(int i=0;i<K;i++){
int a,b;
scanf("%d%d",&a,&b),a--,b--;
swap(shuf[a],shuf[b]);
}
/*
vector<int>v=shuf;
int x=1;
for(;v!=orig;x++){
vector<int>nxt(N);
for(int i=0;i<N;i++)nxt[i]=v[shuf[i]];
v=nxt;
}
printf("%d\n",x);
*/
vector<int>elem(N);
for(int i=0;i<N;i++){
int x=i,c=0;
do{
x=shuf[x];
c++;
}while(x!=i);
elem[i]=c;
}
for(int i=elem.size()-1;i>0;i--){
elem[i-1]=elem[i-1]/gcd(elem[i-1],elem[i])*elem[i];
}
printf("%d\n",elem[0]);
}