题目:http://codeforces.com/contest/1228/problem/C
题意:就不细述了。
题解:对于一个10^9以内的数分解质因数(数量较少),计算每一个质因数在1~n(10^18)每个数的贡献值。
假如有一质因数3,n为28
- 28/3 1 2 1x3 4 5 2x3 7 8 3x3 10 11 4x3 13 14 5x3 16 17 6x3 19 20 7x3 22 23 8x3 25 26 9x3 28
- 9/3 1 2 1x3 4 5 2x3 7 8 3x3
- 3/3 1 2 1x3
计算贡献值 3^(9+3+1)
关键代码:
类似于求 m!下质因数n的个数
参考:https://blog.csdn.net/running_in_dark/article/details/53453659
LL solve(LL n,LL p)
{
LL res=0;
while(n)
{
res+=n/p;
n/=p;
}
return pow(p,res);
}
代码:
#include<bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn=1005;
const int mod=1e9+7;
LL x,n;
vector<int>pm;
void cal(int n)
{
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
pm.push_back(i);
while(n%i==0)
{
n/=i;
}
}
}
if(n>1)
pm.push_back(n);
}
LL pow(LL a,LL n)
{
LL res=1;
while(n)
{
if(n&1)
res*=a,res%=mod;
n>>=1;
a*=a;
a%=mod;
}
return res;
}
LL solve(LL n,LL p)
{
LL res=0;
while(n)
{
res+=n/p;
n/=p;
}
return pow(p,res);
}
int main()
{
cin>>x>>n;
cal(x);
LL ans=1;
for(int i=0;i<pm.size();i++)
{
ans=ans*solve(n,pm[i]);
ans%=mod;
}
cout << ans << endl;
return 0;
}