結果

問題 No.551 夏休みの思い出(2)
ユーザー snteasntea
提出日時 2017-10-10 01:30:29
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 4,000 ms
コード長 8,315 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 192,496 KB
実行使用メモリ 5,172 KB
最終ジャッジ日時 2023-08-10 21:27:19
合計ジャッジ時間 7,316 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 8 ms
4,380 KB
testcase_04 AC 13 ms
4,376 KB
testcase_05 AC 27 ms
4,380 KB
testcase_06 AC 39 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 86 ms
4,380 KB
testcase_28 AC 84 ms
4,380 KB
testcase_29 AC 80 ms
4,380 KB
testcase_30 AC 84 ms
4,380 KB
testcase_31 AC 78 ms
4,380 KB
testcase_32 AC 85 ms
4,380 KB
testcase_33 AC 84 ms
4,384 KB
testcase_34 AC 82 ms
4,376 KB
testcase_35 AC 80 ms
4,380 KB
testcase_36 AC 82 ms
4,380 KB
testcase_37 AC 93 ms
5,172 KB
testcase_38 AC 91 ms
4,608 KB
testcase_39 AC 85 ms
4,384 KB
testcase_40 AC 90 ms
4,384 KB
testcase_41 AC 85 ms
4,380 KB
testcase_42 AC 91 ms
4,928 KB
testcase_43 AC 87 ms
4,380 KB
testcase_44 AC 87 ms
4,380 KB
testcase_45 AC 86 ms
4,376 KB
testcase_46 AC 94 ms
4,904 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifdef LOCAL111
	#define _GLIBCXX_DEBUG
#else
	#define NDEBUG
#endif
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
const int INF = 1e9;
using namespace std;
template<typename T, typename U> ostream& operator<< (ostream& os, const pair<T,U>& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; }

#define endl '\n'
#define ALL(a)  (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#ifdef LOCAL111
	#define DEBUG(x) cout<<#x<<": "<<(x)<<endl
	template<typename T> void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
#else
	#define DEBUG(x) true
	template<typename T> void dpite(T a, T b){ return; }
#endif
#define F first
#define S second
#define SNP string::npos
#define WRC(hoge) cout << "Case #" << (hoge)+1 << ": "
template<typename T> void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;}
template<typename T> bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;}
template<typename T> bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;}

typedef long long int LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;

void ios_init(){
	//cout.setf(ios::fixed);
	//cout.precision(12);
#ifdef LOCAL111
	return;
#endif
	ios::sync_with_stdio(false); cin.tie(0);	
}

//library

random_device rnd;
mt19937 MT(rnd());

int randInt(int from, int to) {
	uniform_int_distribution<int> rand(from, to - 1);
	return rand(MT);
}

long long randInt(long long from, long long to){
	uniform_int_distribution<long long> rand(from, to-1);
	return rand(MT);
}

using Integer = long long;
const Integer mod = 1e9+7;

template <typename T>
long long gcd(T x, T y){
	return y==0 ? x : gcd(y, x%y);
}


template <typename T>
long long lcm(T x, T y){
	return x/gcd(x,y)*y;
}

//const int MAX_n = ;
//const int MAX_r = ;
/*long long Cdp(int  n, int r){
	if()
}*/

//res.first*a+res.second*b == 1 となるresを返す (a,bは互いに素)
pair<Integer, Integer> extgcd(Integer a,Integer b)
{
	if(b==1){
		return pair<Integer, Integer>(0,1);
	}
	pair<Integer, Integer> t=extgcd(b,a%b);
	return pair<Integer, Integer>(t.second,t.first-a/b*t.second);
}

//modの逆元を返す
Integer inverse(Integer a,Integer modl)
{
	return (extgcd(modl,a).second+modl)%modl;
}

//xCyを返す
long long Cinv(long long x, long long y, const long long modl = mod){
	long long n = 1 ,r = 1;
	for(int i = 0; i < y; i++){
		n = n*(i+1)%modl;
		r = r*(x-i)%modl;
	}
	return r*inverse(n,modl)%modl;
}

