Modular Inverse [ZOJ 3609]

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712

View Code
int N,M;
//注意题目说x为正整数,0不算
int Extend_euc(int a,int b,int&x,int&y) {
    if(b==0) {x=1,y=0;return a;}
    int d=Extend_euc(b,a%b,x,y);
    int tmp=x; x=y; y=tmp-(a/b)*y;
    return d;
}
void modeq(int a,int b,int n) {
    int e,i,d,x,y,tmp;
    d=Extend_euc(a,n,x,y);
    if(b%d>0) puts("Not Exist");
    else {
        e=((x*(b/d))%n+n)%n;
        printf("%d\n",e);
    }
}
void get_data() {
    int i,j,k;
    scanf("%d%d",&N,&M);
}

void solve() {
    int i,j,k;
    if(M==1) puts("1");
    else modeq(N,1,M);
}

 

posted @ 2013-05-09 13:05  zhang1107  阅读(123)  评论(0编辑  收藏  举报