POJ 2891 Strange Way to Express Integers

Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 14791 Accepted: 4849

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 a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) 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 airi (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

中国剩余定理:
由于这道题目里面的ai、ri之间不满足两两互质的性质,所以不能用中国剩余定理直接求解。
不过,我们可以模仿中国剩余定理的做法来解决这个问题。
如果只有一个方程:x mod a0 = r0。那么,显然x的最小正值为a0+r0。
根据模的性质,我们容易得知,x+a0*k均为该方程的解。(k为正整数)
如果多了一个方程:x mod a1 = r1。那么,我们为了使之间求得的解x0=a0+r0能够同时满足这两个方程,只好令x0=x0+a0*k,显然这样做x0仍然满足第一个方程。这时候我 们相当于要求解这样一个模方程:(x0+a0*k) mod a1 = r1。这个方程我们可以用拓展欧几里得算法求得k的值。这样,只要令x0变成x0+a0*k,就能同时满足这两个方程了。
推而广之,对于方程x mod ai = ri,假如我们之前求得的解为X,那么我们要令X变成X+k*LCM(a0,a1,a2...ai-1),使得它满足这个方程。k我们可以用拓展欧几里得 算法求解,LCM可以在每一次更新,这样就能在接近O(klogk)的时间复杂度内解决这个问题了。
无解的判断:若某个(X+k*LCM) mod ai = ri无整数解,那么原方程组无解。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define LL __int64 
using namespace std;
int flag;
LL d,result;
LL e_gcd(LL a,LL b,LL &x,LL &y)  
{  
    if(b==0)  
    {  
        x=1;  
        y=0;  
        return a;  
    }  
    LL ans=e_gcd(b,a%b,x,y);  
    LL temp=x;  
    x=y;  
    y=temp-a/b*y;  
    return ans;  
}  
void fun(LL a,LL b,LL n)
{
   LL x,y;
   d=e_gcd(a,n,x,y);     
   if(b%d!=0)
      flag =1;
   result=(x*(b/d)%n+n)%n;
}
int main()
{
	int k,i,j;
	LL a1,r1,a2,r2;
	while(scanf("%d",&k)!=EOF)
	{
		flag=0;
		scanf("%I64d%I64d",&a1,&r1);
		k--;
		while(k--)
		{
		     scanf("%I64d%I64d",&a2,&r2);
		     fun(a1,r2-r1,a2);
             r1+=a1*result;
             a1=a1*a2/d;
             r1=(r1%a1+a1)%a1;
		}
		if(flag)
		  printf("-1\n");
		else
		  printf("%I64d\n",r1);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值