long long H(int n, int r){
	return Cinv(n+r-1,r);
}

bool primej(long long x){
	if(x == 0 || x == 1)	return false;
	for(long long i = 2; i*i <= x; i++){
		if(x%i == 0)	return false;
	}
	return true;
}

std::vector<bool> eratosthenes(int n){
	std::vector<bool> res(n+1,true);
	res[0] = false;
	res[1] = false;
	for(long long i = 2; i*i <= n; i++){
		for(long long j = 2; i*j <= n; j++){
			res[i*j] = false;
		}
	}
	return res;
}

template<typename T>
unordered_map<T,int> primeFactorizem(T x){
	unordered_map<T,int> res;
	for(T i = 2; i*i <= x; i++){
		while(x%i == 0){
			res[i]++;
			x /= i;
		}
	}
	if(x != 1) res[x]++;
	return res;
}

template<typename T>
vector<pair<T,int>> primeFactorize(T x){
	vector<pair<T,int>> res;
	for(T i = 2; i*i <= x; i++){
		int cnt = 0;
		while(x%i == 0){
			cnt++;
			x /= i;
		}
		if(cnt != 0) res.emplace_back(i,cnt);
	}
	if(x != 1){
		if(res.size() != 0 and res.back().first == x){
			res.back().first++;
		}else{
			res.emplace_back(x,1);
		}
	}
	return res;
}

Integer modpow(Integer x, Integer y, Integer mod){
	x %= mod;
	y %= mod;
	Integer tmp = x;
	Integer res = 1;
	while(y != 0){
		if(y&1){
			res = res*tmp%mod;
		}
		tmp = tmp*tmp%mod;
		y >>= 1;
	}
	return res;
}


// x = second (mod first) 0以上の最小解
Integer garner_gen(const vector<pair<Integer, Integer>>& ex) {
	int n = ex.size();
	Integer res = 0;
	Integer k = 1;
	for(int i = 0; i < n; ++i) {
		Integer x, m;
		tie(m,x) = ex[i];
		x = (x%m+m)%m;
		Integer g = gcd(k,m);
		if((x-res)%g != 0) return -1;
		Integer inv = inverse(k/g,m/g);
		Integer v = ((x-res)/g%m+m)%m*inv%m;
		Integer nk = k/g*m;
		res = (res+v*k%nk)%nk;
		k = k/g*m;
	}
	return res;
}

Integer garner_gen(const vector<Integer>& x, const vector<Integer>& mod){
	int n = x.size();
	vector<pair<Integer, Integer>> v(n);
	for(int i = 0; i < n; ++i) {
		v[i] = {mod[i],x[i]};
	}
	return garner_gen(v);
}

//あんま検証してないよ
Integer garner(const vector<pair<Integer, Integer>>& ex) {
	int n = ex.size();
	Integer res = 0;
	Integer k = 1;
	for(int i = 0; i < n; ++i) {
		Integer x, m;
		tie(m,x) = ex[i];
		// x = (x%m+m)%m;
		Integer inv = inverse(k,m);
		Integer v = ((x-res)%m+m)%m*inv%m;
		res = (res+v*k);
		k = k*m;
	}
	return res;
}

Integer garner(const vector<Integer>& x, const vector<Integer>& mod){
	int n = x.size();
	vector<pair<Integer, Integer>> v(n);
	for(int i = 0; i < n; ++i) {
		v[i] = {mod[i],x[i]};
	}
	return garner(v);
}

//検証甘い rand.ccに依存
bool isPrime_MillerRabin(Integer x, int k = 20){
	if(x == 1) return false;
	if((x&1) == 0){
		if(x == 2){
			return true;
		}else{
			return false;
		}
	}

	Integer n = x;
	Integer s = 0;
	x--;
	while((x&1) == 0){
		x /= 2;
		s++;
	}
	Integer d = x;
	for(int cnt = 0; cnt < k; ++cnt) {
		Integer a = randInt((Integer)1,n);
		if(modpow(a,d,n) != 1){
			bool f = true;
			Integer num = modpow(a,d,n);
			for(int r = 0; r < s; ++r) {
				if(num == n-1){
					f = false;
					break;
				}
				num = num * num % n;
			}
			if(f) return false;
		}
	}
	return true;
}

