2020牛客暑期多校训练营(第四场)B-Basic Gcd Problem

传送门

Description

在这里插入图片描述

  • f c i ( n i ) m o d ( 1 0 9 + 7 ) f_{c_i}(n_i)mod(10^9+7)

题解

  • n = i = 1 n p i b i n=\prod_{i = 1}^{n}p_i^{b_i}

  • s u m p o w = i = 1 n b i sumpow=\sum_{i = 1}^{n}b_i

  • f c i ( n i ) = n i s u m p o w f_{c_i}(n_i)=n_i^{sumpow}

实现

#include <bits/stdc++.h>
#define ll long long
const int N=1e6+10,mod=1e9+7;
using namespace std;
ll T,n,c;
bool np[N];
ll tot,p[N],mn[N];
ll ksm(ll a,ll b){ll r=1;while(b){if(b&1)r=r*a%mod;a=a*a%mod,b>>=1;}return r;}
void sieve(){
	np[0]=np[1]=mn[1]=1;
	for(int i=2;i<N;i++){
		if(!np[i]) p[++tot]=i,mn[i]=i;
		for(int j=1;j<=tot&&i*p[j]<N;j++){
			np[i*p[j]]=1,mn[i*p[j]]=p[j];
			if(i%p[j]==0) break;
		}
	}
}int main(){
	sieve(),scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld",&n,&c);
		ll p=0;
		while(n^1) n/=mn[n],++p;
		printf("%lld\n",ksm(c,p));
	}
}

猜你喜欢

转载自blog.csdn.net/ding_ning123/article/details/107497157