結果

問題 No.261 ぐるぐるぐるぐる!あみだくじ!
ユーザー koyumeishikoyumeishi
提出日時 2015-06-30 18:00:47
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,211 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 79,112 KB
最終ジャッジ日時 2024-04-27 02:08:20
合計ジャッジ時間 984 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘std::vector<int> my_pow(std::vector<int>, long long int)’:
main.cpp:112:9: error: ‘iota’ was not declared in this scope
  112 |         iota(ret.begin(), ret.end(), 0);
      |         ^~~~
main.cpp: In function ‘int baby_step_giant_step(std::vector<int>, std::vector<int>, long long int)’:
main.cpp:123:23: error: ‘sqrt’ was not declared in this scope
  123 |         long long H = sqrt(z) + 1;
      |                       ^~~~
main.cpp: In function ‘int main()’:
main.cpp:159:9: error: ‘iota’ was not declared in this scope
  159 |         iota(v.begin(), v.end(), 0);
      |         ^~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include "assert.h"
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <set>

using namespace std;

#define MAX_N 100
#define MAX_K 1000

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

long long lcm(long long a, long long b){
	if(a<b) swap(a,b);
	if(b==1) return a;
	return a* (b/gcd(a,b));
}

class UnionFindTree{
	typedef struct {
		int parent;
		int rank;
	}base_node;
	
	vector<base_node> node;
public:
	UnionFindTree(int n){
		node.resize(n);
		for(int i=0; i<n; i++){
			node[i].parent=i;
			node[i].rank=0;
		}
	}

	int find(int x){
		if(node[x].parent == x) return x;
		else{
			return node[x].parent = find(node[x].parent);
		}
	}
	
	bool same(int x, int y){
		return find(x) == find(y);
	}

	void unite(int x, int y){
		x = find(node[x].parent);
		y = find(node[y].parent);
		if(x==y) return;
		if(node[x].rank < node[y].rank){
			node[x].parent = y;
		}else if(node[x].rank > node[y].rank){
			node[y].parent = x;
		}else{
			node[x].rank++;
			unite(x,y);
		}
	}
};

vector<int> func(vector<int> a, vector<int> b){
	int n = a.size();
	vector<int> ret(n);
	for(int i=0; i<n; i++){
		ret[i] = a[b[i]];
	}

	return ret;
}


int solve_union_find(const vector<int> &v){
	int N = v.size();
	
	UnionFindTree uft(N);
	for(int i=0; i<N; i++){
		uft.unite(i, v[i]);
	}

	map<int,int> forest;
	for(int i=0; i<N; i++){
		int parent = uft.find(i);
		auto itr = forest.find(parent);
		if(itr != forest.end()){
			itr->second += 1;
		}else{
			forest[parent] = 1;
		}
	}
	
	int ans = 1;
	for(auto itr = forest.begin(); itr != forest.end(); itr++){
		int val = itr->second;
		ans = lcm(ans, val);
	}

	return ans;
}

vector<int> my_pow(vector<int> x, long long y){
	int n = x.size();
	vector<int> ret(n);
	iota(ret.begin(), ret.end(), 0);
	while(y){
		if(y&1LL) ret = func(ret, x);
		x = func(x,x);
		y >>= 1LL;
	}
	return ret;
}

//O(sqrt(z) * log(z) / 2)
int baby_step_giant_step(vector<int> x, vector<int> y, long long z){
	long long H = sqrt(z) + 1;
	set< pair<vector<int>, long long> > S;

	vector<int> w = y;
	for(long long i=0; i<H; i++, w=func(w, x)){
		S.insert({w,i});
	}

	long long k = -1;
	vector<int> x_H = my_pow(x, H);
	w = x_H;
	for(long long i=1; i<=H; i++, w = func(w, x_H)){
		auto itr = S.lower_bound({w, 1LL<<60});
		if(itr == S.begin()) continue;

		itr--;
		if(itr->first == w){
			return H * i - itr->second;
		}
	}

	return k;
}


int main(){
	int N;
	cin >> N;
	assert(2<=N && N<=MAX_N);
	
	int K;
	cin >> K;
	assert(0<=K && K<=MAX_K);
	
	
	vector<int> v(N);
	iota(v.begin(), v.end(), 0);
	
	for(int i=0; i<K; i++){
		int x,y;
		cin >> x >> y;
		assert(1<=x && x<= N);
		assert(1<=y && y<= N);
		assert(x<y);
		assert(x+1 == y);
		
		x--;
		y--;
		
		swap( v[x], v[y] );
	}

	int M = solve_union_find(v);

	int Q;
	cin >> Q;
	//assert(1<=Q && Q<=100);
	while(Q--){
		vector<int> B(N);
		for(int i=0; i<N; i++){
			cin >> B[i];
			B[i]--;
		}
		int k = baby_step_giant_step(v, B, M);

		cout << k << endl;
		/*
		vector<int> check(N);
		iota(check.begin(), check.end(), 0);
		sort(B.begin(), B.end());
		assert(check == B);
		*/
	}

	return 0;
}
0