2020牛客暑期多校训练营(第四场)B——Basic Gcd Problem

题目链接

在这里插入图片描述
在这里插入图片描述
题目分析:

在这里插入图片描述

主要就是这个公式,我们分析一下,这是一个迭代公式,直到x=1的时候才会结束。

在这里插入图片描述
每次迭代都会乘上c的值,所以迭代n次就有n个c相乘,也就是c的n次方,主要求解迭代次数
也就是寻找n质因子的个数cnt

for(int i=2;i*i<=n;i++)
        {
            while (n%i==0)
            {
                cnt++;
                n/=i;
            } 
        }

求幂的话防止数据比较大用快速幂

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;
}

AC代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const 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()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
   int t;
   cin>>t; 
   while(t--)
    {
        int cnt=0;
        int n,c;
        cin>>n>>c;
        for(int i=2;i*i<=n;i++)
        {
            while (n%i==0)
            {
                cnt++;
                n/=i;
            } 
        }
        if(n>1) cnt++;
        ll ans=qpow(c,cnt,mod);
        cout<<ans<<"\n"; 
    }
    return 0;  
}

注意:

除了求幂的数据用long long,主函数的其他数据不要用,牛客会卡内存~~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值