結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
anta
|
| 提出日時 | 2015-04-21 04:29:28 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 16 ms / 3,000 ms |
| コード長 | 4,093 bytes |
| コンパイル時間 | 752 ms |
| コンパイル使用メモリ | 87,820 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-04 19:01:21 |
| 合計ジャッジ時間 | 1,684 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:168:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
168 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
ソースコード
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
typedef unsigned int u32;
typedef unsigned long long u64;
u32 rem_1(const u32 *x, int n, u32 y) {
u32 rem = 0, dummy;
for(int i = n - 1; i >= 0; -- i) {
u64 a = (u64)rem << 32 | x[i];
rem = a % y;
}
return rem;
}
u32 add(const u32 *x, const u32 *y, int n, u32 *z) {
u32 carry = 0;
for(int i = 0; i < n; ++ i) {
u32 a = x[i], b = y[i];
u64 c = (u64)a + b + carry;
z[i] = (u32)c;
carry = (u32)(c >> 32);
}
return carry;
}
u32 add_1(const u32 *x, int n, u32 y, u32 *z) {
u32 carry = y;
for(int i = 0; i < n; ++ i) {
u64 c = (u64)x[i] + carry;
z[i] = (u32)c;
carry = (u32)(c >> 32);
}
return carry;
}
u32 add(const u32 *x, int xn, const u32 *y, int yn, u32 *z) {
assert(xn >= yn);
u32 carry = add(x, y, yn, z);
return add_1(x + yn, xn - yn, carry, z + yn);
}
u32 mul_1(const u32 *x, int n, u32 y, u32 *z) {
u32 carry = 0;
for(int i = 0; i < n; ++ i) {
u32 a = x[i];
u64 c = (u64)a * y + carry;
z[i] = (u32)c;
carry = (u32)(c >> 32);
}
return carry;
}
struct Int {
vector<u32> v;
Int() { }
Int(u32 x) { if(x != 0) v.assign(1, x); }
int size() const { return v.size(); }
u32 *data() { return v.empty() ? NULL : &v[0]; }
const u32 *data() const { return v.empty() ? NULL : &v[0]; }
void resize(int n) { v.resize(n); }
Int &normalize() { while(!v.empty() && v.back() == 0) v.pop_back(); return *this; }
u32 operator%(u32 y) const {
return rem_1(data(), size(), y);
}
Int &mulAssign(const Int &x, u32 y) {
int sz = x.size();
resize(sz + 1);
v[sz] = mul_1(x.data(), sz, y, data());
return normalize();
}
Int &operator+=(const Int &that) {
if(size() >= that.size()) {
int sz = size(), tsz = that.size();
resize(sz + 1);
v[sz] = add(data(), sz, that.data(), tsz, data());
}else {
int sz = that.size();
resize(sz + 1);
v[sz] = add(data(), that.data(), sz, data());
}
return normalize();
}
void swap(Int &that) {
v.swap(that.v);
}
u64 get64() const {
if(v.empty()) return 0;
if(v.size() == 1) return v[0];
return v[0] | ((u64)v[1] << 32);
}
};
int exgcd(int a, int b, int &g) {
int u = 1, v = 0;
while(b) {
int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
g = a;
return u;
}
bool crt(Int &a1, Int &n1, int a2, int n2, Int &tmp) {
assert(0 <= a2 && a2 < n2);
int b = n1 % n2, t, g;
t = exgcd(b, n2, g);
int n2_g = n2 / g;
int d1 = a1 % n2;
if((a2 - d1) % g != 0) return false;
int d = (a2 - d1) / g;
int h = (long long)d * t % n2_g;
if(h < 0) h += n2_g;
tmp.mulAssign(n1, h);
tmp += a1;
a1.swap(tmp);
n1.mulAssign(n1, n2_g);
return true;
}
int main() {
int N ;
scanf("%d", &N);
Int a, n = 1U, tmp;
bool ok = true, zero = true;
rep(i, N) {
int X, Y;
cin >> X >> Y;
zero &= X == 0;
ok = ok && crt(a, n, X, Y, tmp);
}
if(!ok) {
puts("-1");
}else {
if(zero) a += n;
int ans = a % 1000000007;
cout << ans << endl;
}
return 0;
}
anta