結果

問題 No.187 中華風 (Hard)
ユーザー antaanta
提出日時 2015-04-21 04:28:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 3,000 ms
コード長 4,551 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 82,984 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 02:09:36
合計ジャッジ時間 1,973 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 8 ms
4,376 KB
testcase_03 AC 7 ms
4,376 KB
testcase_04 AC 14 ms
4,380 KB
testcase_05 AC 14 ms
4,376 KB
testcase_06 AC 14 ms
4,376 KB
testcase_07 AC 15 ms
4,376 KB
testcase_08 AC 14 ms
4,380 KB
testcase_09 AC 15 ms
4,376 KB
testcase_10 AC 14 ms
4,376 KB
testcase_11 AC 14 ms
4,380 KB
testcase_12 AC 14 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 14 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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; }

inline void fasterLLDivMod(unsigned long long x, unsigned y, unsigned &out_d, unsigned &out_m) {
	unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
#ifdef __GNUC__
	asm (
		"divl %4; \n\t"
		: "=a" (d), "=d" (m)
		: "d" (xh), "a" (xl), "r" (y)
		);
#else
	__asm {
		mov edx, dword ptr [xh];
		mov eax, dword ptr [xl];
		div dword ptr [y];
		mov dword ptr [d], eax;
		mov dword ptr [m], edx;
	};
#endif
	out_d = d; out_m = m;
}

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];
		fasterLLDivMod(a, y, dummy, rem);
	}
	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;
}
0