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)。
Output
对应每组数据输出(A/B)%9973。
Sample Input
2
1000 53
87 123456789
Sample Output
7922
6060
Author
xhd
Source
HDU 2007-1 Programming Contest
思路: 强行套用模板,利用扩展欧几里得算法与求逆元函数进行求解。
求(a/b) mod m,设b的逆元是k;
(a/b) mod m = ((a/b) mod m)((bk) mod m) = ((a/b)bk) mod m = (ak) mod m;
(a + b) mod m = ((a mod m) + (b mod m)) mod m;
(a - b) mod m = ((a mod m) - (b mod m)) mod m;
(a * b) mod m = ((a mod m) * (b mod m)) mod m;
#include<string.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e9+10;
const int inf = 0x3f3f3f3f;
const int Base =131;
const ll INF = 1ll << 62;
//const double PI = acos(-1);
const double eps = 1e-7;
const int mod = 1e9+7;
#define mem(a,b) memset(a,b,sizeof(a))
#define speed {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); }
void exgcd(int a,int b,int &x,int &y)//求扩展欧几里得
{
if(b==0)
{
x=1,y=0;
return;
}
exgcd(b,a%b,x,y);
int tmp=x;
x=y;
y=tmp-(a/b)*y;
}
int mod_inversse(int a,int m)//求逆元
{
int x,y;
exgcd(a,m,x,y);
return (m+x%m)%m;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int a,b,exb;
scanf("%d %d",&a,&b);
exb=mod_inversse(b,9973);
int res=a*(exb%9973);
printf("%d\n",res%9973);
}
//system("pause");
return 0;
}