2020 牛客暑假训练营补题 (第四场)

暑假还是不能摸鱼了,尽量每天补一道题。
这里大部分的补题应该都是参考了大佬的题解,所以如果有大佬看到了自己的解法,请多多包涵。

第四场

    • B题
      • 题目描述
      • 题目思路
      • 代码

B题

题目描述

2020 牛客暑假训练营补题 (第四场)_第1张图片
这个题目的意思就是给出一个变量n,代表测试组数,每一行给出式中所需要的x,c。要你计算每一行上式输出的值。

题目思路

其实这就是一个套娃函数,把它要怎么操作弄懂就行,本质还是很简单的就质因子和gcd。

代码

#include 
using namespace std;
int mod = 1e9 + 7;
#define ll long long
ll qpow(ll x, ll y, ll mod)
{
    ll res = 1;
    while (y)
    {
        if (y & 1)
            res = (res * x) % mod;
        x = (x * x) % mod;
        y >>= 1;
    }
    return (res + mod) % mod;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, c, cnt = 0;
        cin >> n >> c;
        for (int i = 2; i * i <= n; i++)
        {
            while (n % i == 0)
            {
                cnt++;
                n /= i;
            }
        }
        if (n > 1)
            cnt++;
        int ans = qpow(c, cnt, mod);
        cout << ans << "\n";
    }
}

你可能感兴趣的:(2020 牛客暑假训练营补题 (第四场))