Submission #831035

# Submission time Handle Problem Language Result Execution time Memory
831035 2023-08-19T15:27:10 Z rainboy Party (INOI20_party) C
100 / 100
303 ms 400 KB
#include <stdio.h>

#define L	60	/* L = ceil(log2(10^18)) */
#define MD	1000000007
#define V2	500000004

int min(int a, int b) { return a < b ? a : b; }

long long power(long long a, long long k) {
	long long p = 1;

	if (k < 0)
		return 0;
	while (k) {
		if (k & 1)
			p = p * a % MD;
		a = a * a % MD;
		k >>= 1;
	}
	return p;
}

int inv(int a) {
	return a == 1 ? 1 : (long long) inv(a - MD % a) * (MD / a + 1) % MD;
}

int pp[L + 2], qq[L + 2], rr[L + 2], ss[L + 2], tt[L + 2];

void init() {
	int i;

	pp[0] = qq[0] = 1, rr[0] = 2, ss[0] = V2;
	for (i = 1; i <= L + 1; i++) {
		pp[i] = pp[i - 1] * 2 % MD;
		qq[i] = (long long) qq[i - 1] * V2 % MD;
		rr[i] = (long long) rr[i - 1] * rr[i - 1] % MD;
		ss[i] = (long long) ss[i - 1] * ss[i - 1] % MD;
	}
}

long long count(int d) {
	return (1LL << (d + 1) / 2) + (1LL << (d + 2) / 2) - 2;
}

int p2count(int d) {
	return (long long) rr[(d + 1) / 2] * rr[(d + 2) / 2] % MD * qq[2] % MD;
}

int q2count(int d) {
	return (long long) ss[(d + 1) / 2] * ss[(d + 2) / 2] % MD * pp[2] % MD;
}

int main() {
	int q;

	init();
	scanf("%d", &q);
	while (q--) {
		long long n, m, wl, wm, wr;
		int l, y, y_, d, p, p_, ans;

		scanf("%lld", &n);
		l = 0;
		while ((1LL << l + 2) - 1 <= n)
			l++;
		m = n - ((1LL << l + 1) - 1);
		p = power(2, n);
		tt[0] = 1;
		for (y = 0; y <= l; y++)
			tt[y + 1] = (long long) tt[y] * ((m & 1LL << y) == 0 ? 1 : ss[y]) % MD;
		ans = 0;
		for (y = 0; y <= l + 1; y++)
			for (d = 0; d <= y + l; d++) {
				p_ = (long long) p * q2count(d + min(d, l - y)) % MD;
				if (d > y)
					p_ = (long long) p_ * ss[d - y + 1] % MD * 2 % MD * p2count((d - y) * 2) % MD;
				if (y + d > l) {
					y_ = (y + l - d + 2) / 2;
					wl = m >> l + 1 - y_ << y - y_;
					wm = (y <= l ? 1LL << y - y_ : m & (1LL << l + 1 - y_) - 1);
					wr = (y <= l ? 1LL << y : m) - wl - wm;
					ans = (ans + ((long long) p_ * ss[l + 1 - y_] % MD - 1) * (wl % MD)) % MD;
					ans = (ans + ((long long) p_ * tt[l + 1 - y_] % MD - 1) * (wm % MD)) % MD;
					ans = (ans + (p_ - 1) * (wr % MD)) % MD;
				} else
					ans = (ans + (p_ - 1) * ((1LL << y) % MD)) % MD;
			}
		ans = (long long) ans * inv(p - 1) % MD;
		if (ans < 0)
			ans += MD;
		printf("%d\n", ans);
	}
	return 0;
}

Compilation message

Main.c: In function 'main':
Main.c:64:20: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   64 |   while ((1LL << l + 2) - 1 <= n)
      |                  ~~^~~
Main.c:66:22: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   66 |   m = n - ((1LL << l + 1) - 1);
      |                    ~~^~~
Main.c:79:22: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
   79 |      wl = m >> l + 1 - y_ << y - y_;
      |                ~~~~~~^~~~
Main.c:79:32: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   79 |      wl = m >> l + 1 - y_ << y - y_;
      |                              ~~^~~~
Main.c:80:30: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   80 |      wm = (y <= l ? 1LL << y - y_ : m & (1LL << l + 1 - y_) - 1);
      |                            ~~^~~~
Main.c:80:55: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   80 |      wm = (y <= l ? 1LL << y - y_ : m & (1LL << l + 1 - y_) - 1);
      |                                                 ~~~~~~^~~~
Main.c:80:61: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   80 |      wm = (y <= l ? 1LL << y - y_ : m & (1LL << l + 1 - y_) - 1);
      |                                         ~~~~~~~~~~~~~~~~~~~~^~~
Main.c:57:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |  scanf("%d", &q);
      |  ^~~~~~~~~~~~~~~
Main.c:62:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |   scanf("%lld", &n);
      |   ^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 5 ms 292 KB Output is correct
