結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー cielciel
提出日時 2015-08-01 00:03:07
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 770 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 46,888 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-24 23:50:27
合計ジャッジ時間 2,013 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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]);
	}
	int Q;
	for(scanf("%d",&Q);Q--;){
	vector<int>z(N);
	for(int i=0;i<N;i++)scanf("%d",&z[i]),z[i]--;
	vector<int>elem(N);
	for(int i=0;i<N;i++){
		int x=shuf[i],c=1;
		for(;x!=z[i];c++){
			if(c==N)goto fail;
			x=shuf[x];
		}
		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];
	}
	if(elem[0]<=N){
		printf("%d\n",elem[0]);continue;
	}
	fail:
	puts("-1");
	}
}
0