【HDU - 1576 】【A/B】

版权声明:本人原创,未经许可,不得转载 https://blog.csdn.net/qq_42505741/article/details/82054776

题目:

要求(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)。

Output

对应每组数据输出(A/B)%9973。

Sample Input

2
1000 53
87 123456789

Sample Output

7922
6060

解题思路:要我们求解(A/B)%9973,当然不能直接求解,你要求一下B的逆元,因为gcd(B,9973) = 1),这就说明 B 相对于模9973是存在逆元 b 的,然后(A/B)%9973就转化为(A * b)%9973 就等于 (n * b)%9973。

ac代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 1000008
using namespace std;
void exgcd(int a,int b,int &x,int &y)
{
	int temp;
	if(b==0)
	{
		x=1;
		y=0;
		return ;
	}
	else
	{
		exgcd(b,a%b,x,y);
		temp=x;
		x=y;
		y=temp-a/b*y;
	}
	
}
int main()
{
	int t;
	scanf("%d",&t);
	int n,b,x,y;
	while(t--)
	{
		scanf("%d%d",&n,&b);
		exgcd(b,9973,x,y);
		while(x<0)	x+=9973;
		printf("%d\n",x*n%9973);
	}
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/qq_42505741/article/details/82054776