HDU 1576 A/B (inverse element)

Problem Description

(A/B)%9973 is required, but because A is very large, we only give n(n=A%9973) (we give A must be divisible by B, and gcd(B,9973) = 1).

Input

The first row of data is a T, which means there are T groups of data.
Each group of data has two numbers n (0 <= n <9973) and B (1 <= B <= 10^9).

Output

Corresponding to each group of data output (A/B)%9973.

Sample Input

2
1000 53
87 123456789

Sample Output

7922
6060

Ideas

a / b (mod p) = a * inv [b] (mod p)
calculating the inverse element method Click multiplicative inverse

Code

#include<iostream>
using namespace std;

void exgcd(int a,int b,int&x,int&y)
{
    
    
	if(!b)
	{
    
    
		x=1;
		y=0;
		return;
	}
	exgcd(b,a%b,y,x);
	y-=(a/b)*x;
}

int inv(int a,int p)
{
    
    
	int x,y;
	exgcd(a,p,x,y);
	return (x%p+p)%p;
 } 
 
int main()
{
    
    
	int t;
	long long int n,b;
	cin>>t;
	while(t--)
	{
    
    
		cin>>n>>b;
		long long int c;
		c=inv(b,9973);
		long long int d;
		d=(n*c)%9973; 
		cout<<d<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/m0_54621932/article/details/113915828