站外题快速幂70分
  • 板块题目总版
  • 楼主van_Dijk
  • 当前回复7
  • 已保存回复7
  • 发布时间2023/9/20 22:10
  • 上次更新2023/11/2 18:55:53
查看原帖
站外题快速幂70分
644697
van_Dijk楼主2023/9/20 22:10

题面:

这是一个求最大公约数的题目:给定整数aa,bb,nn,求ana^n+bnb^n和aa-bb的最大公约数(答案对1e9+7取模)

(1<=a,a,b,nn<=10^18)

代码:

纯快速幂,WA70,无TLE

#include <bits/stdc++.h>
#pragma GCC optimize(2)  //优化
using namespace std;
#define int unsigned long long
#define mod 1000000007
int n, m, k;
int ret;

int quickPower(int a, int b) {
    int ans = 1, base = a;
    while (b > 0) {
        if (b & 1)
            ans *= base;

        base *= base;
        b >>= 1;
    }
    return ans;
}

void input() { cin >> n >> m >> k; }

void solve() {
    int minus = n - m;
    int ans = quickPower(n, k) + quickPower(m, k);
    ret = (__gcd(ans, minus)) % mod;
    return;
}

void output() { cout << ret; }

void FastIO() {  //优化
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

signed main() {
    FastIO();
    input();
    solve();
    output();
    return 0;
}

码风很丑,见谅

2023/9/20 22:10
加载中...