题目链接:https://vjudge.net/contest/373416#problem/I
思路:求出x的素因子在1-n中所有数的贡献。
Input
10 2Output
2Input
20190929 1605Output
363165664Input
947 987654321987654321Output
593574252
代码如下:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
vector<ll>v;
ll book[1000010],su[1000010];
const int mod = 1e9 + 7;
int r=0;
ll qq(ll x,ll n)//快速幂
{
ll res=1;
while(n)
{
if(n%2)
res=(res*x)%mod;
x=(x*x)%mod;
n/=2;
}
return res;
}
void prime()//埃氏筛法
{
book[2]=0;
for(ll i=2; i<=1000000; i++)
{
if(!book[i])
{
su[r++]=i;
for(ll j=i*i; j<=1000000; j+=i)
book[j]=1;
}
}
}
int main()
{
prime();
ll x,n;
scanf("%lld%lld",&x,&n);
for(int i=0; i<r; i++)
{
if(x%su[i]==0)
{
v.push_back(su[i]);//x的因子数
while(x%su[i]==0)
x/=su[i];
}
}
if(x>1)
v.push_back(x);
ll ans=1;
for(int i=0; i<v.size(); i++)
{
ll p=0;
ll y=n;
while(y)//在n中有多少个素数
{
p+=y/v[i];
y/=v[i];
}
ll xx=1;
xx=qq(v[i],p);
ans=(ans*(xx%mod))%mod;
}
printf("%lld\n",ans % mod);
return 0;
}