//librarys
// solve Discrete Logarithm Problem
// log_r(a) mod p-1
template<typename Integer>
class DLP {
private:
	vector<pair<Integer, int>> bsteps;
	unordered_map<Integer, int> gsteps;
	long long root;
	long long mod;

public:
	DLP(Integer p, Integer r) {
		mod = p;
		root = 1;
		while(root*root <= p) root++;
		Integer rinv = inverse(r, p);
		Integer giant_step = modpow(r, root, p);
		Integer baby_step = rinv;
		bsteps = vector<pair<Integer, int>>(root+1);
		gsteps = unordered_map<Integer, int>(2*root+1);
		Integer g = 1;
		Integer b = 1;
		for(int i = 0; i <= root; ++i) {
			gsteps[g%p] = i;
			bsteps[i] = {b%p, i};
			g *= giant_step;
			g %= p;
			b *= baby_step;
			b %= p;
		}
	}

	Integer baby_step_giant_step(Integer a) {
		// Integer ainv = inverse(a, mod);
		for(int i = 0; i <= root; i++) {
			auto ite = gsteps.find(bsteps[i].first*a%mod);
			if(ite != gsteps.end()) {
				return (ite->second*root+bsteps[i].second)%(mod-1);
			}
		}
		return -1;
	}
};

bool is_quadratic_residue(long long a, long long p) {
	if(a == 0) return true;
	long long power = modpow(a, (p-1)/2, p);
	if(power == p-1) {
		return false;
	} else {
		return true;
	}
}

// cipolla's algorithm
long long mod_sqrt(long long a, long long p) {
	if(a == 0) return 0;
	const long long mod = p;
	random_device rnd;
	mt19937 MT(rnd());
	// mt19937 MT(time());
	uniform_int_distribution<int> rand(0, p - 1);
	long long ex = -1;
	long long b = -1;
	while(true) {
		long long t = rand(MT);
		long long r = (t*t-a)%mod;
		if(r < 0) r += mod;
		if(r == 0) continue;
		if(!is_quadratic_residue(r, p)) {
			ex = r;
			b = t;
			break;
		}
	}
	DEBUG(b); DEBUG(ex);
	assert(ex != -1);
	using P = pair<long long, long long>;
	P res(1,0), bi(b,1);
	function<void(P&,P&)> mul_assign
	= [&](P& x, P& y) {
		long long s,t,u,v;
		tie(s,t) = x;
		tie(u,v) = y;
		x = P((s*u%p+t*v%p*ex%p)%p, (s*v%p+t*u%p)%p);
	};
	long long t = (p+1)/2;
	while(t != 0) {
		if(t&1) {
			mul_assign(res, bi);
		}
		mul_assign(bi, bi);
		t >>= 1;
	}
	assert(res.second == 0);
	return res.first;
}

int main()
{
	ios_init();
	// cout << mod_sqrt(4, 5) << endl;
	LL p, r;
	while(cin >> p >> r) {
		int q;
		cin >> q;
		DLP<LL> dlp(p, r);
		REP(_, q) {
			LL a, b, c;
			cin >> a >> b >> c;
			DEBUG(a); DEBUG(b); DEBUG(c);
			LL s = (b*b%p-4*a*c%p)%p;
			s = (s+p)%p;
			DEBUG(s);
			if(is_quadratic_residue(s, p)) {
				long long root = mod_sqrt(s, p);	
				set<LL> s;
				s.insert(((-b+root)*inverse(2*a, p)%p+p)%p);
				s.insert(((-b-root)*inverse(2*a, p)%p+p)%p);
				pite(ALL(s));
			} else {
				cout << -1 << endl;
			}
		}
	}
	return 0;
}
0