POJ--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
void exgcd(int a,int b,int * x,int *y,int * d)
{
if(b==)
{
x=,y=,d=a;
}
else
{
exgcd(b,a%b,x,y,d);
int temp=x;
x=y,y=temp-a/b*y;
}
}
解一元多项式时的代码:
int sove()
{
scanf("%d%d",&a1,&r1);
for(i=;i<n;i++)
{
scanf("%d%d",&a2,r2);
a=a1,b=a2,c=r2-r1;
exgcd(a,b,x,y,d);
if(c%d!=)
{
ifhave=;
}
int t=b/d;
x=(x*(c/d)%t+t)%t;
r1=r1+x*a1;
a1=a1*(a2/d);
}
if(!ifhave)
{
r1=-;
}
return r1; //即为解的个数
}
#include<stdio.h>
#define LL long long
LL x,y,q;
void exgcd(LL a,LL b)
{
if(b==)
{
x=,y=,q=a;
}
else
{
exgcd(b,a%b);
LL temp=x;
x=y,y=temp-a/b*y;
}
} int main()
{
LL a1,r1,a2,r2,n,i;
bool ifhave;
while(scanf("%I64d",&n)!=EOF)
{ scanf("%I64d%I64d",&a1,&r1);
ifhave=true;
for(i=;i<n;i++)
{
scanf("%I64d%I64d",&a2,&r2);
exgcd(a1,a2);
if((r2-r1)%q)
{
ifhave=false;
}
LL t=a2/q;
x=(x*((r2-r1)/q)%t+t)%t;
r1+=a1*x;
a1*=(a2/q);
}
if(!ifhave) r1=-;
printf("%I64d\n",r1);
}
return ;
}
POJ--Strange Way to Express Integers的更多相关文章
- poj Strange Way to Express Integers 中国剩余定理
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 8193 ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj 2981 Strange Way to Express Integers (中国剩余定理不互质)
http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 13 ...
- 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 ...
- Strange Way to Express Integers(中国剩余定理+不互质)
Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- Strange Way to Express Integers
I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- 数论F - Strange Way to Express Integers(不互素的的中国剩余定理)
F - Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format: ...
随机推荐
- 第二章 ActionScript 3.0学习之画星星(鼠标及键盘事件)
今天觉得学到的比较有趣,所以记录之......~~~ 下面这段就是画出星星的代码:StarShape.as package { import flash.display.Shape; import f ...
- python opencv 人脸识别
def findface(image): import cv2 frame=cv2.imread('n1.jpg') classifier=cv2.CascadeClassifier("h ...
- 如何记录linux终端下的操作日志
如何记录linux终端下的操作日志 在linux终端下,为方便检查操作中可能出现的错误,以及避免屏幕滚屏的限制,我们可以把操作日志记录下来.常用的工具有 screen,script,以及tee等,通过 ...
- SQL调用WebService接口
今天在做一个非常奇葩的东西.中间有个过程要在SQL触发器里面调用webservice接口.呵呵~ ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFT ...
- OpenCV学习(26) 直方图(3)
本章中我们学习一下通过backproject直方图,得到一副图像中每个像素属于该直方图的概率.在下边原始图中(左图),我们框选了一块四边形的区域,计算该区域的灰度直方图,然后通过下面的函数calcBa ...
- typedef的用法和相关问题
用了C和C++这么久,今天才仔细研究了下typedef的用法,真的是惭愧啊,不过基础都是不断巩固的啊. typedef 在计算机编程语言中用来为复杂的声明定义简单的别名,与宏定义有些差异.它本身是一种 ...
- yeoman-angular-gulp
1.安装 yeoman npm install -g yo gulp bower 2.安装genarate-angular //npm install generator-angular npm in ...
- 反汇编基本原理与x86指令构造
反汇编基本原理与x86指令构造 概要:旨在讲述程序的二进制代码转换到汇编.即反汇编的基本原理.以及 x86 架构的 CPU 的指令构造,有这个基础后就能够自己编写汇编程序了,也能够将二进制代码数据转换 ...
- 【Python】得到当前日期时间
import time str=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) print(str)
- ZH奶酪:PHP中添加HTML代码的三种方法
php中添加HTML代码,就是php类型的文件中添加html代码~ 第一种是在HTML中加PHP. 大段大段的html代码中,在各个需要执行php的地方<?php .... ?> 比如 l ...