結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー Lepton_sLepton_s
提出日時 2015-08-01 01:22:35
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 100 ms / 5,000 ms
コード長 2,072 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 88,928 KB
実行使用メモリ 31,060 KB
最終ジャッジ日時 2023-09-25 00:02:50
合計ジャッジ時間 2,809 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 2 ms
4,468 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,460 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 6 ms
5,788 KB
testcase_15 AC 6 ms
5,908 KB
testcase_16 AC 6 ms
5,864 KB
testcase_17 AC 9 ms
6,148 KB
testcase_18 AC 3 ms
4,636 KB
testcase_19 AC 3 ms
4,756 KB
testcase_20 AC 4 ms
5,076 KB
testcase_21 AC 4 ms
4,816 KB
testcase_22 AC 4 ms
4,884 KB
testcase_23 AC 3 ms
4,384 KB
testcase_24 AC 3 ms
4,452 KB
testcase_25 AC 4 ms
5,120 KB
testcase_26 AC 4 ms
4,772 KB
testcase_27 AC 13 ms
8,944 KB
testcase_28 AC 9 ms
6,696 KB
testcase_29 AC 8 ms
6,148 KB
testcase_30 AC 26 ms
13,776 KB
testcase_31 AC 20 ms
10,708 KB
testcase_32 AC 19 ms
10,668 KB
testcase_33 AC 18 ms
10,228 KB
testcase_34 AC 14 ms
10,248 KB
testcase_35 AC 15 ms
10,608 KB
testcase_36 AC 43 ms
23,348 KB
testcase_37 AC 90 ms
31,028 KB
testcase_38 AC 100 ms
31,060 KB
testcase_39 AC 46 ms
23,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<25;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long ull;
#define T 12000000
#define R 33000
#define N 200

int n, m, r[N], s[N], s2[N], s3[N], t[R][N], a[N], tmp[N], rr[20], ccnt;
vector<int> t2[R];
set<vector<int> > st;

void ap(int *a, int *b){
	memcpy(tmp, a, sizeof(tmp));
	for(int i = 0; i < n; i++)
		a[i] = tmp[b[i]];
}

int lcm(int a, int b){
	return a/__gcd(a, b)*b;
}

int main(){
	srand(time(NULL));
	cin>>n>>m;
	int rnd1 = rand()%n, rnd2 = rand()%n;
	for(int i = 0; i < n; i++) r[i] = i;
	memcpy(s, r, sizeof(s));
	memcpy(s2, r, sizeof(s));
	memcpy(s3, r, sizeof(s));
	for(int i = 0; i < m; i++){
		int x, y;
		cin>>x>>y;
		swap(s[x-1], s[x]);
	}
	ll E = 1;
	for(int i = 0; i < n; i++){
		int cnt = 1, p = s[i];
		bool small = true;
		while(p!=i){
			if(p>i) small = false;
			p = s[p];
			cnt++;
		}
		if(small){
			rr[ccnt++] = i;
		}
		E = lcm(E, cnt);
	}
	ll F = sqrt(E)+2;
	for(int i = 0; i < F; i++){
		ap(s2, s);
	}
	for(int i = 0; i < F; i++){
		memcpy(t[i], s3, sizeof(s3));
		ap(s3, s2);
	}
	for(int i = 0; i < F; i++){
		t2[i] = vector<int>(n);
		for(int j = 0; j < n; j++) t2[i][j] = t[i][j];
		st.insert(t2[i]);
	}
	int Q;
	cin>>Q;
	while(Q--){
		for(int i = 0; i < n; i++){
			cin>>a[i];
			a[i]--;
		}
		int res = -1;
		vector<int> b(n);
		for(int i = 0; i < F; i++){
			for(int j = 0; j < n; j++) b[j] = a[j];
			if(st.count(b)){
				for(int j = 0; j < F; j++){
					if(!memcmp(a, t[j], sizeof(a))){
						//cerr<<i<<" "<<j<<" "<<F<<endl;
						res = (j*F-i+E-1)%E+1;
						i = j = F;
						break;
					}
				}
			}
			ap(a, s);
		}
		cout<<res<<endl;
	}
	return 0;
}
0