跳到正文

个人记录

Record of good problems

#SUSPENDED#约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年4月6日
更新
2026年4月6日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

★\red\bigstar means the degree of a problem.

Codeforces 1761D. Carry Bit

Comments:

Degree: ★\red\bigstar ★\red\bigstar ★\red\bigstar ★\red\bigstar ★\red\bigstar

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 (a,b)(a, b) that the carries of a+ba + b equals to kk, and 0≤a,b≤2n0 \le a, b \le 2 ^ n. The answer muodulo 109+710^9 + 7.

Solving-ideas:

First, turn the binary representation of a+ba + b 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 kk red blocks and n−kn - k blue blocks. Then try to calculate the number the red-blue block permutations.

We can turn the red blocks into ii parts, and 1≤i≤k1 \le i \le k. Then use the “Board insertion method” to solve this. Obviously, the blue blocks are the “boards”, so turn the red blocks into ii parts need i−1i - 1 “boards”——the blue blocks. So the number of this situation is (k−1i−1)\begin{pmatrix} k-1 \\ i-1 \end{pmatrix}.and for every parts of red blocks, except the last red block don’t have carry from the previous block and only have 11 possibility,other red blocks have 33 possibilitys. All in all, there is (k−1i−1)×3∑j=1ilenj−1=(k−1i−1)×3k−i\begin{pmatrix} k-1 \\ i-1 \end{pmatrix} \times 3^{\sum \limits _{j=1} ^{i} len_j -1} = \begin{pmatrix} k-1 \\ i-1 \end{pmatrix} \times 3^{k - i} ways.

Then, lets talk about how to solve the blue blocks. the parts of blue blocks have 33 possibilities—— i−1,i,i+1i - 1, i, i+1. Let’s discuss these situations.

For i−1i - 1, 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 11 possibility,other blue blocks have 33 possibilities. All in all, there is (n−k−1i−2)×3∑j=1i−1lenj−1=(n−k−1i−2)×3n−k−i+1\begin{pmatrix} n-k-1 \\ i-2 \end{pmatrix} \times 3^{\sum \limits _{j=1} ^{i-1} len_j-1} = \begin{pmatrix} n-k-1 \\ i-2 \end{pmatrix} \times 3^{n-k-i+1} ways.

For ii, there are 22 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 11 possibility,other blue blocks have 33 possibilities. All in all, there is (n−k−1i−1)×3∑j=1ilenj−1=(n−k−1i−1)×3n−k−i\begin{pmatrix} n-k-1 \\ i-1 \end{pmatrix} \times 3^{\sum \limits _{j=1} ^{i} len_j-1} = \begin{pmatrix} n-k-1 \\ i-1 \end{pmatrix} \times 3^{n-k-i} 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 11 possibility,other blue blocks have 33 possibilities. But for the last blue block of the last part, it don’t have any carries so it have 33 possibilities. All in all, there is (n−k−1i−1)×3∑j=1i−1lenj−1×3leni=(n−k−1i−1)×3n−k−i+1\begin{pmatrix} n-k-1 \\ i-1 \end{pmatrix} \times 3^{\sum \limits _{j=1} ^{i-1} len_{j -1}} \times 3^{len_i} = \begin{pmatrix} n-k-1 \\ i-1 \end{pmatrix} \times 3^{n-k-i+1} ways.

For i+1i + 1, 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 11 possibility,other blue blocks have 33 possibilities. But for the last blue block of the last part, it don’t have any carries so it have 33 possibilities. All in all, there is (n−k−1i)×(3∑j=1ileni−1×3leni+1)=(n−k−1i)×3n−k−i\begin{pmatrix} n-k-1 \\ i \end{pmatrix} \times (3^{\sum \limits _{j=1} ^i len_{i}-1} \times 3^{len_{i+1}}) = \begin{pmatrix} n-k-1 \\ i \end{pmatrix} \times 3^{n-k-i} ways.

In the last, we have finally finished discussing all the situation, and and get the answer:

∑i=1k(k−1i−1)×3k−i×((n−k−1i)×3n−k−i+(n−k−1i−1)×3n−k−i+(n−k−1i−1)×3n−k−i+1+(n−k−1i−2)×3n−k−i+1)\sum _{i=1} ^{k} \begin{pmatrix} k-1 \\ i-1 \end{pmatrix} \times 3^{k - i} \times \left ( \begin{pmatrix} n-k-1 \\ i \end{pmatrix} \times 3^{n-k-i}+\begin{pmatrix} n-k-1 \\ i-1 \end{pmatrix} \times 3^{n-k-i}+\begin{pmatrix} n-k-1 \\ i-1 \end{pmatrix} \times 3^{n-k-i+1}+\begin{pmatrix} n-k-1 \\ i-2 \end{pmatrix} \times 3^{n-k-i+1} \right )
Code
C++
#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.
*/
原文