poj Strange Way to Express Integers 中国剩余定理
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 8193 | Accepted: 2448 |
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.
要考虑0的情况,注意输入,,看清题意
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; __int64 Ex_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
if(b==)
{
x=;
y=;
return a;
}
__int64 g=Ex_gcd(b,a%b,x,y);
__int64 hxl=x-(a/b)*y;
x=y;
y=hxl;
return g;
} __int64 gcd(__int64 a,__int64 b)
{
if(b==)
return a;
return gcd(b,a%b);
} int main()
{
__int64 k,m1,m2,r1,r2,x,y,t,i,d,c;
__int64 sum1=,sum2;
bool flag;
while(scanf("%I64d",&k)>)
{
scanf("%I64d%I64d",&m1,&r1);
sum1=m1;sum2=m1;
flag=false;
for(i=;i<=k;i++)
{
scanf("%I64d%I64d",&m2,&r2); if(flag==true) continue;
sum1=sum1*m2;
sum2=gcd(sum2,m2);
d=Ex_gcd(m1,m2,x,y);
c=r2-r1;
if(c%d)
{
flag=true;
continue;
}
x=c/d*x;
t=m2/d;
x=(x%t +t)%t;
r1=m1*x+r1;
m1=m1*m2/d;
}
if(flag==true)
{
printf("-1\n");
continue;
}
if(r1==)
{
r1=sum1/sum2;
}
printf("%I64d\n",r1);
}
return ;
}
poj Strange Way to Express Integers 中国剩余定理的更多相关文章
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- Day7 - E - Strange Way to Express Integers POJ - 2891
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- 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 (扩展gcd)
题目链接 题意:给k对数,每对ai, ri.求一个最小的m值,令m%ai = ri; 分析:由于ai并不是两两互质的, 所以不能用中国剩余定理. 只能两个两个的求. a1*x+r1=m=a2*y+r2 ...
- POJ.2891.Strange Way to Express Integers(扩展CRT)
题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...
随机推荐
- 《快学Scala》第六章 对象 第七章 包和引入
- C++基础知识-inline、const、mutable、this、static
一.在类定义中实现成员函数inline 类内的成员函实现其实也叫作类内的成员函数定义. 这种直接在类的定义中实现的函数,会被当做inline内联函数来处理. 二.成员函数末尾的const const: ...
- windows下vim中文乱码处理
现象:gvim安装后,打开中文utf-8编码的文件中文显示乱码 处理:1.启动gvim8.0,菜单 ”编辑“->"启动设定"在文件最开始处添加如下两行set fileenco ...
- thinkphp5.0 微信扫码支付模式二
仅供个人参考,方便大家. 一.1)https://pay.weixin.qq.com/index.php/core/home/login 复制此地址 打开微信商户平台. 2)下载安全操作证书(最好在 ...
- 洛谷 P2015 二叉苹果树 (树上背包)
洛谷 P2015 二叉苹果树 (树上背包) 一道树形DP,本来因为是二叉,其实不需要用树上背包来干(其实即使是多叉也可以多叉转二叉),但是最近都刷树上背包的题,所以用了树上背包. 首先,定义状态\(d ...
- (转)Python 运算符
原文:https://blog.csdn.net/liang19890820/article/details/69690954 简述 在 Python 中,运算符是执行算术或逻辑运算的特殊符号,操作的 ...
- ACM练习网站
1.http://www.acmerblog.com/ Acm之家 2.http://acm.nyist.net/JudgeOnline/problemset.php 南阳理工学院
- JDBC处理Transaction
package com.ayang.jdbc; import java.sql.*; /** * transaction的构成,随便写一句insenrt,一执行executeUpdate(),它自动提 ...
- WPF中使用Grid来实现图层的效果
<Style x:Key="ScanButtonStyle" TargetType="{x:Type Button}"> <!--<Se ...
- Bootboxjs快速制作Bootstrap的弹出框效果
Bootboxjs是一个简单的js库,简单快捷帮你制作一个Bootstrap的弹出框效果. 一.简介 bootbox.js是一个小的JavaScript库,它帮助您在使用bootstrap框架的时候快 ...