Primes and Multiplication

C - Primes and Multiplication

Ideas: Find xall prime factors, with a vectorstored, then for every prime factors, we have to find answers to its final size of the contribution, that is, to find how many times it appears in the final product.

Solution:

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

In addition, the problem also need to use the power of quick tips.

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

Code:

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

Guess you like

Origin www.cnblogs.com/CADCADCAD/p/11616427.html