結果

問題 No.101 ぐるぐる!あみだくじ!
ユーザー ey429ey429
提出日時 2015-03-05 13:24:48
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 658 bytes
コンパイル時間 122 ms
コンパイル使用メモリ 23,552 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 08:17:23
合計ジャッジ時間 1,399 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:21:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 |         scanf("%d %d",&n,&k);
      |         ~~~~~^~~~~~~~~~~~~~~
main.cpp:28:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |                 scanf("%d %d",&a,&b);
      |                 ~~~~~^~~~~~~~~~~~~~~

ソースコード

diff #

#include<stdio.h>
int s[100];
bool used[100];

void swap(int &a,int &b){
	int p=a;a=b;b=p;
}

void swapL(long long &a,long long &b){
	long long p=a;a=b;b=p;
}

long long gcd(long long a,long long b){
	if(a<b)swapL(a,b);
	if(a%b==0)return b;
	return gcd(b,a%b);
}

int main(){
	int n,k,i;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++){
		s[i]=i;
		used[i]=false;
	}
	for(i=0;i<k;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		swap(s[a],s[b]);
	}
	long long ans=1;
	for(i=0;i<n;i++)if(!used[i]){
		int p=i;
		int t=0;
		while(!used[p]){
			used[p]=true;
			t++;
			p=s[p];
		}
		long long d=gcd(ans,t);
		ans=ans*(t/d);
	}
	printf("%lld\n",ans);
	return 0;
}
0