QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#856872 | #9962. Diminishing Fractions | IllusionaryDominance# | WA | 196ms | 5924kb | C++20 | 2.4kb | 2025-01-14 18:28:20 | 2025-01-14 18:28:20 |
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 ++) {
if (qry[i] == 1) {
printf("1/1\n");
continue;
}else if (qry[i] == 2) {
printf("1/2\n");
continue;
}
long double sum = 0;
bool flag = false;
for (auto [a, b] : ans[qry[i]]) {
sum += (long double)a / b;
if (flag) {
putchar('+');
}else {
flag = true;
}
printf("%d/%d", a, b);
}
printf("-%d/1\n", (int)round(sum));
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 187ms
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: 0
Accepted
time: 187ms
memory: 3968kb
input:
1 1
output:
1/1
result:
ok OK, t = 1
Test #3:
score: 0
Accepted
time: 188ms
memory: 5924kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1/1 1/2 1/2+2/3-1/1 3/4+1/3-1/1 3/4+2/3+3/5-2/1 3/4+2/3+3/5-2/1 1/4+2/3+4/5+2/7-2/1 1/8+1/3+2/5+1/7-1/1 3/8+1/9+4/5+5/7-2/1 3/8+1/9+4/5+5/7-2/1
result:
ok OK, t = 10
Test #4:
score: 0
Accepted
time: 186ms
memory: 3968kb
input:
54 7 20 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 3 47 81
output:
1/4+2/3+4/5+2/7-2/1 15/16+5/9+3/5+2/7+9/11+4/13+12/17+15/19-5/1 1/2 1/2+2/3-1/1 3/4+1/3-1/1 3/4+2/3+3/5-2/1 3/4+2/3+3/5-2/1 1/4+2/3+4/5+2/7-2/1 1/8+1/3+2/5+1/7-1/1 3/8+1/9+4/5+5/7-2/1 3/8+1/9+4/5+5/7-2/1 1/8+5/9+4/5+3/7+1/11-2/1 1/8+5/9+4/5+3/7+1/11-2/1 5/8+8/9+3/5+4/7+6/11+10/13-4/1 5/8+8/9+3/5+4/7...
result:
ok OK, t = 54
Test #5:
score: 0
Accepted
time: 186ms
memory: 3968kb
input:
92 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 255 258 261 264 267 270 273 276 279 282 28...
output:
27/32+2/27+21/25+26/49+10/11+2/13+11/17+8/19+19/23+24/29+18/31+21/37+26/41+30/43+21/47-9/1 15/32+25/27+7/25+31/49+6/11+2/13+14/17+17/19+6/23+1/29+29/31+36/37+9/41+3/43+27/47+11/53-8/1 15/32+25/27+7/25+31/49+6/11+2/13+14/17+17/19+6/23+1/29+29/31+36/37+9/41+3/43+27/47+11/53-8/1 29/32+5/27+23/25+8/49+7...
result:
ok OK, t = 92
Test #6:
score: -100
Wrong Answer
time: 196ms
memory: 5120kb
input:
1000 622 149 839 472 292 345 799 941 449 15 907 494 48 430 505 66 83 97 10 548 286 644 528 249 573 860 557 932 746 262 971 157 603 715 984 333 53 916 784 649 70 768 780 64 118 616 979 466 24 2 517 774 566 419 182 222 40 169 951 830 977 383 355 770 134 973 917 3 854 31 35 825 109 945 671 536 952 888 ...
output:
169/512+106/243+2/125+190/343+95/121+11/169+288/289+24/361+355/529+7/29+21/31+28/37+15/41+25/43+41/47+4/53+18/59+51/61+50/67+18/71+66/73+47/79+77/83+82/89+85/97+90/101+11/103+66/107+76/109+28/113+16/127+91/131+34/137+23/139+7/149+129/151+75/157+8/163+20/167+70/173+138/179+40/181+130/191+120/193+14/1...
result:
wrong answer Numerator = 0 is out of range [1..895]