poj——2891 Strange Way to Express Integers
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 16839 | Accepted: 5625 |
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
题目大意: 有一个数mod ri 等于ai ,求这个数,若求不出来输出“-1”。
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define N 1000 #define ll long long using namespace std; ll n,a[N],m[N]; ll read() { ll x=,f=; char ch=getchar(); ; ch=getchar();} +ch-'; ch=getchar();} return x*f; } ll exgcd(ll a,ll b,ll &x,ll &y) { ) { x=,y=; return a; } ll r=exgcd(b,a%b,x,y),tmp; tmp=x,x=y,y=tmp-a/b*y; return r; } ll crt() { ll a1=a[],m1=m[],a2,m2,c,d; ;i<=n;i++) { m2=m[i],a2=a[i]; c=a2-a1;ll x=,y=; d=exgcd(m1,m2,x,y); ; x=x*c/d; int mod=m2/d; x=(mod+x%mod)%mod; a1+=x*m1;m1*=mod; } ) a1+=m1; return a1; } int main() { while(~scanf("%lld",&n)) { ;i<=n;i++) m[i]=read(),a[i]=read(); printf("%lld\n",crt()); } ; }
poj——2891 Strange Way to Express Integers的更多相关文章
- 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: 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> ...
- POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...
- POJ 2891 Strange Way to Express Integers(中国剩余定理)
题目链接 虽然我不懂... #include <cstdio> #include <cstring> #include <map> #include <cma ...
随机推荐
- Quartz.Net学习笔记(2)-简介
一.Quartz.Net是什么 1.来源 Quartz.Net是一个开源的作业调度框架: 2.下载地址 官网地址:http://www.quartz-scheduler.net/documentati ...
- CSV文件转EXCEl(java)
package main; import java.io.BufferedReader;import java.io.DataInputStream;import java.io.File;impor ...
- 2199. [HZOI 2016] 活动投票
★★ 输入文件:hztp.in 输出文件:hztp.out 简单对比 时间限制:0.5 s 内存限制:2 MB [题目描述] 衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知 ...
- Unity笔记(2)自学第一天
学习记录: 界面使用:
- Digital design之Boolean Algebra
1. 0 and 1 (duality: 0 -- 1, · -- +) X + 0 = X, X · 1 = X X + 1 = 1, X · 0 = 0 2. Idempotent X + X = ...
- Java学习1_一些基础1——16.5.4
每个java程序中都必须有一个main方法,格式为: public class ClassName { public static void main(String[] args) { program ...
- swift extension 的最终目的是结合
与数据结合:对数据进行操作: 与行为结合:使用原有行为生成更便捷的行为: 与协议结合:实现协议: 与类型结合:对类型数据进行操作,添加新的行为: 与关联类型.泛型结合:对类型做出限定.
- swift Enumerations
swift Enumerations enum.case.switch CaseIterable allCases 要区别枚举变量和关联值 枚举变量参与枚举运算: 关联值和rawvalue不参与. A ...
- Jmeter之计数器
如果需要引用的数据量较大,且要求不能重复或者需要自增,那么可以使用计数器来实现. 计数器(counter):允许用户创建一个在线程组之内都可以被引用的计数器. 计数器允许用户配置一个起点,一个最大值, ...
- 梦想CAD控件COM接口文字样式
增加文字样式 用户可以增加文字样式到数据库,并设置其字体等属性,具体实现c#代码如下: private void CreateText() { MxDrawApplication app = new ...