Description
请计算对于给出的N,M,aN-bN与aM-bM的最大公约数。
题目保证a,b互质,且a>b
Input
多组输入,每组四个整数 a, b, N,M(1 <= a <= 500, 1 <= b <= 500, 1 <= N <= 50, 1<= M <= 50)
Output
aN-bN与aM-bM的最大公约数,结果不超过263-1
Sample Input
3 2 2 3
Sample Output
1
水题,注意开long long。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <sstream>
#include <cctype>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a%b);
}
ll qpow(ll a, ll b)
{
ll ans = 1;
while(b)
{
if(b & 1)
ans = (ans * a);
a = (a * a);
b >>= 1;
}
return ans;
}
int main()
{
int a, b, n, m;
while(cin >> a >> b >> n >> m)
cout << qpow(a, gcd(n, m)) - qpow(b, gcd(n, m)) << '\n';
return 0;
}