思路:
质因数分解和快速幂结合
细节注意:
- k&1比k%2判断奇数更快
- 分解质因数时候 i*i<=n比i<=sqrt(n)快
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
typedef long long LL;
LL qsort(LL x,LL k)
{
LL ans = 1;
while(k)
{
if(k&1) ans=ans*x%mod;
k>>=1;
x=x*x%mod;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x,c;
scanf("%d%d",&x,&c);
int c0 = c;
int ans = c0;
int cnt = 0;
for(int i=2;i*i<=x;i++)
{
while(x%i==0)
{
cnt++;
x/=i;
}
}
if(x!=1) cnt++;
// printf("%d\n",cnt);
printf("%lld\n",qsort(ans,cnt));
}
return 0;
}
因为细节超时代码:
因为两个细节而超时
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
typedef long long LL;
LL qsort(LL x,LL k)
{
LL ans = 1;
while(k)
{
//注意这里
if(k%2) ans=ans*x%mod;
k>>=1;
x=x*x%mod;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int x,c;
scanf("%d%d",&x,&c);
int c0 = c;
int ans = c0;
int cnt = 0;
//注意这里
for(int i=2;i<=sqrt(x);i++)
{
while(x%i==0)
{
cnt++;
x/=i;
}
}
if(x!=1) cnt++;
// printf("%d\n",cnt);
printf("%lld\n",qsort(ans,cnt));
}
return 0;
}