As a great ACMer, ZYB is also good at math and number theory.
ZYB constructs a function fc(x)fc(x) such that:
As a great ACMer, ZYB is also good at math and number theory.
ZYB constructs a function fc(x)fc(x) such that:
.
Give some positive integer pairs (ni,ci)(ni,ci), ZYB wants to know fci(ni)mod (109+7)fci(ni)mod(109+7).
输入描述:
The input contains multiple test cases. The first line of input contains one integer T(1≤T≤106)T(1≤T≤106).
In the following TT lines, each line contains two integers ni,cini,ci (1≤ni,ci≤1061≤ni,ci≤106) describing one question.
输出描述:
For each test case, output one integer indicating the answer.
示例1
输入
复制
2
3 3
10 5
输出
复制
3
25
.
Give some positive integer pairs (ni,ci)(ni,ci), ZYB wants to know fci(ni)mod (109+7)fci(ni)mod(109+7).
输入描述:
The input contains multiple test cases. The first line of input contains one integer T(1≤T≤106)T(1≤T≤106).
In the following TT lines, each line contains two integers ni,cini,ci (1≤ni,ci≤1061≤ni,ci≤106) describing one question.
输出描述:
For each test case, output one integer indicating the answer.
示例1
输入
复制
2
3 3
10 5
输出
复制
3
25
读了好多遍题才读懂,C^n,n=最大因子
打表质因子
int arr[N] = {0};//N == 1e6 + 5
for(int i = 1;i <= N;i++)
{
int sum = 0,n = i;
for(int j = 2;j * j <= n;j++)
while(n % j == 0)
sum++,n/=j;
if(n > 1) sum++;//标准的质因数分解模板,分解出来个数
arr[i] = sum;
}
优化打表质因子
int arr[N] = {0};//N == 1e6 + 5
for(int i = 1;i <= N;i++)
{
int sum = 0,n = i;
for(int j = 2;j * j <= n;j++)
{
while(n % j == 0)
{
sum++,n/=j;
if(arr[n] != 0)//这个数前面已经处理过了,那么就直接加就可以了,不用再判断了
{
arr[i] = arr[n] + sum;
n = 0;
break;
}
}
if(n == 0) break;
}
if(n == 0) continue;
if(n > 1) sum++;
arr[i] = sum;
}
优化快速幂
ll quick_pow(ll a,ll b,ll mod)
{
ll ans = 1;
while(b)
{
if(b&1)
ans = a*ans%mod;
b>>=1;
a = a*a%mod;
}
return ans%mod;
}
ac代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int mod = 1e9+7;
typedef long long ll;
int arr[N],t;
ll quick_pow(ll a,ll b)
{
ll ans = 1;
while(b)
{
if(b&1)
ans = a*ans%mod;
b>>=1;
a = a*a%mod;
}
return ans%mod;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
for(int i = 1;i <= N;i++)
{
int sum = 0,n = i;
for(int j = 2;j * j <= n;j++)
{
while(n % j == 0)
{
sum++,n/=j;
if(arr[n] != 0)
{
arr[i] = arr[n] + sum;n = 0;
break;
}
}
if(n == 0) break;
}
if(n == 0) continue;
if(n > 1) sum++;
arr[i] = sum;
}
scanf("%d",&t);
while(t--)
{
ll n,c;
scanf("%lld %lld",&n,&c);
printf("%lld\n",quick_pow(c,arr[n]));
}
}
素数筛
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll N=1e6+10;
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}
ll prime[N],k,t,n,c;
bool isprime[N];
void Prime(){
fill(isprime,isprime+N,1);
k=0;
prime[1]=0;
for(ll i=2;i<N;i++){
if(isprime[i]){
prime[k++]=i;
for(ll j=2;i*j<N;j++)
isprime[i*j]=0;
}
}
}
ll solve(ll n){
ll cnt=0,sum=0;
for(ll i=0;i<k&&prime[i]*prime[i]<=n;i++){
if(n%prime[i]==0){
while(n%prime[i]==0){
sum++;
n/=prime[i];
}
}
}
if(n>1) sum++;
return sum;
}
int main(){
Prime();
scanf("%lld",&t);
while(t--){
scanf("%lld%lld",&n,&c);
printf("%lld\n",power(c,solve(n)));
}
}
参考