最大公约数和最小公倍数
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long gcd ( long long a, long long b)
{
return b? gcd ( b, a% b) : a;
}
long long lcm ( long long a, long long b)
{ return a/ gcd ( a, b) * b; }
int main ( )
{
long long a, b;
while ( cin>> a>> b)
{ printf ( "%lld %lld\n" , gcd ( a, b) , lcm ( a, b) ) ; }
return 0 ;
}
又见GCD
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long gcd ( long long a, long long b)
{
return b? gcd ( b, a% b) : a;
}
long long lcm ( long long a, long long b)
{ return a/ gcd ( a, b) * b; }
int main ( )
{
long long a, b;
while ( cin>> a>> b)
{ printf ( "%lld %lld\n" , gcd ( a, b) , lcm ( a, b) ) ; }
return 0 ;
}
多个数的最大公约数
#include<bits/stdc++.h>
using namespace std;
int main ( )
{
int n, a, b;
while ( cin> > n)
{
cin> > a;
for ( int i= 1 ; i< n; i++ )
{
cin> > b;
a = __gcd ( a, b) ;
}
cout< < a< < endl;
}
return 0 ;
}
多个数的最小公倍数
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long gcd ( long long a, long long b)
{ return b? gcd ( b, a% b) : a; }
long long lcm ( long long a, long long b)
{ return a/ gcd ( a, b) * b; }
int main ( )
{
long long n, i, a[ 11 ] ;
while ( cin>> n)
{
for ( i= 1 ; i<= n; i++ )
cin>> a[ i] ;
for ( i= 1 ; i<= n- 1 ; i++ )
a[ i+ 1 ] = lcm ( a[ i] , a[ i+ 1 ] ) ;
printf ( "%lld\n" , a[ n] ) ;
}
return 0 ;
}
LCM&GCD
#include <bits/stdc++.h>
using namespace std;
int t;
long long x, y;
int main ( )
{
cin>> t;
int cnt;
while ( t-- )
{
cnt= 0 ;
cin>> x>> y;
long long tmp= x* y;
for ( long long i= x; i<= y; i++ )
if ( tmp% i== 0 )
if ( tmp/ i>= x&& tmp/ i<= y&& __gcd ( i, tmp/ i) == x)
cnt++ ;
cout<< cnt<< endl;
}
return 0 ;
}
人见人爱gcd
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int gcd ( int a, int b)
{ return b? gcd ( b, a% b) : a; }
int main ( )
{
int t, a, b;
ios:: sync_with_stdio ( false ) ;
while ( cin>> t)
{
while ( t-- )
{
cin>> a>> b;
printf ( "%d\n" , a* a- 2 * b* gcd ( a, b) ) ;
}
}
return 0 ;
}
高木同学的因子
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main ( )
{
ll x, y;
cin>> x>> y;
int m = __gcd ( x, y) ;
int cnt= 0 ;
for ( int i= 1 ; i* i<= m; i++ )
if ( m% i== 0 ) cnt++ ;
if ( sqrt ( m) * sqrt ( m) == m) cout<< cnt* 2 - 1 << endl;
else cout<< cnt* 2 << endl;
return 0 ;
}
快速幂取模
#include <bits/stdc++.h>
using namespace std;
long long mode ( long long a, long long b, long long mod)
{
long long ans= 1 ;
while ( b)
{
if ( b% 2 == 1 )
{ b-- ; ans= ans* a% mod; }
a= a* a% mod;
b= b/ 2 ;
}
return ans;
}
int main ( )
{
long long a, b, c;
while ( cin>> a>> b>> c)
printf ( "%lld\n" , mode ( a, b, c) ) ;
return 0 ;
}
库特的数学题
#include <bits/stdc++.h>
using namespace std;
long long mode= 1e9 + 7 ;
long long MODE ( long long a, long long b)
{
long long sum= 1 ;
while ( b)
{
if ( b% 2 )
{
sum= sum* a% mode;
b-- ;
}
b= b/ 2 ;
a= a* a% mode;
}
return sum;
}
int main ( )
{
long long n, m;
cin>> n;
m= MODE ( 3 , n) ;
m* = 2 ;
m% = mode;
cout<< m<< endl;
return 0 ;
}