GCD

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

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

题目描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值