同余模方程--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(...

POJ 1426 Find The Multiple(同余模定理优化双入口BFS)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19219...
  • caduca
  • caduca
  • 2015-02-10 14:23
  • 3890

POJ 1845 Sumdiv (快速幂+质因数+约数和公式+同余模)

Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16109   Accepted: 3992 ...

POJ1426 Find The Multiple(DFS||BFS||同余模定理)

题意: 给出一个数,寻找这个数的倍数,要求只能由0和1组成 要点: 可以用BFS和DFS做,如果用BFS做要用同余模定理,否则因为数字很大,队列要开longlong型会爆内存。用DFS可以避免内存过大...

HDU2030_The Embarrassed Cryptographer_千进制、同余模

The Embarrassed Cryptographer Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

同余模定理

同余模定理

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

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

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

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm...

poj 1416 Find The Multiple 同余模定理

原来我用宽度搜索,结果TLE #include "iostream" #include "queue" using namespace std; typedef unsigned long lo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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