QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#844516 | #9248. An Easy Math Problem | hxu10# | AC ✓ | 768ms | 4284kb | C++23 | 3.1kb | 2025-01-06 01:41:10 | 2025-01-06 01:41:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
static const int MAX = 100010;
// primeArr[i] will hold the count of distinct prime factors of i
int primeArr[MAX + 1];
vector<int> primeList; // will store all i for which primeArr[i] == 1
// Precompute primeArr and primeList
void buildPrimeList() {
// Fill primeArr with the number of distinct prime factors
for (int i = 2; i <= MAX; i++) {
// If primeArr[i] > 1, skip
if (primeArr[i] > 1) {
continue;
}
// For each multiple j of i, increment primeArr[j]
for (int j = i; j <= MAX; j += i) {
primeArr[j]++;
}
}
// Collect i where primeArr[i] == 1
for (int i = 2; i <= MAX; i++) {
if (primeArr[i] == 1) {
primeList.push_back(i);
}
}
}
void solveOneTest() {
long long n;
cin >> n;
// Factorize n using primeList
vector<long long> nums;
long long curr = n;
for (auto p : primeList) {
// small optimization
if ((long long)p * (long long)p > curr) {
break;
}
if (curr == 1) {
break;
}
long long countFactors = 0;
while (curr % p == 0) {
countFactors++;
curr /= p;
}
if (countFactors > 0) {
nums.push_back(countFactors);
}
}
// If curr > 1, then it's itself a prime (larger than MAX)
if (curr > 1) {
nums.push_back(1LL);
}
// Calculate base = Π (nums[i] + 1)
long long base = 1;
for (auto x : nums) {
base *= (x + 1);
}
// We'll store our final answer in ansVal
long long ansVal = 0;
// Recursive function, similar to Python's getnext
// index, pre1, flag1, pre2, flag2
function<void(int, long long, bool, long long, bool)> getnext =
[&](int index, long long pre1, bool flag1, long long pre2, bool flag2) {
if (index == (int)nums.size()) {
// At the end, only add if both flags are true
if (flag1 && flag2) {
ansVal += pre1 * pre2;
}
return;
}
// 1) Multiply pre1 by nums[index], set flag1=true
getnext(index + 1, pre1 * nums[index], true, pre2, flag2);
// 2) Multiply pre2 by nums[index], set flag2=true
getnext(index + 1, pre1, flag1, pre2 * nums[index], true);
// 3) Do neither
getnext(index + 1, pre1, flag1, pre2, flag2);
};
// Initial call
getnext(0, 1LL, false, 1LL, false);
// If ansVal is odd, mimic the Python "ERROR" and out-of-range access
if (ansVal % 2 != 0) {
cout << "ERROR " << ansVal << "\n";
vector<int> arr = {1};
// Out of range (intentional!)
cout << arr.at(2) << "\n";
}
// ansVal = ansVal/2 + base
ansVal = ansVal / 2 + base;
cout << ansVal << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
buildPrimeList();
int T;
cin >> T;
for(int t = 1; t <= T; t++) {
solveOneTest();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 4284kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1 2 2 3 2 5 2 4 3 5
result:
ok 10 lines
Test #2:
score: 0
Accepted
time: 768ms
memory: 4116kb
input:
2000 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 6469693230 646969323...
output:
29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 29525 ...
result:
ok 2000 lines
Test #3:
score: 0
Accepted
time: 7ms
memory: 4124kb
input:
2000 1763047095 79735483 1016286871 2864801397 2327774116 2668010360 3469893354 3634459021 1613699068 781737219 574741575 2763134701 1458502604 1822260248 2281150332 2924219311 2493931196 3735904708 158802001 2006921221 729928782 1974841034 727412600 2873393292 1291087179 2741607663 1893408215 29827...
output:
14 5 2 5 23 95 68 14 8 68 203 14 23 32 38 41 8 8 14 2 608 41 158 338 23 41 14 5 14 41 14 203 41 14 17 446 5 53 59 878 2 14 365 203 14 203 2 122 32 95 41 41 5 23 14 41 5 5 14 122 23 203 608 23 41 122 2 14 95 2 68 41 203 14 230 41 68 23 50 14 32 14 8 5 5 5 68 68 122 293 473 5 41 41 14 2 14 14 5 2 122 ...
result:
ok 2000 lines
Extra Test:
score: 0
Extra Test Passed