poj2891 扩展中国剩余定理
求a1x1+r1=y...anxn+rn=y,crt合并
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#include<bits/stdc++.h>
#include<cstdio>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=400000+10,maxn=400000+10,inf=0x3f3f3f3f;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=1,y=0;return a;}
ll ans=exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
return ans;
}
int main()
{
ll a1,a2,r1,r2,x,y;int n;
while(~scanf("%d",&n))
{
scanf("%lld%lld",&a1,&r1);
bool f=1;
for(int i=2;i<=n;i++)
{
// printf("%lld\n%lld\n",a1,r1);
scanf("%lld%lld",&a2,&r2);
ll r=gcd(a1,-a2);
if((r2-r1)%r!=0){f=0;continue;}
exgcd(a1,-a2,x,y);
ll p=a2/gcd(a1,a2);
x*=(r2-r1)/r;x=(x%p+p)%p;
r1=a1*x+r1;
a1=a1/gcd(a1,a2)*a2;
r1%=a1;
}
if(!f)puts("-1");
else printf("%lld\n",r1);
}
return 0;
}
/********************
7
11323 9793
5537 4754
21538 10901
16118 203
2082 1209
22929 9510
16541 15898
********************/
poj2891 扩展中国剩余定理的更多相关文章
- [洛谷P4777] [模板] 扩展中国剩余定理
扩展中国剩余定理,EXCRT. 题目传送门 重温一下中国剩余定理. 中国剩余定理常被用来解线性同余方程组: x≡a[1] (mod m[1]) x≡a[2] (mod m[2]) ...... x≡a ...
- 学习笔记 - 中国剩余定理&扩展中国剩余定理
中国剩余定理&扩展中国剩余定理 NOIP考完回机房填坑 ◌ 中国剩余定理 处理一类相较扩展中国剩余定理更特殊的问题: 在这里要求 对于任意i,j(i≠j),gcd(mi,mj)=1 (就是互素 ...
- 2019牛客暑期多校训练营(第十场)Han Xin and His Troops——扩展中国剩余定理
题意 求解 $n$ 个模方程 $x \equiv a (mod \ b)$,不保证模数互素($1 \leq n \leq 100$,$0 \leq b < a< 10^5$). 分析 套扩 ...
- LOJ 2721 「NOI2018」屠龙勇士——扩展中国剩余定理
题目:https://loj.ac/problem/2721 1.注意别一输入 p[ i ] 就 a[ i ] %= p[ i ] ,因为在 multiset 里找的时候还需要真实值. 2.注意用 m ...
- P4774-屠龙勇士-扩展中国剩余定理
屠龙勇士 很久很久以前,巨龙突然出现,带来了灾难带走公主又消失不见.王国十分危险,世间谁最勇敢,一位英雄出现-- 学习于该大佬博客 那么你就是这位英雄,不过不同的是,你面对的是一群巨龙,虽然巨龙都不会 ...
- 2019ICPC徐州网络赛 A.Who is better?——斐波那契博弈&&扩展中国剩余定理
题意 有一堆石子,两个顶尖聪明的人玩游戏,先取者可以取走任意多个,但不能全取完,以后每人取的石子数不能超过上个人的两倍.石子的个数是通过模方程组给出的. 题目链接 分析 斐波那契博弈有结论:当且仅当石 ...
- Wannafly Camp 2020 Day 3I N门问题 - 概率论,扩展中国剩余定理
有一个猜奖者和一个主持人,一共有 \(n\) 扇门,只有一扇门后面有奖,主持人事先知道哪扇门后有奖,而猜奖者不知道.每一轮,猜奖者选择它认为的有奖概率最大(如果有多个最大,随机选一个)的一扇门,主持人 ...
- POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...
- (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)
前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...
随机推荐
- P2517 [HAOI2010]订货
思路 费用流水题 对每月拆点,入点向出点连cap=ui的边,s向入点连cost=di的边,i的入点向i+1的入点连cap=S的边即可 代码 #include <cstdio> #inclu ...
- 论文笔记之:Heterogeneous Face Attribute Estimation: A Deep Multi-Task Learning Approach
Heterogeneous Face Attribute Estimation: A Deep Multi-Task Learning Approach 2017.11.28 Introductio ...
- 深度学习课程笔记(六)Error
深度学习课程笔记(六)Error Variance and Bias: 本文主要是讲解方差和偏差: error 主要来自于这两个方面.有可能是: 高方差,低偏差: 高偏差,低方差: 高方差,高偏差: ...
- [CodeForces - 276A] Lunch Rush
题目链接:http://codeforces.com/problemset/problem/276/A 从这n个输入中求最大值,注意 和 k的比较,定义一个maxn,对每个输入进行计算即可. AC代码 ...
- 4-Five-Youth
①People are always talking about 'the problem of youth'. If there is one--which I take leave to do ...
- React组件导入的两种方式(动态导入组件的实现)
一. react组件两种导入方式 React组件可以通过两种方式导入另一个组件 import(常用) import component from './component' require const ...
- Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图
E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- WebSocket 教程
转载自:http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebSo ...
- R语言网页爬虫
R 是统计计算和数据分析的利器.给定一个数据集,利用前几章介绍到的 R 中灵活的数据结构或高性能计算,我们可以很方便地进行数据转换.建模和数值分析.一般来说,商业数据库会将数据以表格的形式很好地组织起 ...
- 学习笔记35—大话 Word和Excel
1.word中,加粗表格线条:设计---->笔画粗细. 2.Excel中,冻结某一行:点击工具栏中的视图→冻结空格→冻结单元格. 3.word中,输入卡方符号:插入----> 符号 --- ...