結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー 沙耶花沙耶花
提出日時 2022-03-25 17:19:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 962 bytes
コンパイル時間 4,029 ms
コンパイル使用メモリ 251,868 KB
最終ジャッジ日時 2025-01-28 11:21:17
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

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