B - A/B
要求(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
这一道题是模板题,比较基础。但对初学者来讲,还是比较绕的,还是应该多练题啊!!
这也是我第一次见到的真正用数学的思维去解决题的定理。
以下我将给出两种方式去解答这道题。
对与第一种来书还是要注意下 在扩展欧几里得算法中得到的x可能为负值,所以还需要x=x%9973+9973;
#include<stdio.h>
#include<algorithm>
using namespace std;
const int MOD=9973;
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 t=y;
y=x-(a/b)*y;
x=t;
}
int main()
{
int t,x,y,n,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&b);
exgcd(b,MOD,x,y);
x*=n;
printf("%d\n",(x%MOD+MOD)%MOD);
}
}
接下来是费马小定理
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll kuai(ll a, ll b)
{
ll res=1;
a%=9973;
while(b)
{
if(b&1) res=res*a%9973;
a=a*a%9973;
b>>=1;
}
return res;
}
int main()
{
int t,n,i,m,x,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&b);
x=kuai(b,9971);
printf("%d\n",(n*x)%9973);
}
return 0;
}