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

  对于这道题,题目给出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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值