QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#255446 | #7749. A Simple MST Problem | ucup-team173# | TL | 884ms | 30648kb | C++17 | 3.4kb | 2023-11-18 15:56:25 | 2023-11-18 15:56:25 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define Mp make_pair
#define SZ(x) (int((x).size()))
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
constexpr int N = 1000100;
int v[N], p[N], m;
void solve() {
v[1] = 1;
int l, r;
cin >> l >> r;
ll ans = 0;
set<int> st;
for(int i = l; i <= r; i++) {
vi pr; int x = 1;
for(int j = i; j > 1; j /= v[j])
if(SZ(pr) && pr.back() != v[j]) pr.pb(v[j]), x *= v[j];
else if(!SZ(pr)) pr.pb(v[j]), x *= v[j];
if(st.find(x) == st.end()) st.insert(x);
else ans += SZ(pr);
}
vi nums;
for(auto x : st) nums.pb(x);
if(SZ(nums) <= 1e3) {
vector<array<int, 3>> edges;
v[1] = 1e9;
for(int i = 0; i < SZ(nums); i++) {
for(int j = i + 1; j < SZ(nums); j++) {
int x = nums[i], y = nums[j];
int ans = 0;
while(x > 1 || y > 1){
if(v[x] == v[y]){
ans++;
x /= v[x], y /= v[y];
}
else{
ans++;
if(v[x] < v[y]) x /= v[x];
else y /= v[y];
}
}
edges.pb({ans, i, j});
}
}
sort(edges.begin(), edges.end());
vi f(SZ(nums));
iota(f.begin(), f.end(), 0);
function<int(int)> find = [&](int x) {
return f[x] == x ? x : f[x] = find(f[x]);
};
for(auto [val, i, j] : edges) {
int x = find(i), y = find(j);
if(x != y) {
f[x] = y;
ans += val;
}
}
cout << ans << '\n';
return;
}
vi prcnt(r + 1);
for(int x : nums) {
vi pr;
for(int j = x; j > 1; j /= v[j])
pr.pb(v[j]);
prcnt[x] = SZ(pr);
}
sort(nums.begin(), nums.end(), [&](int x, int y) {
return prcnt[x] < prcnt[y];
});
vi f(r + 1, 1e9);
for(int i = 0; i < SZ(nums); i++) {
int x = nums[i];
vi pr, vals, cnt;
for(int j = x; j > 1; j /= v[j])
pr.pb(v[j]);
function<void(int, int, int)> dfs = [&](int x, int nw, int c) {
if(x == SZ(pr)) {
vals.pb(nw);
cnt.pb(c);
return;
}
dfs(x + 1, nw, c);
dfs(x + 1, nw * pr[x], c + 1);
};
dfs(0, 1, 0);
if(i) {
int now = 1e9;
for(auto v : vals) {
now = min(now, f[v] + SZ(pr));
}
// cout << " " << now << '\n';
ans += now;
}
for(int j = 0; j < SZ(vals); j++) {
f[vals[j]] = min(f[vals[j]], SZ(pr) - cnt[j]);
}
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
v[1] = 1;
for(int i = 2; i <= 1e6; i++) {
if(!v[i]) v[i] = i, p[++m] = i;
for(int j = 1; j <= m && p[j] * i <= 1e6; j++) {
v[i * p[j]] = p[j];
if(v[i] == p[j]) break;
}
}
int t = 1;
cin >> t;
while(t--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 18ms
memory: 12880kb
input:
5 1 1 4 5 1 4 1 9 19 810
output:
0 2 3 9 1812
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 44ms
memory: 14156kb
input:
2 27 30 183704 252609
output:
8 223092
result:
ok 2 lines
Test #3:
score: 0
Accepted
time: 44ms
memory: 14080kb
input:
1 183704 252609
output:
223092
result:
ok single line: '223092'
Test #4:
score: 0
Accepted
time: 196ms
memory: 30648kb
input:
2 639898 942309 30927 34660
output:
983228 11512
result:
ok 2 lines
Test #5:
score: 0
Accepted
time: 149ms
memory: 22892kb
input:
3 21731 33468 46192 370315 1712 3753
output:
34608 948775 5299
result:
ok 3 lines
Test #6:
score: 0
Accepted
time: 60ms
memory: 12480kb
input:
4 15237 67700 10918 86104 98287 116980 17432 23592
output:
148811 210927 60893 18687
result:
ok 4 lines
Test #7:
score: 0
Accepted
time: 307ms
memory: 26656kb
input:
5 5594 70302 19202 69588 5634 7877 59821 469300 97439 261121
output:
179735 145706 6565 1203684 488669
result:
ok 5 lines
Test #8:
score: 0
Accepted
time: 252ms
memory: 17096kb
input:
6 43477 229639 188276 269887 72424 150178 9009 36918 137421 180271 14666 124530
output:
541705 255651 232054 77284 135277 313203
result:
ok 6 lines
Test #9:
score: 0
Accepted
time: 131ms
memory: 13988kb
input:
7 461436 501798 98856 148334 20953 119408 910 5027 762 14117 10959 46088 96149 130311
output:
139017 151124 281536 10504 34818 98004 108375
result:
ok 7 lines
Test #10:
score: 0
Accepted
time: 315ms
memory: 22332kb
input:
8 6448 11162 33691 94044 137536 140277 106719 267437 13494 38185 3185 4173 4835 299526 25162 43201
output:
13177 175485 9711 480992 69059 2808 840950 53422
result:
ok 8 lines
Test #11:
score: 0
Accepted
time: 221ms
memory: 16580kb
input:
9 4136 54985 38425 155322 107602 157683 115533 137627 13677 44903 43432 69310 70004 129314 43033 55373 76424 147123
output:
139668 339337 153266 68520 87592 76612 183238 39109 211339
result:
ok 9 lines
Test #12:
score: 0
Accepted
time: 236ms
memory: 16016kb
input:
10 21662 27103 55634 196143 20466 44902 87028 202799 127745 151602 1598 1679 95299 126981 13367 134183 52307 66285 10136 38071
output:
17117 410126 71979 344673 74754 263 100586 342577 41870 77522
result:
ok 10 lines
Test #13:
score: 0
Accepted
time: 215ms
memory: 13228kb
input:
20 14973 29525 12674 35281 27500 64458 67431 109122 12468 19466 4606 9884 38731 44161 3212 89277 23213 37134 4392 40769 5378 7211 22586 29237 56331 81369 43924 59554 31787 34990 19949 21972 47309 65085 5666 48185 99 2714 7969 131566
output:
40882 63073 107649 129480 19975 14563 17562 238237 41056 99346 5358 20747 73854 48244 9911 6517 54866 117382 6374 347901
result:
ok 20 lines
Test #14:
score: 0
Accepted
time: 227ms
memory: 14620kb
input:
30 11101 53557 6079 6241 869 14433 6164 10602 73432 82272 15845 17496 18885 49518 12127 35037 5812 14898 12225 15757 19736 36027 34506 69210 12204 37099 642 1256 11875 12382 169453 211949 20884 26173 8302 26634 75302 79147 13938 16896 11229 13736 4753 7575 2742 17752 4443 5021 2988 5533 1042 1364 19...
output:
118619 538 35473 12392 28768 4915 88426 63728 25217 10666 47893 102086 69488 1584 1669 135374 16581 50701 12720 8517 7762 8170 40235 1798 7014 936 78143 29 32461 19423
result:
ok 30 lines
Test #15:
score: 0
Accepted
time: 261ms
memory: 12096kb
input:
40 1022 1378 14032 55415 12506 15792 3919 16767 12870 32763 10624 12091 1144 29449 5184 9133 4178 8021 7785 13966 3880 26749 15390 34240 2582 11246 431 4695 7020 28894 14092 27156 52666 55295 4068 22068 7392 11533 18273 31481 18854 47481 7786 39812 7341 24968 22021 54790 3221 10332 4930 37633 4407 1...
output:
959 116367 9946 34920 55460 4626 75707 10961 11069 17829 62128 52987 23362 10769 60198 36594 9093 48818 11976 39528 82591 88609 48598 96601 19475 89551 19435 27135 16967 139445 1063 181709 57729 6335 4114 5586 5189 5669 59793 8451
result:
ok 40 lines
Test #16:
score: 0
Accepted
time: 295ms
memory: 22904kb
input:
50 15626 23807 5585 6016 6101 8103 9127 21310 36189 45079 14069 28358 4793 6034 4029 8938 35309 95923 43860 65991 3472 11896 13230 36032 11979 20636 2432 24320 1680 8915 2612 5242 9797 10319 2232 9531 24754 30470 41799 71066 9681 10551 434 2733 8898 28848 2033 35179 8338 13387 1547 5093 1840 3757 47...
output:
23466 1386 5889 33690 28343 40048 3754 13493 176378 65680 23137 63701 24080 59083 18974 7221 1739 19409 17905 86553 2807 5709 55079 89488 15241 9203 4985 12067 5112 815 36773 6032 21936 48301 14796 43205 40728 1000 10389 346 16233 134 5168 54990 5999 25607 7930 9444 25040 30739
result:
ok 50 lines
Test #17:
score: 0
Accepted
time: 276ms
memory: 17252kb
input:
60 1284 4788 3389 36000 222 7286 25716 33186 38085 40233 3531 6181 774 2555 2653 3785 1441 2911 5474 6951 7325 8378 623 1484 3626 29205 863 14748 49190 61921 2406 35177 7742 9801 200 1071 5199 13189 7551 9135 25991 50726 17330 21478 46 97 1580 6205 7679 16145 7074 19995 17354 41321 7546 9420 3186 71...
output:
9024 88511 17924 22330 7191 7459 4538 3287 3846 4320 3130 2162 69268 36338 39569 88635 5995 2088 22192 4684 73244 12692 112 12062 23979 35542 68915 5472 10740 464 301 171 1183 29157 16502 38047 16131 3142 4940 51225 10426 4988 20823 38873 1185 31126 665 11014 128 9270 4447 12193 22928 10020 521 5876...
result:
ok 60 lines
Test #18:
score: 0
Accepted
time: 336ms
memory: 20652kb
input:
70 2967 4866 39014 52178 4501 9931 2868 17671 18883 24548 44427 53286 9239 37925 1019 1411 234 3511 4974 11964 3336 7079 2397 2807 1482 5860 845 967 47 3347 22581 38595 618 9784 1987 6683 1970 10280 13073 16008 12155 23980 26996 33533 1211 2524 116 3464 292 360 342 432 10396 22134 21754 66962 10977 ...
output:
5260 41323 14979 39729 17180 28404 79481 1059 8102 19420 10197 1225 11392 373 8086 47200 23657 12336 21966 8903 32960 19601 3385 8239 193 250 32568 131030 41881 26631 57583 18937 22825 27887 7666 45760 37714 4084 1599 7021 13489 3750 22270 13773 686 10747 2711 1325 15075 206 12399 28434 14321 5809 1...
result:
ok 70 lines
Test #19:
score: 0
Accepted
time: 721ms
memory: 20660kb
input:
80 227 762 1864 11313 3710 6984 9210 16876 2146 5715 12841 17952 540 3928 2471 3084 1297 10323 1336 4968 2550 2572 33143 35415 821 1994 1397 14292 6850 16209 9375 15494 829 4086 6431 26191 20951 28745 3738 19999 101 4288 2971 5562 809 1230 21199 26540 181 386 11348 11762 2783 5152 17773 39328 2469 3...
output:
1273 25013 8944 21618 9356 14607 8548 1852 23539 9384 74 7598 3126 34097 26366 18382 8268 54251 24042 43950 10390 7142 1132 16776 486 1397 6513 61971 2251 2716 119 10648 11149 14387 88616 48016 3101 13598 42354 15386 761 11212 11013 15564 4022 23786 18860 39658 2971 20321 9970 65802 3429 2772 19206 ...
result:
ok 80 lines
Test #20:
score: 0
Accepted
time: 884ms
memory: 22572kb
input:
90 1834 3251 1286 4017 968 8098 991 1889 4679 9273 47 2565 404 3201 1698 5864 1568 3560 2090 4761 5519 20863 952 1123 6765 7477 1881 12407 22889 32445 1149 1341 5532 20827 2641 11993 4531 5449 1440 16324 2378 29912 10725 14148 913 1665 3757 3940 2607 6438 663 1609 1134 19991 7011 7315 3043 4479 1183...
output:
3678 7004 18415 2406 12675 6103 6973 10872 5155 7008 41935 469 2265 27853 29404 566 41797 25248 2792 39305 74142 10472 2013 597 10541 2425 49947 1026 3977 1732 21450 11809 1816 1175 21684 176 24632 13337 2393 3679 145481 1240 1203 34363 15259 26644 14236 8201 5876 432 18294 211 1018 2099 28005 33979...
result:
ok 90 lines
Test #21:
score: 0
Accepted
time: 848ms
memory: 21724kb
input:
100 2851 7328 280 359 20953 49433 106 1188 195 208 5208 9395 8380 12027 8091 11163 3161 7447 215 3448 2022 7980 4069 7320 2838 3679 15 231 1888 14353 10452 12414 4 1185 789 1675 19521 23307 301 2895 6831 30967 33555 35031 483 2712 4746 21571 4294 13075 13819 13959 5986 15296 9258 9513 331 6202 2467 ...
output:
12003 221 83960 2553 41 11627 11086 8930 11682 7983 15657 8920 2482 466 33116 6113 2741 2360 11587 6407 66138 4987 5539 45716 24197 479 25858 866 14840 7126 11092 15742 1357 59273 7251 95126 5877 9720 19666 59359 3213 5286 7653 2305 8616 3020 718 7748 3928 6874 27620 0 23370 10227 7369 4750 59084 55...
result:
ok 100 lines
Test #22:
score: -100
Time Limit Exceeded
input:
500 2771 3702 817 1288 1215 5937 453 1488 3267 3321 2058 2466 703 825 9 97 1251 2046 234 1684 4778 7026 2763 5165 11 32 620 1738 367 1281 2469 5782 650 5733 138 479 1879 3268 124 1307 2795 3892 2069 3176 6336 7230 1176 2018 324 1422 1596 3589 680 1487 1904 2126 16 27 620 1118 52 215 4143 4516 511 62...