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: ...
随机推荐
- 物联网(IoT)的11大云平台:AWS、Azure、谷歌云、Oracle、
物联网(IoT)的11大云平台:AWS.Azure.谷歌云.Oracle. 2018-11-06 14:02 云技术 关键词:物联网AzureGoogleSalesforce云计算 导读:现在,我们将 ...
- 从win10家庭版/中文版升级到win10专业版
发布时间:2015-8-4 很多同学在不了解win10系统版本的情况下,安装了win10家庭版/中文版,不管是win10家庭版,还是win10家庭中文版,或者是win10家庭单语言版,它们都是 ...
- 【BZOJ】【1038】【ZJOI2008】瞭望塔
计算几何/半平面交 说是半平面交,实际上只是维护了个下凸壳而已……同1007水平可见直线 对于每条线段,能看到这条线段的点都在这条线段的“上方”,那么对所有n-1条线段求一个可视区域的交,就是求一个半 ...
- Android循环ViewPager(二)
上午没事写了一篇,下午有事,晚上回来看看感觉写的差点意思,上篇文章大概的关于循环是自己添加了两个空的View,看到网上还有一种就是在自定义的Adapter中getCount中返回最大值,然后destr ...
- c#写扩展方法
学习MVC时,学会了写扩展方法,用起来很方便. 01 using System; 02 using System.Collections.Generic; 03 using System.Linq; ...
- MySQL有关1042 Can’t get hostname for your address的问题分析解决过程
[Comment 1] 前同事企鹅上面说他安装的mysql 5.5,发现用mysql客户端远程连接的时候,报1042-Can’t get hostname for your address错误,但是 ...
- [Angular-Scaled web] 2. Architecture sub-modules
Common models will be a sub models for category and bookmarks. Because they are used everywhere. For ...
- [Javascript] Create Objects
var vehicle1 = {type: "Motorboat", capacity: 6, storedAt: "Ammunition Depot"}; v ...
- java编程思想---对象
一.对象 对于每种语言来说,都有自己操纵内存中元素的方法. 在java中,一切被视为对象.可是操纵对象的是一个"引用".举个样例,能够比作为遥控器对电视的操作,遥控器就是引用,而电 ...
- 反汇编基本原理与x86指令构造
反汇编基本原理与x86指令构造 概要:旨在讲述程序的二进制代码转换到汇编.即反汇编的基本原理.以及 x86 架构的 CPU 的指令构造,有这个基础后就能够自己编写汇编程序了,也能够将二进制代码数据转换 ...