12 Correct 5 ms 212 KB Output is correct
13 Correct 4 ms 212 KB Output is correct
14 Correct 5 ms 212 KB Output is correct
15 Correct 4 ms 212 KB Output is correct
16 Correct 5 ms 212 KB Output is correct
17 Correct 4 ms 288 KB Output is correct
18 Correct 4 ms 212 KB Output is correct
19 Correct 4 ms 212 KB Output is correct
20 Correct 4 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 284 KB Output is correct
2 Correct 11 ms 284 KB Output is correct
3 Correct 10 ms 284 KB Output is correct
4 Correct 9 ms 288 KB Output is correct
5 Correct 10 ms 280 KB Output is correct
6 Correct 10 ms 280 KB Output is correct
7 Correct 9 ms 212 KB Output is correct
8 Correct 10 ms 280 KB Output is correct
9 Correct 10 ms 212 KB Output is correct
10 Correct 10 ms 212 KB Output is correct
11 Correct 281 ms 280 KB Output is correct
12 Correct 280 ms 272 KB Output is correct
13 Correct 287 ms 268 KB Output is correct
14 Correct 282 ms 268 KB Output is correct
15 Correct 281 ms 280 KB Output is correct
16 Correct 280 ms 272 KB Output is correct
17 Correct 298 ms 276 KB Output is correct
18 Correct 286 ms 280 KB Output is correct
19 Correct 294 ms 280 KB Output is correct
20 Correct 303 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 284 KB Output is correct
2 Correct 10 ms 288 KB Output is correct
3 Correct 9 ms 284 KB Output is correct
4 Correct 9 ms 280 KB Output is correct
5 Correct 10 ms 212 KB Output is correct
6 Correct 12 ms 288 KB Output is correct
7 Correct 12 ms 284 KB Output is correct
8 Correct 13 ms 212 KB Output is correct
9 Correct 12 ms 280 KB Output is correct
10 Correct 10 ms 212 KB Output is correct
11 Correct 28 ms 212 KB Output is correct
12 Correct 28 ms 212 KB Output is correct
13 Correct 28 ms 264 KB Output is correct
14 Correct 28 ms 284 KB Output is correct
15 Correct 33 ms 288 KB Output is correct
16 Correct 28 ms 224 KB Output is correct
17 Correct 28 ms 212 KB Output is correct
18 Correct 27 ms 260 KB Output is correct
19 Correct 27 ms 212 KB Output is correct
20 Correct 27 ms 280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 5 ms 292 KB Output is correct
12 Correct 5 ms 212 KB Output is correct
13 Correct 4 ms 212 KB Output is correct
14 Correct 5 ms 212 KB Output is correct
15 Correct 4 ms 212 KB Output is correct
16 Correct 5 ms 212 KB Output is correct
17 Correct 4 ms 288 KB Output is correct
18 Correct 4 ms 212 KB Output is correct
19 Correct 4 ms 212 KB Output is correct
20 Correct 4 ms 212 KB Output is correct
21 Correct 10 ms 284 KB Output is correct
22 Correct 11 ms 284 KB Output is correct
23 Correct 10 ms 284 KB Output is correct
24 Correct 9 ms 288 KB Output is correct
25 Correct 10 ms 280 KB Output is correct
26 Correct 10 ms 280 KB Output is correct
27 Correct 9 ms 212 KB Output is correct
28 Correct 10 ms 280 KB Output is correct
29 Correct 10 ms 212 KB Output is correct
30 Correct 10 ms 212 KB Output is correct
31 Correct 281 ms 280 KB Output is correct
32 Correct 280 ms 272 KB Output is correct
33 Correct 287 ms 268 KB Output is correct
34 Correct 282 ms 268 KB Output is correct
35 Correct 281 ms 280 KB Output is correct
36 Correct 280 ms 272 KB Output is correct
37 Correct 298 ms 276 KB Output is correct
38 Correct 286 ms 280 KB Output is correct
39 Correct 294 ms 280 KB Output is correct
40 Correct 303 ms 212 KB Output is correct
41 Correct 9 ms 284 KB Output is correct
42 Correct 10 ms 288 KB Output is correct
43 Correct 9 ms 284 KB Output is correct
44 Correct 9 ms 280 KB Output is correct
45 Correct 10 ms 212 KB Output is correct
46 Correct 12 ms 288 KB Output is correct
47 Correct 12 ms 284 KB Output is correct
48 Correct 13 ms 212 KB Output is correct
49 Correct 12 ms 280 KB Output is correct
50 Correct 10 ms 212 KB Output is correct
51 Correct 28 ms 212 KB Output is correct
52 Correct 28 ms 212 KB Output is correct
53 Correct 28 ms 264 KB Output is correct
54 Correct 28 ms 284 KB Output is correct
55 Correct 33 ms 288 KB Output is correct
56 Correct 28 ms 224 KB Output is correct
57 Correct 28 ms 212 KB Output is correct
58 Correct 27 ms 260 KB Output is correct
59 Correct 27 ms 212 KB Output is correct
60 Correct 27 ms 280 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 92 ms 212 KB Output is correct
64 Correct 93 ms 276 KB Output is correct
65 Correct 98 ms 400 KB Output is correct
66 Correct 94 ms 280 KB Output is correct
67 Correct 106 ms 284 KB Output is correct
68 Correct 110 ms 284 KB Output is correct
69 Correct 109 ms 288 KB Output is correct
70 Correct 105 ms 276 KB Output is correct
71 Correct 105 ms 276 KB Output is correct
72 Correct 97 ms 280 KB Output is correct
73 Correct 292 ms 272 KB Output is correct
74 Correct 285 ms 276 KB Output is correct
75 Correct 285 ms 268 KB Output is correct
76 Correct 286 ms 256 KB Output is correct
77 Correct 278 ms 280 KB Output is correct
78 Correct 279 ms 272 KB Output is correct
79 Correct 277 ms 264 KB Output is correct
80 Correct 275 ms 276 KB Output is correct
81 Correct 281 ms 264 KB Output is correct
82 Correct 278 ms 260 KB Output is correct