poj 2891 模线性方程组求解
| Time Limit: 1000MS | Memory Limit: 131072K | |
| Total Submissions: 8005 | Accepted: 2378 |
Description
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:
Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
- Line 1: Contains the integer k.
- Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2
8 7
11 9
Sample Output
31
题目大意:k个模线性方程求解
AC代码:
#include <iostream>
using namespace std; __int64 Extended_Euclid(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
__int64 t,d;
if(b==)
{x=;y=;return a;}
d = Extended_Euclid(b,a%b,x,y);
t = x;
x=y;
y=t-(a/b)*y;
return d;
} __int64 CRT(__int64 n)
{
__int64 i,x,y;
__int64 d,c,t,m,b,mm,bb;
int flag=;
scanf("%I64d %I64d",&m,&b);
mm=m;bb=b;
if(n==)
return b%m;
for(i=;i<n;i++)
{
scanf("%I64d %I64d",&m,&b);
if(flag)
{
d=Extended_Euclid(mm,m,x,y);
c=b-bb;
if(c%d)
flag=;
else
{
t=m/d;
bb=mm*((c/d*x%t+t)%t)+bb;
mm=mm*m/d;
}
}
}
if(flag)
return bb%mm;
else
return -;
}
int main()
{
int n;
while(cin>>n)
printf("%I64d\n",CRT(n));
return ;
}
poj 2891 模线性方程组求解的更多相关文章
- POJ 1061 青蛙的约会(拓展欧几里得算法求解模线性方程组详解)
题目链接: BZOJ: https://www.lydsy.com/JudgeOnline/problem.php?id=1477 POJ: https://cn.vjudge.net/problem ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- hihoCoder 1303 数论六·模线性方程组
Description 求解模线性方程组, \(m_i\) 不互质. Sol 扩展欧几里得+中国剩余定理. 首先两两合并跟上篇博文一样. 每次通解就是每次增加两个数的最小公倍数,这对取模任意一个数都是 ...
- hiho一下 第九十七周 数论六·模线性方程组
题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军 ...
- Strange Way to Express Integers (一般模线性方程组)
Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8476 Accepted: 2554 Description Elin ...
- poj 2891 Strange Way to Express Integers (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- 【hihocoder 1303】模线性方程组
[题目链接]:http://hihocoder.com/problemset/problem/1303 [题意] [题解] /* x % m[1] = r[1] x % m[2] = r[2] x = ...
随机推荐
- 什么是Java Marker Interface(标记接口)
先看看什么是标记接口?标记接口有时也叫标签接口(Tag interface),即接口不包含任何方法.在Java里很容易找到标记接口的例子,比如JDK里的Serializable接口就是一个标记接口. ...
- QT 图形视图框架
https://blog.csdn.net/qq769651718/article/details/79357936 使用QPushButton.QLabel.QCheckBox等构成GUI的控件或自 ...
- python之路——递归函数
阅读目录 楔子 初识递归 再谈递归 递归函数与三级菜单 递归函数与二分查找算法 楔子 在讲今天的内容之前,我们先来讲一个故事,讲的什么呢?从前有座山,山里有座庙,庙里有个老和尚讲故事,讲的什么呢?从前 ...
- js和JQuery中的获取宽、高、位置等方法整理
1.获取当前窗口宽度区别(需要注意的是用的window还是document)JQuery:console.log($(window).width()); //获取窗口可视区域的宽度 console.l ...
- 洛谷 P2370 P2370 yyy2015c01的U盘
https://www.luogu.org/problemnew/show/P2370 二分+背包 #include <algorithm> #include <iostream&g ...
- PAT 乙级 1005
题目 题目地址:PAT 乙级 1005 题解 本题主要就在于将一个数的一系列计算结果不重复地存储起来并便于检索,考虑到STL中的集合有相似的特性,使用set可以有效地简化代码和运算. 过程如下: (初 ...
- 几种优化web页面加载速度的策略
剥离静态资源请求到CDN 一般在主域名下的HTTP请求里都会携带大量Cookie信息,最大4KB,每个域名下最多50条:但如果仅仅访问js/css/jpeg等静态资源文件的话是不需要Cookie信息, ...
- 【动态规划】luoguP1941 飞扬的小鸟
细节总是打挂选手:) 题目描述 Flappy Bird是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小鸟顺利通过画面右方的管道缝隙.如果小鸟一不小心撞到了水管 ...
- mysql查询数据包太大:max_allowed_packet
查询大小: show variables like '%max_allowed_packet%'; 修改大小: set global max_allowed_packet = 1024 * 1024 ...
- 哪些 Python 库让你相见恨晚?
知乎用户,A European Swallow. 苇叶.Aran He.jerry等人赞同 补充三个有助于自动化日常工作的: sh:sh 1.08 — sh v1.08 documentation可以 ...