結果

問題 No.187 中華風 (Hard)
ユーザー f1b_maxbl00df1b_maxbl00d
提出日時 2022-06-20 06:18:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 3,000 ms
コード長 5,113 bytes
コンパイル時間 1,480 ms
コンパイル使用メモリ 132,588 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 17:24:40
合計ジャッジ時間 4,521 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 148 ms
5,376 KB
testcase_03 AC 145 ms
5,376 KB
testcase_04 AC 186 ms
5,376 KB
testcase_05 AC 185 ms
5,376 KB
testcase_06 AC 185 ms
5,376 KB
testcase_07 AC 185 ms
5,376 KB
testcase_08 AC 135 ms
5,376 KB
testcase_09 AC 134 ms
5,376 KB
testcase_10 AC 135 ms
5,376 KB
testcase_11 AC 188 ms
5,376 KB
testcase_12 AC 188 ms
5,376 KB
testcase_13 AC 57 ms
5,376 KB
testcase_14 AC 57 ms
5,376 KB
testcase_15 AC 147 ms
5,376 KB
testcase_16 AC 152 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 145 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 186 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#pragma warning(disable:4996)
//#include <Windows.h>
#include <iostream>
#include <vector>
#include <limits.h>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <random>
#include <functional>
#include <stack>
#include <iomanip>
#include <cassert>
//#include <boost/multiprecision/cpp_int.hpp>
#include <complex>
#include <cstdio>
#include <list>
#include <bitset>
//#include <stdio.h>

//< in.txt > out.txt
using namespace std;
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
//typedef boost::multiprecision::cpp_int bigint;
typedef pair<LL, LL> PLL;
typedef pair<int, int> PI;
typedef pair<LD, LL> pdl;
typedef pair<LD, LD> pdd;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef unsigned long long ULL;
template<class T>
using pqueue = priority_queue<T, vector<T>, function<bool(T, T)>>;

template<class T>
inline void chmin(T& a, T b) {
	a = min(a, b);
}

template<class T>
inline void chmax(T& a, T b) {
	a = max(a, b);
}

//y/xのfloorを求める
LL floor_(LL y, LL x) {
	if (x < 0) {
		x *= -1;
		y *= -1;
	}
	if (y >= 0) {
		return y / x;
	}
	else {
		if ((-y) % x == 0) {
			return y / x;
		}
		else {
			return -((-y) / x) - 1;
		}
	}
}

inline LL mod(LL a, LL m) {
	LL res = a % m;
	return res >= 0 ? res : res + m;
}

void input();
void solve();

void daminput();
void naive();

void outputinput();

int main() {
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	cout << fixed << setprecision(12);
	input();
	//daminput();
	solve();
	//naive();
	//outputinput();
	return 0;
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

//最大公約数
//O(log max(a,b))
template<class T>
T GCD(T a, T b) {
	if (b == 0)return a;
	return GCD(b, (T)(a % b));
}

//最大公約数(複数)
//O(nlog max(a_i))?
template<class T>
T GCD(vector<T> v) {
	for (int n = 1; n < v.size(); n++) {
		v[n] = GCD(v[n], v[n - 1]);
	}
	return v[v.size() - 1];
}

//最小公倍数
template<class T>
T LCM(T a, T b) {
	return a * b / GCD(a, b);
}

//最小公倍数(複数)
template<class T>
T LCM(vector<T> v) {
	for (int n = 1; n < v.size(); n++) {
		v[n] = LCM(v[n], v[n - 1]);
	}
	return v[v.size() - 1];
}

//ax+by=gcd(a,b)の解
LL extgcd(LL a, LL b, LL& x, LL& y) {
	LL d = a;
	if (b != 0) {
		d = extgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	else {
		x = 1; y = 0;
	}
	return d;
}

//ax=gcd(a,m) mod mなるxを返す
LL ModInv(LL a, LL m) {
	LL x, y;
	extgcd(a, m, x, y);
	return x;
}

//x = b1 mod m1
//x = b2 mod m2
//なる連立合同式を解く mod lcm(m1,m2)において解が存在するならば(x,lcm(m1,m2))を、存在しないならば(0,-1)を返す
PLL SimCongruence(LL b1, LL m1, LL b2, LL m2) {
	LL p, q;
	LL d = extgcd(m1, m2, p, q);
	if ((b1 - b2) % d != 0) {
		return PLL(0, -1);
	}
	else {
		//?
		LL m = m1 / d * m2;
		LL tmp = (b2 - b1) / d * p % (m2 / d);
		LL r = mod(b1 + m1 * tmp, m);
		return PLL(r, m);
	}
}

//garnerのアルゴリズム x=b[k] mod m[k] (0 < k < K) の解xをmod Mで求める(ただしm[-]は互いに素)
//O(K^2log(max(m[k])))
LL Garner(VLL& b, VLL& m, LL M) {
	int K = m.size();
	//番兵
	m.push_back(M);
	//const[k] := t0+t1m0+...+tkm0m1...m{k-1} mod mk
	VLL con(K + 1, 0);
	//coeff[k] := m0...m{k-1} mod mk
	VLL coeff(K + 1, 1);
	for (int k = 0; k < K; k++) {
		//solve t*coeff[k]=b[k]-const[k] mod mk
		LL t = mod((b[k] - con[k]) * ModInv(coeff[k], m[k]), m[k]);
		for (int i = k + 1; i <= K; i++) {
			con[i] = mod(con[i] + t * coeff[i], m[i]);
			coeff[i] = mod(coeff[i] * m[k], m[i]);
		}
	}
	return con.back();
}

//garnerのアルゴリズムでx=b[k] mod m[k] (0 < k < K) の解xをmod Mで求める際、m[-]が互いに素でないならば実行する
//そもそも解が存在しないならば-1を返す
//O(K^2log(max(m[k])))
LL PreGarner(VLL& b, VLL& m, LL M) {
	LL res = 1;
	int K = b.size();
	for (int i = 0; i < K; i++) {
		for (int j = 0; j < i; j++) {
			LL g = GCD(m[i], m[j]);
			if ((b[i] - b[j]) % g != 0) {
				return -1;
			}
			m[i] /= g;
			m[j] /= g;
			LL gi = GCD(m[i], g);
			LL gj = g / gi;
			do {
				g = GCD(gi, gj);
				gi *= g, gj /= g;
			} while (g != 1);
			m[i] *= gi;
			m[j] *= gj;
			b[i] %= m[i];
			b[j] %= m[j];
		}
	}
	for (int i = 0; i < K; i++) {
		res = mod(res * m[i], M);
	}
	return res;
}

int N;
VLL X, Y;

void input() {
	cin >> N;
	X.resize(N);
	Y.resize(N);
	for (int n = 0; n < N; n++) {
		cin >> X[n] >> Y[n];
	}
}

void daminput() {
}

void solve() {
	LL res = PreGarner(X, Y, MOD);
	if (res == -1) {
		cout << -1 << "\n";
		return;
	}
	bool flag = true;
	for (int n = 0; n < X.size(); n++) {
		if (X[n] != 0) {
			flag = false;
			break;
		}
	}
	LL res2 = Garner(X, Y, MOD);
	if (!flag) {
		cout << res2 << "\n";
	}
	else {
		cout << res << "\n";
	}
}

void naive() {
}

void outputinput() {
}
0