今天是数论周的最后一天,记得一周前从一个什么是数论?费马定理有什么用都不会的萌新,现在已经能开始写欧拉公式的模版、欧几里得、拓展欧几里得之类的模版了。
要求(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
对于这道题,题目给出gcd(B, 9973)=1即互质,又9973为质数,所以可以看出此处用到费马小定理。
由a=1(mod p),可以知道a^(p-1)=1(mod p)于是有a^(-1)=a^(p-2) (mod p)两边同除以a得到。
完整代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long ll;
ll a,b;
ll s_mi(ll x, ll y, ll mod)
{
x%=mod;
//int k=0;
ll ans=0;
/*while(y)
{
int t=y&1; //第k位上是否有1?
ll temp=1;
y=y>>1;
if(t)
{
for(int i=1; i<=k; i++)
{
temp=(temp*(x*x)%mod)%mod;
}
}
ans=(ans+temp)%mod;
k++; //放在最尾使得幂次从0开始
}*/
ans=x;
for(int i=1; i<y; i++)
{
ans=(ans%mod*x%mod)%mod;
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",(a*s_mi(b, 9971, 9973))%9973);
}
return 0;
}