Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 19509   Accepted: 6592

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 ≤ ik) 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 ≤ ik).

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

  1. 2
  2. 8 7
  3. 11 9

Sample Output

  1. 31

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

模不互素不能用CRT,所有就有了我解线性方程组的方法?

但是有个关键步骤不太懂。。只是会用这个板子。。

  1. #include <cstdlib>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include<iostream>
  6. #include <cmath>
  7. #include<string>
  8. #define ll long long
  9. #define dscan(a) scanf("%d",&a)
  10. #define mem(a,b) memset(a,b,sizeof a)
  11. using namespace std;
  12. #define MAXL 1105
  13. #define Endl endl
  14. #define maxn 1000005
  15. ll x,y;
  16. inline ll read()
  17. {
  18. ll x=,f=;char ch=getchar();
  19. while(ch<''||ch>'') {if(ch=='-') f=-;ch=getchar();}
  20. while(ch>=''&&ch<='') {x=*x+ch-'';ch=getchar();}
  21. return x*f;
  22. }
  23. ll exgcd(ll a,ll b,ll &x,ll &y){
  24. if(b==) {
  25. x=;y=;return a;
  26. }
  27. ll d=exgcd(b,a%b,x,y);
  28. ll temp=x;
  29. x=y;
  30. y=temp-a/b*y;
  31. return d;
  32. }
  33. int main()
  34. {
  35. ll k,a,b,c,d;
  36. while(~scanf("%lld",&k))
  37. {
  38. cin>>a>>b;
  39. //cout<<"hhh"<<Endl;
  40. int flag=;
  41. for(ll i=;i<k;++i)
  42. {
  43. c=read();d=read();
  44. if(flag) continue;
  45. ll r=d-b;
  46. ll hhh=exgcd(a,c,x,y);
  47. if(r%hhh) {flag=;continue;}
  48. ll tmp=c/hhh;
  49. x=((r/hhh*x)%tmp+tmp)%tmp;//不理解
  50. b=a*x+b;
  51. a=a*c/hhh;
  52. }
  53. if(flag) cout<<"-1"<<endl;
  54. else cout<<b<<endl;
  55. }
  56. }

POJ 2891的更多相关文章

  1. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  2. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  3. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  4. 【POJ 2891】 Strange Way to Express Integers

    [题目链接] http://poj.org/problem?id=2891 [算法] exgcd [代码] #include <algorithm> #include <bitset ...

  5. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  6. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  7. POJ 2891 中国剩余定理(不互素)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 17877 ...

  8. 【POJ 2891】Strange Way to Express Integers(一元线性同余方程组求解)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  9. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  10. 一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)

    2891 -- Strange Way to Express Integers import java.math.BigInteger; import java.util.Scanner; publi ...

随机推荐

  1. 2019 ACM-ICPC全国邀请赛(西安) M.Travel 二分+判联通

    https://nanti.jisuanke.com/t/39280 讲道理这题写bfs求最大边权限制下从1到n的最短步数,然后二分判一下就行了. 然鹅我还是直接套了dij,一开始纠结dij能不能过, ...

  2. DNS服务初步搭建

    一.准备DNS服务环境 选择 bind dns服务软件包 直接yum安装 bind 和 bind-utils 工具包,测试机器安装bind-utils测试工具包. 服务程序名为 named 二.配置D ...

  3. .net core IdentityServer4 使用query参数

    基本用法请参考官方文档:https://identityserver4.readthedocs.io/en/latest/index.html 这里不对具体用法进行说明,一般情况下,Startup添加 ...

  4. tcl之基本语法—2

  5. SQL Server中的日期,时间组合查询

    如图所示,Jdate和Jdate2是两个分开的字段,一个是date类型,存储日期,一个是time(0)类型,存储具体时间 现在有这样的要求,就是获得(Jdate和Jdate2组合起来的日期时间)在(当 ...

  6. Java 关键字volatile 与 synchronized 作用与区别

     1,volatile 它所修饰的变量不保留拷贝,直接访问主内存中的.    在Java内存模型中,有main memory,每个线程也有自己的memory (例如寄存器).为了性能,一个线程会在自己 ...

  7. 校内考试之zay与银临(day1)

    T1大美江湖(洛谷P5006) zayの题解: 这个题的本质是模拟 不过有卡ceil的地方 ceil是对一个double进行向上取整,而对于int/int来说,返回值是int 举个生动的栗子 ceil ...

  8. Docker背后的内核知识(一)

    Docker背后的内核知识 当谈论Docker时,常常会聊到Docker的实现方式.很多开发者都知道,Docker容器本质上是宿主机上的进程.Docker通过namespace实现了资源隔离.通过cg ...

  9. 连续小波变换(CWT)

    整理下时频分析变换的方法,遇见好的文章就记录下来了,本篇博客参考知乎https://www.zhihu.com/topic/19621077/top-answers上的一个回答,自己手敲一遍,增强记忆 ...

  10. Linux程序编辑器

    重点回顾:Linux底下的配置文件多为文本文件,故使用vim即可进行设定编辑: vim可视为程序编辑器,可用以编辑shell script,配置文件等,避免打错字 vi为所有unix like的操作系 ...