Skip to content

Commit b6c1434

Browse files
Create Sum OR Explanation.txt
1 parent abc1f0e commit b6c1434

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)