88pts WA 求助,已使用int128
查看原帖
88pts WA 求助,已使用int128
790188
bsdsdb楼主2025/1/3 18:32
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
// using namespace __gnu_pbds;
// tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>T;
using namespace std;
typedef __int128 ll;
typedef pair<ll, ll> pll;
typedef long double ldb;
//#define umap unordered_map
#define umap __gnu_pbds::cc_hash_table
#define mkp make_pair
#define prque priority_queue
#define emp emplace
#define empb emplace_back
#define empf emplace_front
#define invarg invalid_argument
#define cus_throw(msg) throw invarg(string(msg) + " at " + __FILE__ + ":" + to_string(__LINE__))
random_device rndv;
mt19937 rd(rndv());
const ll inf = 0x3f3f3f3f3f3f3f3fll;
const vector<ll> millerrabin = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
const double eps = 1e-8;
inline void enter(){putchar('\n');}
inline void space(){putchar(' ');}
inline ll readll(){ll ret=0,k=1;char c;do{c=getchar();if(c=='-'){k=-1;}}while(('0'>c)|('9'<c));do{ret=(ret<<3)+(ret<<1)+c-48;c=getchar();}while(('0'<=c)&(c<='9'));return ret*k;}
inline void read(ll&x){x=readll();}
inline char readchar(){char ret;do{ret=getchar();}while(ret<=32);return ret;}
inline void read(char&x){x=readchar();}
inline string readstr(){string ret;char c;do{c=getchar();}while(c<=32);do{ret+=c;c=getchar();}while((c>32)&(c!=EOF));return ret;}
inline void read(string&x){x=readstr();}
inline void write(ll x){if(!x){putchar('0');return;}if(x<0){putchar('-');x*=-1;}char op[20]={};ll cur=0;while(x){op[++cur]=x%10+48;x/=10;}while(cur){putchar(op[cur--]);}}
inline ostream& operator<<(ostream& out, __int128 x){if(!x){out<<"0";return out;}if(x<0){out<<"-";x*=-1;}char op[40]={};ll cur=0;while(x){op[++cur]=x%10+48;x/=10;}while(cur){out<<op[cur--];}return out;}
// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

const ll MAXN = 1e5 + 5;

ll n, a[MAXN], m[MAXN];

pll exgcd(ll a, ll b) {
	if (a < 0) {
		pll ret = exgcd(-a, b);
		return mkp(-ret.first, ret.second);
	}
	if (b < 0) {
		pll ret = exgcd(a, -b);
		return mkp(ret.first, -ret.second);
	}
	if (a < b) {
		pll ret = exgcd(b, a);
		return mkp(ret.second, ret.first);
	}
	if (b == 0) {
		return mkp(1, 0);
	}
	ll k = a / b, r = a % b;
	pll ret = exgcd(r, b);
//	(kb+r)x+by=rx+b(y-kx)
	return mkp(ret.first, ret.second - k * ret.first);
}
pll crt(ll a1, ll m1, ll a2, ll m2) {
//	cerr << a1 << ' ' << m1 << ' ' << a2 << ' ' << m2 << endl;
	ll g = __gcd(m1, m2);
	pll eg = exgcd(m1, -m2);
//	cerr << eg.first << ' ' << eg.second << endl;
	eg.first = eg.first * (a2 - a1) / g;
	eg.second = eg.second * (a2 - a1) / g;
	ll lc = m1 * m2 / g;
	ll x = (eg.first * m1 % lc + a1 + lc) % lc;
	return mkp(x, lc);
}

int main() {
	read(n);
	a[0] = 0, m[0] = 1;
	for (ll i = 1; i <= n; ++i) {
		read(a[i]), read(m[i]);
		if (a[i] > m[i]) {
			swap(a[i], m[i]);
		}
		a[i] %= m[i];
		pll tmp = crt(a[i], m[i], a[i - 1], m[i - 1]);
		a[i] = tmp.first, m[i] = tmp.second;
	}
	write(a[n] % m[n]), enter();
	return 0;
}

;             ;;;;;;;;          ;
;                   ;          ; ;
;                  ;          ;   ;
;                 ;          ;     ;
;                ;          ;;;;;;;;;
;               ;          ;         ;
;              ;          ;           ;
;;;;;;;;;;;   ;;;;;;;;   ;             ;

   ;                        ;
  ;                          ;
 ;                            ;
;                              ;
;                              ;
;                              ;
 ;                            ;
  ;         ;;          ;;   ;
   ;        ;;          ;;  ;
2025/1/3 18:32
加载中...