QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#863762 | #9962. Diminishing Fractions | hos_lyric | AC ✓ | 128ms | 11380kb | C++14 | 3.6kb | 2025-01-19 22:03:37 | 2025-01-19 22:03:37 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// a x + b y = (+/-) gcd(a, b)
// (a, 0): g = a, x = 1, y = 0
// (0, b), (b, b), (-b, b) (b != 0): g = b, x = 0, y = 1
// otherwise: 2 |x| <= |b|, 2 |y| <= |a|
// S: signed integer
template <class S> S gojo(S a, S b, S &x, S &y) {
if (b != 0) {
const S g = gojo(b, a % b, y, x);
y -= (a / b) * x;
return g;
} else {
x = 1;
y = 0;
return a;
}
}
/*
\sum[i] x[i]/p[i]^e[i] \in \Z
<=> ((\prod[j] p[j]^e[j]) / p[i]^e[i]) x[i] == 1 (mod p[i]^e[i])
*/
constexpr int N = 50010;
int lpf[N];
int psLen;
int ps[N], qs[N];
Int ms[N], prods[N];
bool asked[N];
string ans[N];
int main() {
int T;
for (; ~scanf("%d", &T); ) {
vector<int> Ns(T);
for (int t = 0; t < T; ++t) {
scanf("%d", &Ns[t]);
}
fill(asked, asked + N, false);
for (int t = 0; t < T; ++t) asked[Ns[t]] = true;
for (int p = 2; p < N; ++p) lpf[p] = p;
psLen = 0;
for (int p = 2; p < N; ++p) if (lpf[p] == p) {
ps[psLen++] = p;
for (int n = p; n < N; n += p) chmin(lpf[n], p);
}
for (int i = 0; i < psLen; ++i) {
const int p = ps[i];
int &q = qs[i];
for (q = p; q <= N / p; q *= p) {}
ms[i] = 1;
prods[i] = 1;
}
// cerr<<"qs = ";pv(qs,qs+psLen);
for (int n = 2; n < N; ++n) {
{
const int p = lpf[n];
int nn = n;
do { nn /= p; } while (nn % p == 0);
if (nn == 1) {
for (int i = 0; i < psLen; ++i) {
if (ps[i] == p) {
ms[i] *= p;
} else {
(prods[i] *= p) %= qs[i];
}
}
}
}
if (asked[n]) {
// cerr<<"n = "<<n<<endl;
double sum = 0.0;
for (int i = 0; i < psLen && ps[i] <= n; ++i) {
// cerr<<" "<<prods[i]<<"^-1 mod "<<ms[i]<<endl;
Int x, y;
gojo(prods[i], ms[i], x, y);
if ((x %= ms[i]) < 0) x += ms[i];
sum += (double)x / ms[i];
ans[n] += '+';
ans[n] += to_string(x);
ans[n] += '/';
ans[n] += to_string(ms[i]);
}
const int x0 = -round(sum - 0.25);
if (x0) ans[n] = to_string(x0) + "/1" + ans[n];
if (ans[n][0] == '+') ans[n] = ans[n].substr(1);
}
}
ans[1] = "1/1";
for (int t = 0; t < T; ++t) {
puts(ans[Ns[t]].c_str());
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 77ms
memory: 6712kb
input:
2 3 6
output:
-1/1+1/2+2/3 -2/1+3/4+2/3+3/5
result:
ok OK, t = 2
Test #2:
score: 0
Accepted
time: 79ms
memory: 6440kb
input:
1 1
output:
1/1
result:
ok OK, t = 1
Test #3:
score: 0
Accepted
time: 78ms
memory: 5760kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1/1 1/2 -1/1+1/2+2/3 -1/1+3/4+1/3 -2/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 -1/1+1/8+1/3+2/5+1/7 -2/1+3/8+1/9+4/5+5/7 -2/1+3/8+1/9+4/5+5/7
result:
ok OK, t = 10
Test #4:
score: 0
Accepted
time: 79ms
memory: 5888kb
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:
-2/1+1/4+2/3+4/5+2/7 -5/1+15/16+5/9+3/5+2/7+9/11+4/13+12/17+15/19 1/2 -1/1+1/2+2/3 -1/1+3/4+1/3 -2/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 -1/1+1/8+1/3+2/5+1/7 -2/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 -4/1+5/8+8/9+3/5+4/7+6/11+10/13 -4...
result:
ok OK, t = 54
Test #5:
score: 0
Accepted
time: 79ms
memory: 5760kb
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:
-9/1+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 -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+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/2...
result:
ok OK, t = 92
Test #6:
score: 0
Accepted
time: 86ms
memory: 7196kb
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:
-58/1+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/19...
result:
ok OK, t = 1000
Test #7:
score: 0
Accepted
time: 96ms
memory: 7552kb
input:
1000 1748 1741 1576 1940 1648 1825 1183 1447 1318 1277 1913 1208 1417 1388 1143 1581 1222 1904 1407 1006 1175 1218 1734 1934 1003 1704 1984 1399 1333 1840 1317 1233 1133 1232 1776 1881 1476 1712 1401 1231 1978 1964 1419 1644 1103 1498 1454 1480 1377 1352 1837 1616 1009 1413 1199 1977 1477 1579 1920 ...
output:
-135/1+275/1024+410/729+319/625+190/343+1226/1331+162/169+114/289+169/361+302/529+370/841+7/961+299/1369+958/1681+21/43+23/47+47/53+14/59+19/61+57/67+26/71+14/73+20/79+43/83+65/89+63/97+7/101+60/103+88/107+4/109+107/113+92/127+84/131+20/137+77/139+142/149+64/151+108/157+91/163+104/167+49/173+20/179+...
result:
ok OK, t = 1000
Test #8:
score: 0
Accepted
time: 110ms
memory: 9284kb
input:
1000 2107 2115 2985 2832 2564 2529 2971 2637 2666 2172 2496 2191 2465 2199 2260 2161 2402 2369 2762 2674 2734 2252 2488 2185 2652 2014 2018 2960 2313 2063 2696 2976 2457 2247 2180 2647 2907 2901 2912 2538 2512 2623 2267 2986 2348 2170 2131 2166 2563 2111 2860 2254 2462 2080 2054 2803 2397 2778 2411 ...
output:
-158/1+85/2048+596/729+193/625+214/343+167/1331+18/169+103/289+124/361+275/529+487/841+532/961+1087/1369+582/1681+91/1849+14/47+31/53+57/59+58/61+22/67+54/71+35/73+41/79+33/83+26/89+29/97+43/101+23/103+54/107+55/109+57/113+67/127+58/131+38/137+115/139+5/149+52/151+102/157+97/163+146/167+124/173+157/...
result:
ok OK, t = 1000
Test #9:
score: 0
Accepted
time: 120ms
memory: 11252kb
input:
1000 3416 3784 3793 3610 3982 3174 3253 3088 3197 3926 3664 3669 3431 3821 3340 3298 3498 3627 3194 3354 3362 3512 3865 3529 3988 3973 3852 3552 3672 3282 3035 3639 3998 3090 3771 3618 3402 3139 3903 3110 3452 3947 3941 3225 3187 3283 3243 3722 3808 3491 3309 3935 3937 3259 3909 3665 3809 3390 3285 ...
output:
-238/1+921/2048+1583/2187+3038/3125+2063/2401+101/1331+1412/2197+73/289+102/361+7/529+24/841+637/961+849/1369+1339/1681+931/1849+322/2209+2764/2809+30/59+58/61+10/67+1/71+37/73+10/79+11/83+81/89+37/97+29/101+54/103+20/107+20/109+23/113+21/127+106/131+102/137+43/139+17/149+41/151+22/157+48/163+104/16...
result:
ok OK, t = 1000
Test #10:
score: 0
Accepted
time: 125ms
memory: 11348kb
input:
899 4695 4616 4545 4771 4315 4850 4821 4722 4493 4338 4566 4144 4721 4334 4536 4313 4264 4669 4648 4780 4551 4044 4506 4798 4483 4372 4371 4636 4650 4590 4340 4383 4756 4104 4077 4067 4692 4008 4141 4610 4532 4751 4345 4498 4214 4621 4519 4505 4681 4726 4011 4879 4103 4283 4470 4844 4520 4740 4333 4...
output:
-318/1+2597/4096+1553/2187+487/3125+1742/2401+651/1331+144/2197+275/289+177/361+252/529+439/841+537/961+1060/1369+627/1681+547/1849+587/2209+1847/2809+554/3481+1664/3721+2220/4489+10/71+44/73+65/79+13/83+21/89+71/97+39/101+91/103+55/107+90/109+101/113+45/127+98/131+70/137+101/139+70/149+87/151+10/15...
result:
ok OK, t = 899
Test #11:
score: 0
Accepted
time: 113ms
memory: 9596kb
input:
1000 3798 4097 3356 3197 4364 3360 3927 4704 4072 3229 3681 3276 4187 4013 4014 3134 3743 3208 4792 3235 4788 3953 3313 4147 3230 3497 3181 4376 4631 3710 3602 4191 3405 3381 4722 3607 4374 3037 3149 3682 3557 3338 4471 3242 4470 4753 4637 3343 3625 4516 3505 3553 3626 3073 3222 4514 4402 4063 3372 ...
output:
-255/1+2037/2048+1064/2187+3074/3125+527/2401+772/1331+1793/2197+172/289+60/361+29/529+791/841+827/961+1154/1369+1633/1681+1084/1849+327/2209+482/2809+122/3481+2786/3721+53/67+55/71+43/73+55/79+59/83+5/89+57/97+99/101+17/103+56/107+17/109+51/113+115/127+58/131+19/137+129/139+4/149+55/151+56/157+1/16...
result:
ok OK, t = 1000
Test #12:
score: 0
Accepted
time: 122ms
memory: 9984kb
input:
474 7545 5913 9012 7937 9033 8958 6042 9802 6773 7104 7992 7475 7128 5208 6469 5645 7483 49664 7929 9114 8828 7916 9405 6829 8448 8454 9204 7795 5960 6310 9545 49393 8789 5482 8149 7405 8428 7210 5902 8761 5209 8251 7599 9264 5237 8710 6878 8842 5430 9871 8230 8959 9001 8926 9201 5003 8737 7628 7133...
output:
-481/1+117/4096+4606/6561+2117/3125+1928/2401+488/1331+1865/2197+2833/4913+40/6859+58/529+437/841+944/961+970/1369+92/1681+477/1849+1977/2209+860/2809+1892/3481+1004/3721+769/4489+3978/5041+1939/5329+3261/6241+547/6889+62/89+76/97+29/101+65/103+89/107+63/109+37/113+62/127+82/131+59/137+127/139+40/14...
result:
ok OK, t = 474
Test #13:
score: 0
Accepted
time: 123ms
memory: 10724kb
input:
505 654 4006 658 39050 749 2525 45144 8 107 366 181 1161 49410 315 24 1125 56 2895 4689 19600 861 18 17643 27130 4321 20973 43326 5761 3127 514 4286 286 55 4733 1114 1022 540 15500 47 158 2168 529 428 41729 1778 83 53 904 416 25550 134 475 2624 386 13073 4799 16 484 27746 7 35604 37 1814 1384 45 377...
output:
-57/1+99/512+136/243+149/625+251/343+35/121+147/169+251/289+162/361+74/529+7/29+11/31+13/37+8/41+28/43+42/47+30/53+24/59+41/61+4/67+58/71+31/73+6/79+68/83+60/89+69/97+51/101+80/103+65/107+3/109+16/113+21/127+70/131+46/137+21/139+144/149+98/151+156/157+133/163+60/167+58/173+177/179+153/181+148/191+77...
result:
ok OK, t = 505
Test #14:
score: 0
Accepted
time: 128ms
memory: 10848kb
input:
164 5348 13781 10630 42085 48735 22502 30143 8994 17829 40332 4188 40168 2858 4539 27532 42223 44889 33093 47647 8530 48824 812 14976 32746 29270 44536 33166 2354 27686 630 43523 32120 38166 6687 1125 45588 46641 42232 29120 11682 17398 3236 7499 48684 43474 11053 182 13451 43129 35818 15957 11254 4...
output:
-352/1+757/4096+674/2187+989/3125+2301/2401+18/1331+1102/2197+131/4913+344/361+393/529+127/841+439/961+587/1369+1393/1681+930/1849+509/2209+2570/2809+1943/3481+413/3721+2705/4489+4721/5041+3510/5329+27/79+39/83+51/89+46/97+93/101+95/103+95/107+63/109+5/113+113/127+90/131+67/137+77/139+96/149+123/151...
result:
ok OK, t = 164
Test #15:
score: 0
Accepted
time: 125ms
memory: 9984kb
input:
156 11299 49780 39754 44360 13750 33643 31382 48665 22685 25974 3679 15594 20849 11844 40645 20943 26660 48724 29993 21829 26251 7053 15218 29474 31529 31754 2823 4866 17437 40827 38285 3116 32140 37739 3831 29989 28174 13666 24668 41569 45278 43765 47564 39670 35238 28460 21784 45880 9557 38904 499...
output:
-692/1+7077/8192+1588/6561+2944/3125+1969/2401+1006/1331+451/2197+3797/4913+2370/6859+433/529+443/841+879/961+283/1369+73/1681+1683/1849+606/2209+2253/2809+2900/3481+145/3721+3386/4489+2917/5041+2070/5329+3527/6241+3736/6889+3230/7921+4302/9409+8038/10201+9430/10609+68/107+4/109+89/113+23/127+13/131...
result:
ok OK, t = 156
Test #16:
score: 0
Accepted
time: 119ms
memory: 9984kb
input:
89 42615 41319 46294 44842 45088 40377 45143 42991 47088 47811 44569 40910 41531 49894 45027 47842 48327 48243 49764 46548 46950 41965 49765 49084 41705 40820 48320 46963 42990 42063 42431 46354 40397 39081 47952 45441 41398 40985 45235 47640 44526 41162 40397 47458 46986 49919 45345 49801 43912 425...
output:
-2204/1+41/32768+17947/19683+424/15625+2904/16807+2264/14641+28519/28561+1497/4913+6735/6859+6549/12167+23058/24389+26650/29791+1224/1369+380/1681+850/1849+831/2209+1539/2809+2329/3481+3092/3721+4121/4489+4997/5041+3755/5329+4319/6241+5786/6889+1992/7921+1650/9409+6919/10201+7956/10609+6279/11449+10...
result:
ok OK, t = 89
Test #17:
score: 0
Accepted
time: 126ms
memory: 10364kb
input:
708 8690 6897 1563 9172 7717 7283 5164 2859 7604 6259 5595 8855 7487 5464 2724 1949 6643 6878 8975 4077 9549 1489 2960 4831 4874 5669 7869 5594 1092 9912 3233 4090 7631 5734 6151 4286 5171 7006 6875 2487 2383 5073 1110 3362 3032 6497 7582 7001 5070 4532 8695 2019 3678 2955 2379 9183 3355 8851 3049 8...
output:
-537/1+7979/8192+1697/6561+1994/3125+34/2401+1248/1331+875/2197+4889/4913+6181/6859+117/529+249/841+526/961+500/1369+1532/1681+1723/1849+36/2209+1655/2809+2606/3481+1152/3721+3669/4489+829/5041+3554/5329+4904/6241+2013/6889+2254/7921+35/97+32/101+48/103+14/107+16/109+31/113+53/127+89/131+82/137+24/1...
result:
ok OK, t = 708
Test #18:
score: 0
Accepted
time: 125ms
memory: 10496kb
input:
84 48657 48005 48587 46114 48091 46487 49367 46438 46342 49209 49724 46640 49786 48261 48525 48488 46034 46558 48181 48315 47021 46848 48476 49853 48898 49808 46580 46793 46961 49425 47721 48827 48081 47505 47240 49031 46320 47091 47584 49823 46483 46527 49809 48285 49970 46559 46599 47692 49083 490...
output:
-2531/1+28837/32768+9544/19683+8962/15625+2435/16807+3312/14641+26576/28561+193/4913+5407/6859+11824/12167+7120/24389+16188/29791+603/1369+378/1681+200/1849+70/2209+191/2809+2022/3481+1394/3721+208/4489+23/5041+5274/5329+2501/6241+2134/6889+4602/7921+1018/9409+462/10201+9182/10609+3473/11449+60/1188...
result:
ok OK, t = 84
Test #19:
score: 0
Accepted
time: 123ms
memory: 10240kb
input:
80 49938 49968 49950 49924 49989 49971 49926 49999 49982 49930 49974 49957 49947 49992 49948 49941 49998 49967 49978 49965 49997 49963 49923 49981 49958 49975 49937 49942 49996 49993 49979 49961 49931 49956 49944 49987 49953 49933 49943 50000 49970 49940 49990 49927 49951 49955 49934 49994 49972 499...
output:
-2550/1+3721/32768+19006/19683+10263/15625+3688/16807+11575/14641+6514/28561+174/4913+4926/6859+1330/12167+15727/24389+21554/29791+11/1369+499/1681+968/1849+640/2209+1339/2809+1537/3481+1873/3721+1585/4489+3547/5041+4590/5329+3442/6241+5331/6889+370/7921+5890/9409+7821/10201+643/10609+7232/11449+304...
result:
ok OK, t = 80
Test #20:
score: 0
Accepted
time: 122ms
memory: 10992kb
input:
81 49999 49993 49991 49957 49943 49939 49937 49927 49921 49919 49891 49877 49871 49853 49843 49831 49823 49811 49807 49801 49789 49787 49783 49757 49747 49741 49739 49729 49727 49711 49697 49681 49669 49667 49663 49639 49633 49627 49613 49603 49597 49559 49549 49547 49537 49531 49529 49523 49499 494...
output:
-2547/1+19953/32768+8914/19683+2866/15625+6179/16807+8714/14641+26246/28561+3061/4913+2680/6859+6499/12167+5025/24389+10624/29791+264/1369+1225/1681+1398/1849+1080/2209+2698/2809+2703/3481+2032/3721+3975/4489+19/5041+325/5329+6102/6241+4025/6889+3881/7921+383/9409+6631/10201+2569/10609+8470/11449+71...
result:
ok OK, t = 81
Test #21:
score: 0
Accepted
time: 123ms
memory: 10240kb
input:
83 48990 48988 48972 48952 48946 48906 48888 48882 48870 48868 48858 48856 48846 48822 48820 48816 48808 48798 48786 48780 48778 48766 48760 48756 48750 48732 48730 48678 48676 48672 48660 48648 48646 48622 48618 48610 48592 48588 48570 48562 48540 48538 48532 48526 48522 48496 48490 48486 48480 484...
output:
-2531/1+17613/32768+19291/19683+2884/15625+4421/16807+654/14641+5709/28561+3119/4913+858/6859+10628/12167+20714/24389+11429/29791+149/1369+1350/1681+33/1849+734/2209+1494/2809+1833/3481+3458/3721+1179/4489+2631/5041+4485/5329+3174/6241+3741/6889+2416/7921+2206/9409+137/10201+10363/10609+153/11449+49...
result:
ok OK, t = 83
Test #22:
score: 0
Accepted
time: 124ms
memory: 11380kb
input:
91 49999 49877 49747 49613 49481 49339 49211 49081 48953 48823 48679 48541 48413 48281 48157 48029 47903 47779 47657 47533 47407 47279 47149 47017 46889 46757 46633 46511 46381 46237 46103 45979 45853 45707 45569 45439 45317 45191 45061 44939 44809 44687 44563 44417 44293 44171 44041 43913 43789 436...
output:
-2547/1+19953/32768+8914/19683+2866/15625+6179/16807+8714/14641+26246/28561+3061/4913+2680/6859+6499/12167+5025/24389+10624/29791+264/1369+1225/1681+1398/1849+1080/2209+2698/2809+2703/3481+2032/3721+3975/4489+19/5041+325/5329+6102/6241+4025/6889+3881/7921+383/9409+6631/10201+2569/10609+8470/11449+71...
result:
ok OK, t = 91
Extra Test:
score: 0
Extra Test Passed