GCD

本文介绍了一种高效解决特定GCD问题的方法,利用快速幂运算及改进的最大公约数求解算法,在大数环境下实现快速计算。通过遍历求约数的方式,并结合剪枝优化,显著提高了计算速度。
部署运行你感兴趣的模型镜像

题目描述

Given an integer N and M, you have to calculate
题目描述
Here GCD(N,K) means the greatest common divisor of integer N and integer K
.

输入

The first line contains a single integer T(1≤T≤10), indicating the number of test cases.
Each test case contains two integers N and M (1≤N,M≤109).

输出

Output the required answer modulo 109+7 for each test case, one per line.

样例输入

2
4 2
2 5

样例输出

24
30

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

#define M 1000000007
#define LL long long

int a[1024], b[1024];
int num, num1;

void gcd(int n) {
    num = 0;
    for (int i = 1; i <= sqrt(n); ++i) {
        if ((n % i) == 0) {
            a[num++] = i;
            a[num++] = n / i;
            if (n / i == i)
                num--;
        }
    }
    sort(a,a + num);
    num1 = n;
    for (int i = num-1; i >= 0; --i) {
        b[i] = n/a[i];
        for (int j = num-1; j > i ; --j) {
            if(a[j] % a[i] == 0)
                b[i] -= b[j];
        }
        num1 -= b[i];
    }
}

LL test(LL m, int gcd) {
    LL sum = 1;
    while(gcd){
        if(gcd % 2 == 1)
            sum = (sum * m) % M;
        m = (m * m) % M;
        gcd >>= 1;
    }
    sum = sum % M;
    return sum;
}

int main()
{
    int t, n;
    LL s, m;
    cin >> t;
    while (t--) {
        cin >> n >> m;
        if (n == 1){
            printf("%lld\n",m);
            continue;
        }
        s = 0;
        num1 = 1;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        gcd(n);
        for (int i = 0; i < num; ++i) {
            //cout << a[i] << ", " << b[i] << endl;
            s = (s + b[i] * test(m, a[i])) % M;
        }
        //cout << num1 << endl;
        s = (s + m * (num1)) % M;
        s = s % M;
        printf("%lld\n",s);
    }
    return 0;
}
    这道题逻辑是很简单的,但是按着最普通的方法做,你都不知道到超时多久了,所以得用特殊的方法。这里有幂,我们就用快速幂的方法。还有大数求最大公约数,我之前用的是辗转相除法,一直到10000000就不行了,时间超过了1s。后来用了遍历求约数的方法,结果到1000000000大概要3s,真的是醉了。最后在遍历的基础上,做了些剪枝,然后速度快得飞起~。
    其实辗转相除法是算比较快的了,只不过这里的题目要有好多次求最大公约数,所以这里不适合。先求约数的方法显然更适合这种方法。

在做题目的时候翻到一个不错的讲GDC的博客
http://www.cnblogs.com/zhangshu/archive/2011/07/20/2111276.html

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

在 Python 中,计算两个数的最大公约数(GCD)有多种方法,主要包括以下几种实现方式: ### 1. 使用内置库 `math` 中的 `gcd` 函数 Python 3.5 及以上版本的 `math` 模块提供了 `gcd` 函数,可以直接用于计算两个整数的最大公约数。该方法简洁高效,推荐在支持的环境中使用。 ```python import math a = 24 b = 45 print(math.gcd(a, b)) # 输出 3 ``` ### 2. 使用辗转相除法(欧几里得算法) 这是经典的数学算法,通过反复取余直到余数为零来求解最大公约数。其核心逻辑是将较大的数除以较小的数,并用较小的数和余数继续操作,直到余数为零,此时的除数即为最大公约数[^1]。 ```python def gcd(x, y): while y != 0: x, y = y, x % y return x print(gcd(24, 45)) # 输出 3 ``` ### 3. 使用更相减损法(辗转相减法) 这种方法通过不断用较大的数减去较小的数,直到两个数相等为止。该方法在没有取余操作的早期计算机中较为常用[^3]。 ```python def gcd_subtract(a, b): while a != b: if a > b: a -= b else: b -= a return a print(gcd_subtract(24, 45)) # 输出 3 ``` ### 4. 使用递归实现辗转相除法 递归方式可以更简洁地表达算法逻辑,适合理解函数式编程思想。 ```python def gcd_recursive(x, y): if y == 0: return x else: return gcd_recursive(y, x % y) print(gcd_recursive(24, 45)) # 输出 3 ``` ### 5. 使用 `fractions` 模块中的 `gcd` 函数(适用于 Python 3.5 之前版本) 在较早版本的 Python 中,可以使用 `fractions` 模块中的 `gcd` 函数,但该方法已在 Python 3.5 被 `math.gcd` 替代。 ```python from fractions import gcd print(gcd(24, 45)) # 输出 3 ``` ### 注意事项 - `math.gcd` 返回的结果始终为非负整数,即使输入包含负数。 - 如果输入的两个数均为零,则 `gcd` 无定义,但在某些实现中可能返回 `0`。 - 若需计算最小公倍数(LCM),可通过公式 `lcm(a, b) = abs(a * b) // gcd(a, b)` 实现[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值