結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー 沙耶花沙耶花
提出日時 2022-03-25 17:19:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 962 bytes
コンパイル時間 3,901 ms
コンパイル使用メモリ 254,892 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 03:06:59
合計ジャッジ時間 5,096 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define Inf 1000000001


int main(){
	
	int N,K;
	cin>>N>>K;
	vector<int> a(N);
	rep(i,N)a[i] = i;
	rep(i,K){
		int x,y;cin>>x>>y;
		swap(a[x-1],a[y-1]);
	}
	
	int q;
	cin>>q;
	
	rep(_,q){
		vector<int> b(N);
		rep(i,N){
			cin>>b[i];
			b[i]--;
		}
		vector<long long> m,r;
		bool F = true;
		rep(i,N){
			int cc = i;
			int M=0,R=-1;
			rep(j,10000){
				cc = a[cc];
				if(cc==b[i]){
					R = j+1;
				}
				if(cc==i){
					M = j+1;
					break;
				}
			}
			if(R==-1){
				F = false;
			}
			else{
				m.push_back(M);
				r.push_back(R%M);
			}
		}
		if(!F){
			cout<<-1<<endl;
			continue;
		}
		auto ans = crt(r,m);
		if(ans.first==0&&ans.second==0)cout<<-1<<endl;
		else{
			long long P = ans.first;
			if(P==0)P += ans.second;
			cout<<P<<endl;
		}
	}
	
	
	
	return 0;
}
0