C. Primes and Multiplication time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's introduce some definitions that will be needed later. Let prime(x)prime(x) be the set of prime divisors of xx. For example, prime(140)={2,5,7}prime(140)={2,5,7}, prime(169)={13}prime(169)={13}. Let g(x,p)g(x,p) be the maximum possible integer pkpk where kk is an integer such that xx is divisible by pkpk. For example:
Let f(x,y)f(x,y) be the product of g(y,p)g(y,p) for all pp in prime(x)prime(x). For example:
You have integers xx and nn. Calculate f(x,1)⋅f(x,2)⋅…⋅f(x,n)mod(109+7)f(x,1)⋅f(x,2)⋅…⋅f(x,n)mod(109+7). Input The only line contains integers xx and nn (2≤x≤1092≤x≤109, 1≤n≤10181≤n≤1018) — the numbers used in formula. Output Print the answer. Examples input Copy 10 2 output Copy 2 input Copy 20190929 1605 output Copy 363165664 input Copy 947 987654321987654321 output Copy 593574252 Note In the first example, f(10,1)=g(1,2)⋅g(1,5)=1f(10,1)=g(1,2)⋅g(1,5)=1, f(10,2)=g(2,2)⋅g(2,5)=2f(10,2)=g(2,2)⋅g(2,5)=2. In the second example, actual value of formula is approximately 1.597⋅101711.597⋅10171. Make sure you print the answer modulo (109+7)(109+7). In the third example, be careful about overflow issue.
|
#include<stdio.h>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long ll;
ll x,n,ans=1;
ll pr(ll a,ll b)
{
if(b==0)return 1;
ll res=1;
while(b!=0)
{
if(b%2==1)res*=a;
a*=a;
b/=2;
a%=mod;
res%=mod;
}
return res;
}
void pre(ll i)
{
ll m=n;
while(m)
{
m/=i;
ans=(ans*pr(i,m))%mod;
}
}
int main()
{
cin>>x>>n;
for(int i=2; i*i<=x; i++)
{
if(x%i==0)pre(i);
while(x%i==0)x/=i;
}
if(x!=1)pre(x);
cout<<ans<<endl;
return 0;
}