水题,只是想借此记一下gcd函数的模板
1 #include<cstdio> 2 int gcd(int m,int n){return n?gcd(n,m%n):m;} 3 int main() 4 { 5 int n,m,t; 6 scanf("%d",&t); 7 while(t--){ 8 scanf("%d%d",&m,&n); 9 if(gcd(n,m)==1) printf("NO "); 10 else printf("YES "); 11 } 12 }
关于HDU1108,lcm( m , n ) = gcd( m , n ) * ( a / gcd( m , n ) ) * ( b / gcd( m , n ) ) = m * n / gcd( m , n );
1 #include<cstdio> 2 int gcd(int m,int n){return n?gcd(n,m%n):m;} 3 int lcm(int m,int n){return m/gcd(m,n)*n;} 4 int main() 5 { 6 int n,m; 7 while(scanf("%d%d",&m,&n)!=EOF) printf("%d ",lcm(m,n)); 8 }
当然,我们知道,对于一个递归函数,将它用while展开,会更加快:
1 int gcd(int m,int n) 2 { 3 int tmp; 4 while(n!=0) 5 { 6 tmp=n; 7 n=m%n; 8 m=tmp; 9 } 10 return m; 11 }