File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Contests/Starters/84/Explanations Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ Each bit is set in at most 1 integer.
2
+
3
+ Let us use simple Mathematics to count the number of ways to distribute b bits into 3 sets
4
+
5
+ 1. 3^b
6
+ 2. There are 3 x 2^b ways in which one of the two integers are empty
7
+ 3. There are 3 ways to distribute in which two of the three integers are empty
8
+
9
+ Answer = 3^b - 3 x 2^b + 3
10
+
11
+ -----
12
+
13
+ void solve()
14
+ {
15
+ long long n;
16
+ cin >> n;
17
+
18
+ int bit_count = no_of_ones(n);
19
+
20
+ const int MOD = 1e9 + 7;
21
+ long long all_three = power(3, bit_count, MOD);
22
+ long long only_two = (3*power(2, bit_count, MOD))%MOD;
23
+ long long only_one = 3;
24
+
25
+ long long answer = all_three - only_two + only_one + MOD;
26
+ answer %= MOD;
27
+
28
+ cout << answer << "\n";
29
+ }
You can’t perform that action at this time.
0 commit comments