同余模方程--BIT 1053 Han Xin Count Soldiers

原创 2013年12月05日 15:49:39

用了2天多,尼玛的,幸好有ZY大神讲解及 对代码调试助攻。。自己程序太渣,还是上ZY代码吧。。

Han Xin Count Soldiers

时间限制: 1秒  内存限制: 64M

Description

In ancient China, there is a great general named Han Xin. One day he wanted to know how many soldiers he had. So he asked his soldiers to stand in one line for every 3 soldiers, and there are 2 left. Then he asked his soldiers to stand in one line for every 5 soldiers, and there are 3 left. At last he asked them to stand in one line for every 7 soldiers, then there are 2 left. After that he knew he had 1073 soldiers! But now there are a lot of soldiers in front of Han Xin, and he wants to know the number again. He has asked his soldiers stand in lines for n (1 <= n <= 100) rounds. And in the ith round, he asked his soldiers stand in one line for every pi (2 <= pi <= 1000) soldiers, and there is ri (0 <= ri < pi) left. Now can you told Han Xin how many soldiers he have ?

Input

The input has several test cases. For each test case, there is one integer n (1 <= n <= 1000) in the first line. And then there n lines, each line has two integers pi (2 <= pi <= 1000) and ri (0 <= ri < pi). The input is ended by EOF.

Output

For each test case, please output the number of soldiers Han Xin had. If there are more than one solution, please output the smallest positive one. And if there is no solution, please output -1 instead.

Sample Input

3

3 2

5 3

7 2

2

4 1

6 3

Sample Output

23

9


有两组的话,设为a1,b1,a2,b2.

可得:x≡b1(mod a1);    ---   x=b1+a1*y1;

x≡b2(mod a2);    ---   x=b2+a2*y2;

得:b1+a1*y1 = b2 (mod a2);

      a1*y1 = (b2-b1) (mod a2);

      y1 = (b2-b1)*(a1对a2 的逆元)  (mod a2);   //求a1逆元时,a1和a2可以约分,整个方程需要约分。

y1求出后,即可得出一个针对  前两个方程的解,x0;

然后的 x≡x0 (mod lcm(a1,a2));

这样 ,两个方程可以变为一个方程了。。

然后 n 个方程就能变为一个方程了。。


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define ll long long
ll a1,b1,a2,b2;
ll x,y,flag;
ll gcd(ll a,ll b){
	return b?gcd(b,a%b):a;
}
ll exgcd(ll a,ll b)
{
	ll t,gcd;
	if(b==0)
	{
		x=1,y=0;
		return a;
	}
	gcd=exgcd(b,a%b);
	t=x,x=y,y=t-a/b*y;
	return gcd;
}
int main()
{
	int i,j,k,n;
	ll d,t,tem,bb;
	while(~scanf("%d",&n)){
		flag=0;
		scanf("%lld%lld",&a1,&b1);
		for(i=1;i<n;i++){
			scanf("%lld%lld",&a2,&b2);
			d=gcd(a1,a2);
			if((b2-b1)%d==0){
				exgcd(a1/d,a2/d);//先约分,再求逆元
				tem=x;//求出的逆元
				k=(tem*(b2-b1)/d)%(a2/d);
				b1=(k*a1+b1)%(a1/d*a2);
				a1=(a1/d*a2);
			}else
				flag=1;
		}
		if(flag==1) printf("-1\n");
		else{
			b1=(b1+a1)%a1;
			if(b1==0) b1=a1;
			printf("%lld\n",b1);
		}
	}
}


扩展gcd 以及线性同余模方程

欧几里得算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd(...

20140714 「初等数论 - 拓展欧几里得+同余模方程」POJ 1061 青蛙的约会

Online Judge Problem Set Authors Online Contests User Web Board Home Page F.A.Qs Statist...

20140714 「初等数论 - 拓展欧几里得+同余模方程」POJ 2115 C Looooops

Online Judge Problem Set Authors Online Contests User Web Board Home Page F.A.Qs Statist...

(HDU 1226)超级密码 BFS 同余模应用

级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su...

同余模定理

同余模定理

【POJ】1426 Find The Multiple(暴力|同余模定理|BFS)

暴力可以解决,题目要求的数据没有超出long long#include using namespace std; long long ans; long long n; bool dfs(long l...

poj 1416 Find The Multiple 同余模定理

原来我用宽度搜索,结果TLE #include "iostream" #include "queue" using namespace std; typedef unsigned long lo...
  • fofu33
  • fofu33
  • 2013年05月10日 13:28
  • 463

HDU 3123 GCC(同余模定理)

GCC 大意:给一个n,一个m,求(0! + 1! + 2! + 3! + 4! + ... + n!)%m     0 0 思路:n可以达到10^100,很明显不能直接处理。但是发现只要n>m...

hdu 1573 X问题(解线性同余模方程组在给定取值范围的解)

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:同余模方程--BIT 1053 Han Xin Count Soldiers
举报原因:
原因补充:

(最多只允许输入30个字)