POJ--Strange Way to Express Integers
Strange Way to Express Integers
Time Limit: 1000MS |
Memory Limit: 131072K |
|
---|---|---|
Total Submissions: 8370 |
Accepted: 2508 |
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.
Source
POJ Monthly--2006.07.30, Static
同余问题:
关于欧几里得扩展的代码模板:
1 void exgcd(int a,int b,int * x,int *y,int * d)
2 {
3 if(b==0)
4 {
5 x=1,y=0,d=a;
6 }
7 else
8 {
9 exgcd(b,a%b,x,y,d);
10 int temp=x;
11 x=y,y=temp-a/b*y;
12 }
13 }
解一元多项式时的代码:
1 int sove()
2 {
3 scanf("%d%d",&a1,&r1);
4 for(i=0;i<n;i++)
5 {
6 scanf("%d%d",&a2,r2);
7 a=a1,b=a2,c=r2-r1;
8 exgcd(a,b,x,y,d);
9 if(c%d!=0)
10 {
11 ifhave=0;
12 }
13 int t=b/d;
14 x=(x*(c/d)%t+t)%t;
15 r1=r1+x*a1;
16 a1=a1*(a2/d);
17 }
18 if(!ifhave)
19 {
20 r1=-1;
21 }
22 return r1; //即为解的个数
23 }
代码:
1 #include<stdio.h>
2 #define LL long long
3 LL x,y,q;
4 void exgcd(LL a,LL b)
5 {
6 if(b==0)
7 {
8 x=1,y=0,q=a;
9 }
10 else
11 {
12 exgcd(b,a%b);
13 LL temp=x;
14 x=y,y=temp-a/b*y;
15 }
16 }
17
18 int main()
19 {
20 LL a1,r1,a2,r2,n,i;
21 bool ifhave;
22 while(scanf("%I64d",&n)!=EOF)
23 {
24
25 scanf("%I64d%I64d",&a1,&r1);
26 ifhave=true;
27 for(i=1;i<n;i++)
28 {
29 scanf("%I64d%I64d",&a2,&r2);
30 exgcd(a1,a2);
31 if((r2-r1)%q)
32 {
33 ifhave=false;
34 }
35 LL t=a2/q;
36 x=(x*((r2-r1)/q)%t+t)%t;
37 r1+=a1*x;
38 a1*=(a2/q);
39 }
40 if(!ifhave) r1=-1;
41 printf("%I64dn",r1);
42 }
43 return 0;
44 }
- 《微信小程序七日谈》- 第四天:页面路径最多五层?导航可以这么玩
- 《微信小程序七日谈》- 第五天:你可能要在登录功能上花费大力气
- 《微信小程序七日谈》- 第六天:小程序devtool隐藏的秘密
- boi剖析 - 基于webpack的css sprites实现方案
- 深入JDK源码之ThreadLocal类
- 独家 | 一文读懂TensorFlow基础
- Webpack中hash与chunkhash的区别,以及js与css的hash指纹解耦方案
- RPC原理及实现
- RMI原理及实现
- webpack多页面开发与懒加载hash解决方案
- 前后端分离和模块化-58到家微信首页重构之路
- Node.js建站笔记-使用react和react-router取代Backbone
- 协同过滤推荐算法Java代码实现
- Nginx模块之Filter解析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用ansible部署DNS主从(ubuntu)
- “一瓶一证”防伪溯源,腾讯安全领御与百年张裕达成战略合作
- ubuntu离线安装python环境
- ubuntu下解决Unment dependencies问题
- 【Vulnhub】Literally Vulnerable
- 内核通信之 Netlink 源码分析和实例分析
- supervisor简单使用
- 干货!Python常用数据类型的基本操作(长文系列第一篇)
- JAVA注意事项
- python3实现邮件的发送
- C++ 学习笔记
- python基础—pathlib模块
- python基础—dict
- python基础—datetime模块
- Docker容器的"单进程模型"