QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187548 | #3853. Lines in a grid | realIyxiang# | TL | 81ms | 95932kb | C++14 | 1.6kb | 2023-09-24 17:59:28 | 2023-09-24 17:59:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
const int N = 1e7 + 5;
const int P = 1e6 + 3;
int n, q, r, S, t, pra, cnt, ans, res, Inv, pr[N], mu[N], pre[N];
bool ipr[N];
int inc (int a, int b) { return (a += b) >= P ? a - P : a; }
int dec (int a, int b) { return (a -= b) < 0 ? a + P : a; }
int mul (int a, int b) { return 1ll * a * b % P; }
int fpow (int a, int b) { int ans = 1; for ( ; b; a = mul(a, a), b >>= 1) if(b & 1) ans = mul(ans, a); return ans; }
void sieve (int n) {
ipr[1] = mu[1] = 1;
rep(i, 2, n) {
if(!ipr[i]) pr[++cnt] = i, mu[i] = P - 1;
for (int j = 1; j <= cnt && i <= n / pr[j]; ++j) {
ipr[i * pr[j]] = 1;
if(i % pr[j] == 0) { mu[i * pr[j]] = 0; break ; }
ipr[i * pr[j]] = 1, mu[i * pr[j]] = dec(0, mu[i]);
}
}
rep(i, 1, n) pre[i] = inc(pre[i - 1], mu[i]);
}
int main () {
sieve(N - 5);
cin >> q;
pra = mul(3, fpow(4, P - 2));
Inv = fpow(2, P - 2);
while (q--) {
cin >> n, ans = n;
// rep(i, 1, n) printf("%d ", mu[i]); printf("\n");
if(n == 1) { puts("0"); continue ; }
rep(d, 1, n) {
int x = n / d;
res = mul(n, x);
res = dec(res, mul(d, mul(x, mul(x + 1, Inv))));
res = mul(res, res);
ans = inc(ans, mul(mu[d], res));
}
rep(d, 1, n / 2) {
int x = n / 2 / d;
res = mul(n, x);
res = dec(res, mul(2 * d, mul(x, mul(x + 1, Inv))));
res = mul(res, res);
ans = dec(ans, mul(mu[d], res));
}
printf("%d\n", mul(ans, 2));
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 79ms
memory: 95932kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
0 6 20 62 140 306 536 938 1492 2306
result:
ok 10 lines
Test #2:
score: 0
Accepted
time: 81ms
memory: 95056kb
input:
100 1 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
output:
0 6 20 62 140 306 536 938 1492 2306 3296 4722 6460 8830 11568 14946 18900 23926 29544 36510 44388 53586 63648 75674 88948 104374 121032 139966 160636 184466 209944 239050 270588 305478 342480 383370 427020 475830 527280 583338 642900 708798 777912 854022 934604 21071 111365 209991 313609 425767 5425...
result:
ok 100 lines
Test #3:
score: -100
Time Limit Exceeded
input:
1000 999000 999001 999002 999003 999004 999005 999006 999007 999008 999009 999010 999011 999012 999013 999014 999015 999016 999017 999018 999019 999020 999021 999022 999023 999024 999025 999026 999027 999028 999029 999030 999031 999032 999033 999034 999035 999036 999037 999038 999039 999040 999041 9...
output:
546070 193945 346418 377018 37466 751815 955839 157887 339995 522058 582121 473366 340311 290383 918701 862726 444011 438858 911955 921485 301270 699249 433065 907182 40424 772622 357721 211889 575810 629609 30983 876744 264169 808803 188203 748204 392664 267459 919490 320743 3927 490236 833633 9156...