SDNUOJ 1680.easy problem Ⅰ

本文介绍了一个简单的算法问题,即计算2N-1与2M-1的最大公约数。通过使用辗转相除法求最大公约数,并结合快速幂运算提高效率,实现了一种简洁的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

请计算对于给出的N,M,2N-1与2M-1的最大公约数。
多组测试样例

Input

两个整数N,M(1 <= N <= 50, 1<= M <= 50)

Output

2N-1与2M-1的最大公约数

Sample Input

1 2

Sample Output

1

水题直接求解就行。

代码:

#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 n, m;
    while(cin >> n >> m)
        cout << qpow(2, gcd(n, m)) - 1 << '\n';
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值