QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#824821#4987. 图rizynvu100 ✓895ms1022140kbC++141.3kb2024-12-21 16:00:202024-12-21 16:00:23

Judging History

This is the latest submission verdict.

  • [2024-12-21 16:00:23]
  • Judged
  • Verdict: 100
  • Time: 895ms
  • Memory: 1022140kb
  • [2024-12-21 16:00:20]
  • Submitted

answer

#include<bits/stdc++.h>
using ll = long long;
constexpr int maxn = 3e7 + 10, N = 3e7, M = 1e7;
ll mod;
inline ll qpow(ll a, ll b, ll v = 1) {
   while (b) {
      if (b & 1) (v *= a) %= mod;
      (a *= a) %= mod, b >>= 1;
   }
   return v;
}
ll fac[maxn], ifac[maxn], inv[maxn];
inline ll binom(int n, int m) {
   return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
ll f[maxn], g[maxn];
int main() {
   int q;
   scanf("%d%lld", &q, &mod);
   for (int i = fac[0] = 1; i <= N; i++) fac[i] = fac[i - 1] * i % mod;
   ifac[N] = qpow(fac[N], mod - 2);
   for (int i = N; i; i--) ifac[i - 1] = ifac[i] * i % mod;
   for (int i = inv[0] = 1; i <= N; i++) inv[i] = ifac[i] * fac[i - 1] % mod;
   const ll inv2 = inv[2], inv6 = inv[6];
   ll pw = 1ll;
   for (int i = 0; i <= M; i++) {
      f[i] = fac[3 * i] * ifac[i] % mod * pw % mod;
      (pw *= inv6) %= mod;
   }
   g[0] = g[1] = 1, g[2] = inv[2];
   for (int i = 3; i <= N; i++) {
      g[i] = (g[i - 1] + g[i - 3] * inv2 % mod) * inv[i] % mod;
   }
   for (int n; q--; ) {
      scanf("%d", &n);
      if (n % 3 == 0) {
         printf("%lld\n", f[n / 3] % mod);
         continue;
      }
      ll ans = g[n - 1] * fac[n] % mod;
      if (n % 3 == 2) {
         (ans += mod - binom(n, 2) * f[n / 3] % mod) %= mod;
      }
      printf("%lld\n", ans);
   }
   return 0;
}

详细

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 883ms
memory: 1022068kb

input:

8 998244353
1
2
3
4
5
6
7
8

output:

1
1
1
8
15
10
217
568

result:

ok 8 numbers

Subtask #2:

score: 15
Accepted

Dependency #1:

100%
Accepted

Test #2:

score: 15
Accepted
time: 878ms
memory: 1022032kb

input:

200 321402013
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
9...

output:

1
1
1
8
15
10
217
568
280
12050
39831
15400
1123993
4448458
1401400
157764896
80087671
190590400
187957435
189964100
215150544
210537812
320589946
116147435
77322237
258401726
143880854
163578365
60466855
241862371
40755093
241051099
80785167
247356411
251514276
178274428
312164931
154916780
3015398...

result:

ok 200 numbers

Subtask #3:

score: 15
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Test #3:

score: 15
Accepted
time: 895ms
memory: 1022140kb

input:

3000 621098477
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
...

output:

1
1
1
8
15
10
217
568
280
12050
39831
15400
1123993
4448458
1401400
157764896
101793220
190590400
608725310
26465057
188464334
181746076
546229079
477992250
553788308
95041077
72862000
377999633
453324334
390343581
378383862
26333899
448789829
467634276
16859121
578701622
560223528
54126595
7737831
...

result:

ok 3000 numbers

Subtask #4:

score: 20
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Test #4:

score: 20
Accepted
time: 858ms
memory: 1022140kb

input:

100000 987654337
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
9...

output:

1
1
1
8
15
10
217
568
280
12050
39831
15400
1123993
4448458
1401400
157764896
722891697
190590400
425266236
890968006
656619868
590941449
783143242
198897988
819863574
553350865
444314195
349243413
573657492
638473836
972976899
210482843
633634816
467619442
823146932
716413123
883383200
284524945
92...

result:

ok 100000 numbers

Subtask #5:

score: 30
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Test #5:

score: 30
Accepted
time: 832ms
memory: 1022124kb

input:

100000 987654439
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
9...

output:

1
1
1
8
15
10
217
568
280
12050
39831
15400
1123993
4448458
1401400
157764896
722891697
190590400
425263074
890951482
656616196
590100051
778293856
197951836
535903428
732607737
136808165
102841042
564792893
235466406
348855375
548639079
248113574
958091288
425942518
467065119
697029461
583676776
44...

result:

ok 100000 numbers