首页 > 其他 > 详细

乘法逆元

时间:2016-09-28 15:20:35      阅读:258      评论:0      收藏:0      [点我收藏+]
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
 
给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的。
 
Input
输入2个数M, N中间用空格分隔(1 <= M < N <= 10^9)
Output
输出一个数K,满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的。
Input示例
2 3
Output示例
2

技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

long long min(long long a,long long b)
{
    if(a>b)return b;
    else return a;
}

int main()
{
    long long n,m;
    cin>>m>>n;
    long long ans=1000000000;
    for(long long i=1;i<n;++i)
    {
        if(i*m%n==1)ans=min(ans,i);
    }
    cout<<ans;
    puts("");
    return 0;
}
View Code

注意longlong

//能AC但是会T

正解exgcd

技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long x,y;
void ex_gcd(int a,int b)
{
    if(b==0)
    {
        x=1;
        y=0;
        return;
    }
    ex_gcd(b,a%b);
    int temp=x;
    x=y;
    y=temp-a/b*y;
}

int main()
{
    long long n,m;
    scanf("%lld%lld",&n,&m);
    ex_gcd(n,m);
    printf("%lld",(x%m+m)%m);
    puts("");
    return 0;
}
AC代码

 






乘法逆元

原文:http://www.cnblogs.com/gc812/p/5916320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!