答案
2025-06-30 07:25:33
发布于:浙江
0阅读
0回复
0点赞
deepseek给出的答案(已试过,可行)
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <climits>
using namespace std;
int main() {
int N;
cin >> N;
long long m1, m2;
cin >> m1 >> m2;
vector<long long> S(N);
for (int i = 0; i < N; i++) {
cin >> S[i];
}
// 如果m1为1,则M=1,任何细胞在0秒时都满足条件
if (m1 == 1) {
cout << 0 << endl;
return 0;
}
// 分解m1的质因数
map<long long, int> factors_m1;
long long temp = m1;
for (long long i = 2; i * i <= temp; i++) {
while (temp % i == 0) {
factors_m1[i]++;
temp /= i;
}
}
if (temp > 1) {
factors_m1[temp]++;
}
// 计算M的质因数分解
map<long long, int> factor_M;
for (auto &p : factors_m1) {
factor_M[p.first] = p.second * m2;
}
long long min_time = LLONG_MAX;
bool found = false;
for (int i = 0; i < N; i++) {
long long s = S[i];
bool valid = true;
long long cell_time = 0;
for (auto &f : factor_M) {
long long p = f.first; // 质因子
long long exp_M = f.second; // M中的指数
int cnt = 0; // s中p的指数
// 计算s中质因子p的指数
long long temp_s = s;
while (temp_s % p == 0) {
cnt++;
temp_s /= p;
}
// 如果s中不包含质因子p
if (cnt == 0) {
valid = false;
break;
}
// 计算该质因子所需的最小时间(向上取整)
long long t_p = (exp_M + cnt - 1) / cnt;
if (t_p > cell_time) {
cell_time = t_p;
}
}
if (valid) {
found = true;
if (cell_time < min_time) {
min_time = cell_time;
}
}
}
if (found) {
cout << min_time << endl;
} else {
cout << -1 << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个