跳到正文

题解

题解:P17806 [ICPC 2019 Nanchang R] And and Pair

lw393约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月30日
收录
2026年10月30日
更新
2026年11月9日
篇幅
约 3 分钟
题目
P17806

版本

  1. v1当前

差点被这题击杀。

枚举一下 ii 的最高位,他得满足这一位上 SS 是 11,假设后面的 00 的个数是 xx,后面 11 的数量是 yy。

容易知道固定了最高位以后答案就是 2x3y2^x3^y。

直接求和即可,注意还有 i=j=0i=j=0 这个情况。至于为什么差点寄了,是因为我一开始看错题了。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long

const int N = 1e6 + 5, mod = 1e9 + 7;

int fact[N], inv[N];
int fast_power(int a, int b){
	int ans = 1;
	while(b){
		if(b & 1) ans = ans * a % mod;
		a = a * a % mod; b >>= 1;
	}
	return ans;
}
void init() {
	fact[0] = 1; for(int i = 1; i < N; i++) fact[i] = fact[i - 1] * i % mod;
	inv[N - 1] = fast_power(fact[N - 1], mod - 2);
	for(int i = N - 2; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod;
}
int c(int a, int b) {
	if(a < b || b < 0) return 0;
	return fact[a] * inv[b] % mod * inv[a - b] % mod;
}

void solve() {
	string s; cin >> s;
	int n = s.length(); s = ' ' + s;
	vector<int> pre(n + 1);
	for(int i = 1; i <= n; i++)
		if(s[i] == '1') pre[i] = pre[i - 1] + 1;
		else pre[i] = pre[i - 1];
	int ans = 0;
	for(int i = 1; i <= n; i++)
		if(s[i] == '1') {
			int x = pre[n] - pre[i];
			int y = n - i - x;
			(ans += fast_power(2, y) * fast_power(3, x) % mod) %= mod;
		}
	cout << ans + 1 << '\n';
}

signed main() {
	init();
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	// For Problem with file IO
	// #ifndef CPH
	//   freopen(".in", "r", stdin);
	//   freopen(".out", "w", stdout);
	// #endif
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}
原文