HDU 1576:A/B
A/B
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2811 Accepted Submission(s): 2079
Problem Description
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2 1000 53 87 123456789
Sample Output
7922 6060
只能说是趁热打铁,又来一道扩展欧几里德的题目,也很简单。
(n=A%9973)翻译过来就是A=9973*X+n      (1)
求的y=(A/B)%9973翻译过来就是A/B=9973*Z+y (2),注意这里咱们要求的是y
通过(1)(2)式,因为A不知道,所以要把A消掉。(真有一种做高中题目的感觉。。。)得到的方程整理即是
9973*(Z*B-X) +B*y = n
已知中说了gcd(9973,B)=1,太好了这个条件。因为咱们对(Z*B-X) 这一部分不关注,只求y。所以把(Z*B-X)当成一个未知数就可以了,一个扩展欧几里德就A掉。
代码:
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int xx,yy,yue;
int a,b,d;
void ex_gcd(int a,int b, int &xx,int &yy)
{
	if(b==0)
	{
		xx=1;
		yy=0;
		yue=a;
	}
	else
	{
		ex_gcd(b,a%b,xx,yy);
		int t=xx;
		xx=yy;
		yy=t-(a/b)*yy;
	}
}
int main()
{
	int test;
	cin>>test;
	while(test--)
	{
		long long n,B;
		cin>>n>>B;
		ex_gcd(9973,B,xx,yy);
		yy=yy*n;
		yy=(yy%9973+9973)%9973;
		cout<<yy<<endl;
	}
	return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号 
