2020牛客多校 第四场 B-Basic Gcd Problem (数论 + 线性筛 + 快速幂)

题目链接: B-Basic Gcd Problem

Description

题意:给出n, c,求c的n的质因数个数的次方
As a great ACMer, ZYB is also good at math and number theory.
ZYB constructs a function fc(x) .
such that:
在这里插入图片描述
Give some positive integer pairs (ni,cc), ZYB wants to know f {ci}(ni)/mod(109+7).

Input

  • The input contains multiple test cases. The first line of input contains one integer T (1≤ T ≤106).
  • In the following T lines, each line contains two integers ni, ci(1 ≤ n i, ci ≤ 106) describing one question.

Output

For each test case, output one integer indicating the answer.

Sample Input

2
3 3
10 5

Sample Output

3
25

Method

  • 首先是最重要的一点就是从f(x)函数里面找出结论:f(x) = cx的质因数个数;
  • 切记不能使用普通或者暴力的方法打素数表,TLE警告;别问我为什么这么肯定
  • 我是通过产生素数表同时将表中的值 改为当 x 值为 i 时,最小的质因数大小,这样可以非常快的计算出质因数个数;
  • 最后就是快速幂了,get(c, sum)即可;

Code

详见注释

#include 
#include 

using namespace std;
#define Max 1000003
#define ll long long
#define mod 1000000007
template<typename T> T get(T a, T b) { 		//快速幂 
	ll s = 1%mod;
	while (b) {
		if (b%2 == 1) {
			s = (s*a)%mod;
		}
		b /= 2;
		a = (a%mod)*(a%mod)%mod;
	}
	return s;
}

int T, n, prm[Max]={0};						//质数表 
ll ans, sum, c;

void solve(int x)
{
	while(x > 1)							//计算n的质因数个数 
	{
		sum++;
		x /= prm[x];
	}
	ans = get(c, sum);					
	printf("%lld\n", ans);
}
int main()
{
	for(int i=2; i<Max; i++)				//生成质数表 
	{
		if(!prm[i]) {
			for(int j=1; j*i<Max; j++)
				prm[j*i] = i;
		}
	}
	scanf("%d", &T);
	while(T--)
	{
		sum = 0;
		scanf("%d%lld", &n, &c);
		solve(n);
	}
	return 0;
}

蒟蒻一只,欢迎指正

你可能感兴趣的:(训练赛病历,数论)