QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#856866 | #9962. Diminishing Fractions | IllusionaryDominance# | WA | 184ms | 5968kb | C++20 | 2.3kb | 2025-01-14 18:24:43 | 2025-01-14 18:24:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 50000 + 5;
int qry[MAX_N];
char mark[MAX_N];
int pri[MAX_N], num[MAX_N], K[MAX_N], prod[MAX_N], cur[MAX_N], inv[MAX_N], pc;
char np[MAX_N];
vector <pair <int, int>> ans[MAX_N];
int exgcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
}
int main() {
int T;
scanf("%d", &T);
for (int i = 1; i <= T; i ++) {
scanf("%d", qry + i);
mark[qry[i]] = i;
}
int N = 50000;
for (int i = 2; i <= N; i ++) {
if (!np[i]) {
pri[++ pc] = i;
num[pc] = cur[pc] = i;
while (1ll * num[pc] * i <= N) {
num[pc] *= i;
}
prod[pc] = 1;
for (int j = 1; j < pc; j ++) {
prod[j] = 1ll * prod[j] * i % num[j];
prod[pc] = 1ll * prod[pc] * cur[j] % num[pc];
}
}
for (int j = 1; j <= pc && i * pri[j] <= N; j ++) {
np[i * pri[j]] = 1;
if (i % pri[j] == 0) break;
}
for (int j = 1; j <= pc; j ++) {
if (1ll * pri[j] * cur[j] <= i) {
cur[j] *= pri[j];
for (int k = 1; k <= pc; k ++) {
if (k != j) prod[k] = 1ll * prod[k] * pri[j] % num[k];
}
}
}
if (mark[i]) {
for (int j = 1; j <= pc; j ++) {
// fprintf(stderr, "j = %d, p = %d, prod = %d\n", j, pri[j], prod[j]);
int a = prod[j] % cur[j], b = cur[j], x, y;
assert(exgcd(a, b, x, y) == 1);
if (x < 0) x += b;
ans[i].emplace_back(x, b);
}
}
}
for (int i = 1; i <= T; i ++) {
double sum = 0;
bool flag = false;
for (auto [a, b] : ans[qry[i]]) {
sum += (double)a / b;
if (flag) {
putchar('+');
}else {
flag = true;
}
printf("%d/%d", a, b);
}
printf("-%d/1\n", (int)floor(sum));
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 184ms
memory: 3968kb
input:
2 3 6
output:
1/2+2/3-1/1 3/4+2/3+3/5-2/1
result:
ok OK, t = 2
Test #2:
score: -100
Wrong Answer
time: 184ms
memory: 5968kb
input:
1 1
output:
-0/1
result:
wrong answer Numerator = 0 is out of range [1..1]