最大公约数函数gcd(int a,int b)的构造和ax=b (mod n) 的解

Problem

Give you a modular equation ax=b (mod n). Count how many different x (0<=x<n) satisfying the equation.

Input

The input may contain several test cases.

Each test contains a line with three integers a, b and n, each integer is positive and no more than 10^9.

Input is terminated by EOF.

Output

For each test case, output a line containing the number of solutions.

Sample input

4 2 6

Sample output

2

#include <stdio.h>
long a,b,n;
int gcd(long a,long b){
    
if(a==0){
        
return b;
    }

    
else
    
{
        
return gcd(b%a,a);
    }

}

int main(){
    
while(scanf("%d %d %d",&a,&b,&n)!=EOF){
        
long d=gcd(a,n);
        
if(b%d==0)
            printf(
"%d\n",d);
        
else printf("0\n",d);
    }

    
return 0;
}
posted on 2008-04-08 12:53 zhongguoa 阅读(1067) 评论(1)  编辑 收藏 引用

评论

# re: 最大公约数函数gcd(int a,int b)的构造和ax=b (mod n) 的解[未登录] 2015-06-25 19:25 xxx  回复  更多评论   

#include <stdio.h>
long a,b,n;
int gcd(long a,long b){
if(a==0){
return b;
}
else
{
return gcd(b%a,a);
}
}
int main(){
while(scanf("%d %d %d",&a,&b,&n)!=EOF){
long d=gcd(a,n);
if(b%d==0)
printf("%d\n",d);
else printf("0\n",d);
}
return 0;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理