求逆元两种方法 hdu-1576-A/B

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1576

题目意思:

求出A/B mod 9973的值,n=A mod 9973 ,gcd(B,9973)=1.

因为gcd(B,9973)=1.

可以用费马小定理和扩展欧几里得算法两种方法来求B的逆元,然后化除法为乘法。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<queue>
#define eps 1e-6
#define INF (1<<30)
#define PI acos(-1.0)
using namespace std;

#define ll __int64

ll quick(ll a,ll b,ll m) //用费马小定理解
{
   ll res=1;

   while(b)
   {
      if(b&1)
         res=(res*a)%m;
      a=(a*a)%m;
      b=b>>1;
   }
   return res;
}

void egcd(int a,int b,int &x,int &y) //用扩展的欧几里得算法解
{
   if(b==0)
   {
      x=1;y=0;
      return ;
   }
   egcd(b,a%b,x,y);
   int temp=x;
   x=y,y=temp-a/b*y;
   return ;
}

int main()
{
   int t;
   int n,b;

   scanf("%d",&t);
   while(t--)
   {
      scanf("%d%d",&n,&b);
      int x,y;

     /* egcd(b,9973,x,y);
      x=x%9973;
      if(x<0)
         x+=9973;

      printf("%d\n",(n*x)%9973);*/

      printf("%d\n",(n*quick(b,9973-2,9973))%9973);
   }

   return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值