POJ——T 2891 Strange Way to Express Integers
http://poj.org/problem?id=2891
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 16849 | Accepted: 5630 |
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
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
敲模板磨时间、、
- #include <algorithm>
- #include <cstdio>
- using namespace std;
- #define LL long long
- const int N();
- LL n,a[N],m[N];
- LL exgcd(LL a,LL b,LL &x,LL &y)
- {
- if(!b)
- {
- x=; y=;
- return a;
- }
- LL ret=exgcd(b,a%b,x,y),tmp=x;
- x=y; y=tmp-a/b*y;
- return ret;
- }
- LL CRT()
- {
- LL ret=m[],tot=a[];
- for(int i=;i<=n;i++)
- {
- LL x,y,tt=a[i],c=m[i]-ret;
- LL gcd=exgcd(tot,tt,x,y);
- if(c%gcd) return -;
- x=x*c/gcd;
- LL mod=tt/gcd;
- x=(x%mod+mod)%mod;
- ret+=x*tot; tot*=mod;
- }
- if(!ret) ret+=tot;
- return ret;
- }
- int main()
- {
- for(;~scanf("%lld",&n);)
- {
- for(int i=;i<=n;i++)
- scanf("%lld%lld",a+i,m+i);
- printf("%lld\n",CRT());
- }
- return ;
- }
POJ——T 2891 Strange Way to Express Integers的更多相关文章
- 【POJ】2891 Strange Way to Express Integers
http://poj.org/problem?id=2891 题意:求最小的$x$使得$x \equiv r_i \pmod{ a_i }$. #include <cstdio> #inc ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
随机推荐
- node使用express命令报错找不到ejs的解决方法
首先确定已经全局安装过好几遍express和express-generator,但一使用express命令直接报找不到ejs模块,全局和本地安装ejs都没用,nodemon模块报同样错误,找不到deb ...
- Oracle基础入门(三)
一:PLsql一些基本操作 调节plsql的字体大小 二:创建表,如果学过sql server的数据库就会发现其实Oracle跟的一些新建表和新增修改其实是差不多的 新建表 Create table ...
- [Python] Scatter Plot for daily return
Sploe = 2: means that SPY move up 1, ABC move up 2 Correlation: how close those dots close to the li ...
- Go 语言编程
[课程名称]Go 语言编程 [课程时间]2014年7月30日(周三) 20:50 - 22:00 [课程安排]20:50-21:00 通过邮件地址登录网络课堂 ...
- 《R实战》读书笔记一
你仅仅要想处理数据,<R实战>这本书就能够助你一臂之力. <R实战>的目标是让你认识R,而且可以对数据进行操作.可视化和理解.该书包含4部分16个章节8个附录. 第一部分:入门 ...
- 融云消息接口apicloud
融云提供消息发送服务,支持个人消息,群消息,讨论组,聊天室消息, 以下是它涉及到的接口. 初始化,连接之后,可以使用. <!DOCTYPE html> <html> <h ...
- Google Summer of Code 2017 经验谈
Google Summer of Code (GSoC) 2018 又要开始了. 如果想实现你心中的开源梦想, 用代码让世界变得更美好. 参加GSoC可能是你进入开源的世界最好途径. GSoC是什么 ...
- Django 框架篇(四) : 视图(view)详解 以及 路由系统(url)
|--Django的View(视图) |-- CBV和FBV: |-- 给视图增加装饰器: |-- request对象: |-- response对象: |-- Django的路由系统(url): | ...
- tensorflow学习之路---简单的代码
import numpyimport tensorflow as tf #自己创建的数据x_data = numpy.random.rand(100).astype(numpy.float32)#创建 ...
- jQuery 判断是否包含在数组中 jQuery.inArray()
var arr = [ "mysql", "php", "css", "js" ]; $.inArray(" ...