题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers
[ICPC 2017 Hong Kong R] Count the Even Integers 题解
题目大意
给定
思路分析
1. 转化为标准杨辉三角
本题的杨辉三角第
验证:
- 第
1 层:C(1,0)=1, C(1,1)=1 ,共2 个 ✅ - 第
2 层:C(2,0)=1, C(2,1)=2, C(2,2)=1 ,共3 个 ✅
2. 奇偶性与二进制
根据 Lucas 定理,
因此,第
验证:
3. 总数与奇数个数
前
前
则偶数个数为:
4. 高效计算 S_{\text{odd}}
最后加上
具体公式:
其中
5. 算法步骤
- 读入
N (使用高精度) - 计算
S_{\text{total}} = N(N+3)/2 - 计算
S_{\text{odd}} = F(N) - 1 - 输出
S_{\text{even}} = S_{\text{total}} - S_{\text{odd}}
代码(C++)
由于 unsigned long long 元素存
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull BASE = 1000000000000000000ULL;
struct Big {
vector<ull> d;
Big() { d.push_back(0); }
Big(ull x) {
while (x) { d.push_back(x % BASE); x /= BASE; }
if (d.empty()) d.push_back(0);
}
Big(string s) {
for (int i = s.size() - 1; i >= 0; i -= 18) {
int l = max(0, i - 17);
d.push_back(stoull(s.substr(l, i - l + 1)));
}
}
Big add(const Big &b) const {
Big r;
r.d.resize(max(d.size(), b.d.size()));
ull carry = 0;
for (size_t i = 0; i < r.d.size(); i++) {
if (i < d.size()) carry += d[i];
if (i < b.d.size()) carry += b.d[i];
r.d[i] = carry % BASE;
carry /= BASE;
}
if (carry) r.d.push_back(carry);
return r;
}
Big sub(const Big &b) const {
Big r;
r.d.resize(d.size());
ull borrow = 0;
for (size_t i = 0; i < d.size(); i++) {
ull cur = d[i] - borrow;
if (i < b.d.size()) cur -= b.d[i];
if (cur > d[i]) { cur += BASE; borrow = 1; }
else borrow = 0;
r.d[i] = cur;
}
while (r.d.size() > 1 && r.d.back() == 0) r.d.pop_back();
return r;
}
Big mul(ull x) const {
Big r;
r.d.resize(d.size());
__int128 carry = 0;
for (size_t i = 0; i < d.size(); i++) {
carry += (__int128)d[i] * x;
r.d[i] = carry % BASE;
carry /= BASE;
}
while (carry) { r.d.push_back(carry % BASE); carry /= BASE; }
return r;
}
Big mul(const Big &b) const {
Big r;
r.d.resize(d.size() + b.d.size());
for (size_t i = 0; i < d.size(); i++) {
__int128 carry = 0;
for (size_t j = 0; j < b.d.size(); j++) {
carry += (__int128)d[i] * b.d[j] + r.d[i + j];
r.d[i + j] = carry % BASE;
carry /= BASE;
}
if (carry) r.d[i + b.d.size()] += carry;
}
while (r.d.size() > 1 && r.d.back() == 0) r.d.pop_back();
return r;
}
Big div2() const {
Big r;
r.d.resize(d.size());
ull rem = 0;
for (int i = d.size() - 1; i >= 0; i--) {
ull cur = rem * BASE + d[i];
r.d[i] = cur / 2;
rem = cur % 2;
}
while (r.d.size() > 1 && r.d.back() == 0) r.d.pop_back();
return r;
}
void print() const {
printf("%llu", d.back());
for (int i = d.size() - 2; i >= 0; i--) printf("%018llu", d[i]);
}
};
Big pow_int(int a, int b) {
Big r(1), base(a);
while (b) {
if (b & 1) r = r.mul(base);
base = base.mul(base);
b >>= 1;
}
return r;
}
Big F(string bin) {
Big ans(0);
int cnt = 0, n = bin.size();
for (int i = 0; i < n; i++) {
if (bin[i] == '1') {
ans = ans.add(pow_int(2, cnt).mul(pow_int(3, n - i - 1)));
cnt++;
}
}
ans = ans.add(pow_int(2, cnt));
return ans;
}
string to_bin(string dec) {
string bin;
while (dec != "0") {
int rem = 0;
string nxt;
for (char c : dec) {
int cur = rem * 10 + (c - '0');
if (!nxt.empty() || cur / 2 > 0) nxt += (cur / 2 + '0');
rem = cur % 2;
}
bin += (rem + '0');
dec = nxt.empty() ? "0" : nxt;
}
reverse(bin.begin(), bin.end());
return bin.empty() ? "0" : bin;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
string s;
while (cin >> s) {
Big N(s);
Big total = N.add(Big(3)).mul(N).div2();
string bin = to_bin(s);
Big odd = F(bin).sub(Big(1));
Big even = total.sub(odd);
even.print();
printf("\n");
}
return 0;
}
本文经 deepseek 润色。