- P61's solution
P61's Solution
- 2025-9-14 19:55:30 @
最终乘积的符号只于负号个数的奇偶性有关。讨论所有符号的方案数:$\displaystyle\sum_{2 \mid i} C_n^i = \sum_{2 \nmid i} C_n^i = 2^{n-1}$。
接下来,对乘积的绝对值做质因数分解,对每个质因数 单独讨论方案数,由插板法,方案数为 。
最后把符号和所有质因数的方案数乘起来即可。
#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
int fc[1000112], cf[1000112];
int power(int b, int k)
{
if (k == 0) return 1;
if (k == 1) return b;
long long s = power(b, k >> 1);
s = s * s % P;
if (k & 1) s = s * b % P;
return s;
}
int divs(int a, int b, int p)
{
if (b % a == 0) return b / a;
int x = divs(p % a, a - b % a, a);
return (1ll * x * p + b) / a;
}
int C(int m, int n)
{
return 1ll * fc[n] * cf[m] % P * cf[n - m] % P;
}
void factor(long long& ans, int x, int y)
{
for (int i = 2; i * i <= x; i++)
{
if (x % i) continue;
int e = 0;
while (x % i == 0) x /= i, e++;
ans = ans * C(y - 1, y + e - 1) % P;
}
if (x != 1) ans = ans * y % P;
}
int main()
{
int T;
cin >> T;
fc[0] = 1;
for (int i = 1; i <= 1000100; i++)
fc[i] = 1ll * fc[i - 1] * i % P;
cf[1000100] = divs(fc[1000100], 1, P);
for (int i = 1000099; i >= 0; i--)
cf[i] = 1ll * cf[i + 1] * (i + 1) % P;
while (T--)
{
int x, y;
cin >> x >> y;
long long ans = power(2, y - 1);
factor(ans, x, y);
cout << ans << endl;
}
return 0;
}