Primes and Multiplication
题意:不好描述(逃)戳上面链接吧
分析:
题目看起来很复杂,但是理一下其实就是 x的每个质因数在1到n的的质因数集合中出现次数 答案就是x的每个质因数的次数幂(好像很绕,我也不知道在讲什么)
举个例子,x=5,n=70,x的质因数只有5,
f
(
5
,
1
)
∗
f
(
5
,
2
)
…
f
(
5
,
70
)
=
g
(
1
,
5
)
∗
g
(
2
,
5
)
…
g
(
70
,
5
)
f(5,1)*f(5,2)\dots f(5,70) = g(1,5)*g(2,5)\dots g(70,5)
f(5,1)∗f(5,2)…f(5,70)=g(1,5)∗g(2,5)…g(70,5)
去求这个结果
g(1,5),g(2,5)…g(4,5)…g(69,5) 这些不能整除5结果都为1
g(5,5),g(10,5),g(15,5)…都等于5
g(25,5),g(50,5)等于25
等于1的不管,对结果没有影响,重点就是求有多少个值等于5,多少个值等于25
用70/5=14可以得到能整除5的个数,但是这里面还包含了等于25的值,所以就不能再用70/25,而是用70/5=14再去除5,就能求出能整除两个5的个数
用次数去更新答案,也就是这一步:
for (int i = 0; i < prime.size(); i++) {
ll k = 0, tmp = n;
while (tmp) {
tmp /= prime[i];
k += tmp;//加上能整除当前质因数的个数
}
ans = (ans * quick_pow(prime[i], k)) % mod;
}
Code:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a));
#define lowbit(x) (x & -x)
#define lrt nl, mid, rt << 1
#define rrt mid + 1, nr, rt << 1 | 1
template <typename T>
inline void read(T& t) {
t = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
t = t * 10 + ch - '0';
ch = getchar();
}
t *= f;
}
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll Inf = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x7f7f7f7f;
const double eps = 1e-5;
const double Pi = acos(-1);
const int mod = 1e9 + 7;
vector<int> prime;
ll quick_pow(ll n, ll k) {
ll ret = 1, base = n;
while (k) {
if (k & 1)
(ret *= base) %= mod;
(base *= base) %= mod;
k >>= 1;
}
return ret;
}
int main(void) {
ll x, n;
read(x), read(n);
for (ll i = 2; i * i <= x; i++) {
if (x % i == 0) {
prime.push_back(i);
while (x % i == 0)
x /= i;
}
}
if (x > 1)
prime.push_back(x);
ll ans = 1;
for (int i = 0; i < prime.size(); i++) {
ll k = 0, tmp = n;
while (tmp) {
tmp /= prime[i];
k += tmp;
}
ans = (ans * quick_pow(prime[i], k)) % mod;
}
printf("%lld\n", ans);
return 0;
}