码迷,mamicode.com
首页 > 其他好文 > 详细

Primes and Multiplication

时间:2019-10-02 00:53:51      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:block   tps   turn   using   技巧   multi   ack   def   names   

C - Primes and Multiplication

思路:找到x的所有质数因子,用一个vector储存起来,然后对于每一个质因子来说,我们要找到它对最后的答案的贡献的大小,即要找到它在最后的乘积中出现了多少次。

求解方法:

for(auto i:v)
{
    ll cnt=0;
    ll temp=n/i;
    while(temp)
        cnt+=temp,temp/=i;
    ans=(ans*qpow(i,cnt))%mod;
}

另外,该题还需要用到快速幂的技巧。

ll qpow(ll x,ll n)
{
    ll re=1;
    while(n)
    {
        if(n&1) re=(re*x)%mod;
        n>>=1,x=(x*x)%mod;
    }
    return re;
}

代码:

// Created by CAD on 2019/10/1.
#include <bits/stdc++.h>

#define ll long long
using namespace std;
const int mod=1e9+7;

ll qpow(ll x,ll n)
{
    ll re=1;
    while(n)
    {
        if(n&1) re=(re*x)%mod;
        n>>=1,x=(x*x)%mod;
    }
    return re;
}
vector<ll> v;
int main()
{
    ll n,x;    cin>>x>>n;
    for(ll i=2;i*i<=x;++i)
        if(x%i==0)
        {
            v.push_back(i);
            while(x%i==0)
                x/=i;
        }
    if(x!=1) v.push_back(x);
    ll ans=1;
    for(auto i:v)
    {
        ll cnt=0;
        ll temp=n/i;
        while(temp)
            cnt+=temp,temp/=i;
        ans=(ans*qpow(i,cnt))%mod;
    }
    cout<<ans<<endl;
}

Primes and Multiplication

标签:block   tps   turn   using   技巧   multi   ack   def   names   

原文地址:https://www.cnblogs.com/CADCADCAD/p/11616427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!