点击打开题目
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4308 Accepted Submission(s): 3326
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 123456789Sample Output
7922
6060Author
xhd
Source
HDU 2007-1 Programming Contest
根据题意列方程:Bx - 9973y = d
a = B,b = 9973;
A = B*x …… ①
上面求出x后,先让 x *= d,然后得出的x就是A/B的值,因为可能是个负数,所以变成进行一次求模运算。然后就可以输出了。
代码如下;
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
int exGCD(int a,int b,int &x,int &y)
{
if (!b)
{
x = 1;
y = 0;
return a;
}
int g = exGCD(b,a%b,y,x);
y -= a / b * x;
return g;
}
int main()
{
int u;
int a,b,c,d,x,y;
scanf ("%d",&u);
while (u--)
{
scanf ("%d %d",&d,&a);
b = 9973;
c = exGCD(a,b,x,y);
x *= d;
x = (x % b + b) % b;
printf ("%d\n",x);
}
return 0;
}