means the degree of a problem.
Codeforces 1761D. Carry Bit
Comments:
Degree:
Tags: combinatorics
Very mind-testing problem and cost a lot of my time to deep thinking.
Statement:
Just want you to calculate the number of that the carries of equals to , and . The answer muodulo .
Solving-ideas:
First, turn the binary representation of into red blocks and blue blocks, which the red blocks means carries to the next position and the blue blocks means no carries. Combined with the statement, there are red blocks and blue blocks. Then try to calculate the number the red-blue block permutations.
We can turn the red blocks into parts, and . Then use the “Board insertion method” to solve this. Obviously, the blue blocks are the “boards”, so turn the red blocks into parts need “boards”——the blue blocks. So the number of this situation is .and for every parts of red blocks, except the last red block don’t have carry from the previous block and only have possibility,other red blocks have possibilitys. All in all, there is ways.
Then, lets talk about how to solve the blue blocks. the parts of blue blocks have possibilities—— . Let’s discuss these situations.
For , both of the front and the end are red blocks, and for every parts of the blue blocks, except the last blue block have a carry from the previous block and only have possibility,other blue blocks have possibilities. All in all, there is ways.
For , there are sub-cases:
If the front is blue and the end is red, for every parts of blue blocks, except the last blue block have carry from the previous block and only have possibility,other blue blocks have possibilities. All in all, there is ways.
If the front is red and the end is blue, for every parts of blue blocks, except the last blue block have carry from the previous block and only have possibility,other blue blocks have possibilities. But for the last blue block of the last part, it don’t have any carries so it have possibilities. All in all, there is ways.
For , both of the front and the end are blue blocks, and for every parts of the blue blocks, except the last blue block have a carry from the previous block and only have possibility,other blue blocks have possibilities. But for the last blue block of the last part, it don’t have any carries so it have possibilities. All in all, there is ways.
In the last, we have finally finished discussing all the situation, and and get the answer:
Code
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define lll __int128
#define db double
#define ldb long db
#define pii pair<int, int>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define endl '\n'
#define ft first
#define sd second
using namespace std;
const int N = 1000005;
const ll Mod = 1000000007;
ll n, k;
ll p3[N];
ll fac[N], inv[N];
ll qpw(ll x, ll y) {
ll res = 1;
while(y) {
if(y & 1ll) {
(res *= x) %= Mod;
}
(x *= x) %= Mod;
y /= 2;
}
return res;
}
void init() {
p3[0] = fac[0] = 1;
for(int i = 1; i <= N - 5; i++) {
p3[i] = p3[i - 1] * 3 % Mod;
fac[i] = fac[i - 1] * i % Mod;
}
inv[N - 5] = qpw(fac[N - 5], Mod - 2);
for(int i = N - 5; i >= 1; i--) {
inv[i - 1] = inv[i] * i % Mod;
}
}
ll C(ll x, ll y) {
if(x < y) {
return 0;
}
return fac[x] * inv[y] % Mod * inv[x - y] % Mod;
}
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
init();
cin >> n >> k;
if(k == 0) {
cout << p3[n] << endl;
return 0;
}
ll ans = 0;
for(int i = 1; i <= k; i++) {
ll res1 = C(k - 1, i - 1) * p3[k - i] % Mod;
ll res2 = C(n - k - 1, i) * p3[n - k - i] % Mod;
ll res3 = C(n - k - 1, i - 1) * p3[n - k - i] % Mod;
ll res4 = C(n - k - 1, i - 1) * p3[n - k - i + 1] % Mod;
ll res5 = C(n - k - 1, i - 2) * p3[n - k - i + 1] % Mod;
(ans += res1 * (res2 + res3 + res4 + res5) % Mod) %= Mod;
}
cout << ans << endl;
return 0;
}
/*
Progress is impossible without change, and those who cannot change their minds cannot change anything.
NEVER GIVE UP.
*/