結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
f1b_maxbl00d
|
| 提出日時 | 2022-06-20 06:15:00 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,126 bytes |
| コンパイル時間 | 1,319 ms |
| コンパイル使用メモリ | 128,940 KB |
| 最終ジャッジ日時 | 2025-01-29 23:27:36 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 5 WA * 20 |
ソースコード
//#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 << Garner(X, Y, MOD) << "\n";
}
else {
cout << res << "\n";
}
}
void naive() {
}
void outputinput() {
}
f1b_maxbl00d