Description
请计算对于给出的N,M,2N-1与2M-1的最大公约数。
多组测试用例
Input
两个整数N,M(1 <= N <= 100, 1<= M <= 100)
Output
2^N-1与2^M-1的最大公约数,保证结果不超过2^63-1(此处”^"均不是异或,而是次幂的意思)
Sample Input
1 2
Sample Output
1
注意:输出2的gcd(n,m)次幂后减1就行,直接求会爆long long
附上AC代码:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <utility>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
int main()
{
ll a,b;
ll gcd(ll x, ll y);
ll q_pow(ll a, ll b);
int n,m;
while(cin>>n>>m)
{
a=gcd(n,m);
b=q_pow(2,a)-1;//不要分别求2的m次幂和2的n次幂,会爆long long
printf("%lld\n",b);
}
return 0;
}
ll gcd(ll x, ll y)
{
if(x%y == 0)return y;
return gcd(y, x%y);
}
ll q_pow(ll a, ll b)
{
ll ans = 1;
while(b > 0)
{
if(b & 1)
ans = ans * a ;
a = a * a ;
b >>= 1;
}
return ans;
}