题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers
Sunrise_up
·
·
题解
求点赞 qwq!
思路
$$
0,1,1,4,6,9,9,16,22,29,33,42,48,55,55,70,84,99,111,128,142,157,165,186,\dots
$$
代码部分提供了打表代码。
好像没啥规律。
由于是统计偶数的个数,正难则反,那就考虑求奇数的个数。
由于前 $n$ 层共有 $\dfrac{n(n+3)}{2}$ 项,那么前 $n$ 层偶数的个数为 $\dfrac{n(n+3)}{2}$ 减去前 $n$ 层奇数的个数。
然后继续打表前 $n$ 层奇数的个数:
$$
2,4,8,10,14,18,26,28,32,36,44,48,56,64,80,82,86,90,98,102,110,118,134,\dots
$$
这很有规律。
考虑对相邻两项做差(第 $i$ 项为原第 $i$ 项与原第 $i-1$ 项的差,第 $1$ 项不变),求出第 $x$ 层的奇数的个数:
$$
2,2,4,2,4,4,8,2,4,4,8,4,8,8,16,2,4,4,8,4,8,8,16,\dots
$$
容易发现这些数都是 $2^k$。
考虑把 $k$ 列出来:
$$
1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,\dots
$$
发现了第 $n$ 项为二进制下 $n$ 中 $1$ 的个数,即 $\operatorname{popcount}(n)$。
:::::info[正确性]
有 Lucas 定理推论:二项式系数 $\displaystyle\binom{n}{k}$ 为奇数,当且仅当在二进制下 $k$ 是 $n$ 的子集(即 $k \ \mathrm{and}\ n=k$,或者说 $k$ 的每一位 $1$ 都对应 $n$ 的某一位 $1$)。
::::info[Lucas 定理]
当 $p$ 是质数时,有:
$$
\binom{n}{m}=\binom{\frac{n}{p}}{\frac{m}{p}}\binom{n\bmod p}{m\bmod p}\pmod p
$$
[模板 P3807](https://www.luogu.com.cn/problem/P3807)。
::::
因此,第 $i$ 层(即帕斯卡三角形第 $i$ 行,数为 $\displaystyle\binom{i}{0},\binom{i}{1},\dots,\binom{i}{i}$)中奇数的个数,就等于满足 $k\subseteq i$(二进制子集关系)的 $k$ 的个数,所以第 $i$ 层奇数个数就是 $2^{\operatorname{popcount}(i)}$。
:::::
如果你真不会找规律的话,推荐 [OEIS](oeis.org)。
所以前 $n$ 层奇数的个数为 $\displaystyle\sum_{i=1}^n 2^{\operatorname{popcount}(i)}$。
所以前 $n$ 层偶数的个数为 $\displaystyle\frac{n(n+3)}{2}-\sum_{i=1}^n 2^{\operatorname{popcount}(i)}$。
---
对于求 $\displaystyle\sum_{i=1}^n 2^{\operatorname{popcount}(i)}$,$n$ 太大,考虑数位 dp。
考虑定义 $dp_{pos,cnt,f}$ 表示从高位往低位填,填了 $pos$ 位,该数二进制下有 $cnt$ 个 $1$,要不要管上限是 $n$ 的限制(若 $f=1$ 则有)。
那么就可以求出所有 $[0,n]$ 范围内的整数 $x$ 的 $2^{\operatorname{popcount}(x)}$ 之和。
因为多统计了 $0$,所以还要减回去。
---
换一个做法,其实就是非递归版本的数位 dp。
考虑将 $[1,n]$ 拆成若干连续的段。
有一个拆法,就是从低位到高位枚举 $n$ 的每一个为 $1$ 的二进制位(下标从 $0$ 开始),假如处理到第 $k$ 位,则:
- 第 $k$ 位强制为 $0$。
- 高于 $k$ 位的部分与 $n$ 完全相同。其中 $1$ 的个数记为 $c$。
- 低于 $k$ 位的共 $k$ 位,可以任意取 $0$ 或 $1$。
显然所有段正好覆盖 $[0,n-1]$,要额外记录 $n$ 的答案,再减去 $0$ 的贡献。
对于低于 $k$ 位中任意一个数,它有 $t$ 个 $1$(均有 $t\in[0,k]$),则整个数 $1$ 的个数为 $c+t$,贡献 $2^{c+t}$。
所以这个段的贡献为:
$$
\sum_{t=0}^k\binom{k}{t}2^{c+t}=2^c\sum_{t=0}^k\binom{k}{t}2^{t}
$$
根据二项式定理:
$$
2^c\sum_{t=0}^k\binom{k}{t}2^{t}=2^c3^k
$$
设 $n$ 二进制下为 $b_l\dots b_1b_0$,所以:
$$
\sum_{i=1}^n 2^{\operatorname{popcount}(i)}={\Large(}\sum_{i=0}^n 2^{\operatorname{popcount}(i)}{\Large)}-1={\Large(}\sum_{k=0}^l [b_k=1]\cdot 2^{\operatorname{popcount}(b_l\dots b_{k+1})}\cdot 3^k{\Large)}+2^{\operatorname{popcount}(n)}-1
$$
倒序处理即可。
---
要用高精度。
## 代码
:::info[打表代码]
这份代码依次输出了前 $1\sim100$ 层偶数的个数。
```cpp
#include<bits/stdc++.h>
using namespace std;
int a[101][101],ans;
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
a[1][1]=a[1][2]=1,cout<<"0,";
for(int i=2;i<=100;i++){
a[i][1]=a[i][i+1]=1;
for(int j=2;j<=i;j++){
a[i][j]=a[i-1][j-1]+a[i-1][j];
if(!(a[i][j]&1))ans++;
}
cout<<ans<<',';
}
}
```
:::
AC 代码太长了放一个折叠框。
:::success[递归版本]
时间复杂度 $O(\log^2 n)$。
```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
using ull=uint64_t;
const int B=16;//16*64=1024bit
struct i1024{
ull d[B]{};
i1024(){}
i1024(ull x){d[0]=x;}
i1024 operator+(const i1024 &b)const{
i1024 res;
ull c=0;
for(int i=0;i<B;i++){
__uint128_t s=(__uint128_t)d[i]+b.d[i]+c;
res.d[i]=(ull)s,c=(ull)(s>>64);
}
return res;
}
i1024 &operator+=(const i1024 &b){
*this=*this+b;
return *this;
}
i1024 operator-(const i1024 &b)const{
i1024 res;
ull bw=0;
for(int i=0;i<B;i++){
__uint128_t ta=d[i],tb=(__uint128_t)b.d[i]+bw;
bw=0;
if(ta<tb)ta+=(__uint128_t)1<<64,bw=1;
res.d[i]=(ull)(ta-tb);
}
return res;
}
i1024 &operator-=(const i1024 &b){
*this=*this-b;
return *this;
}
i1024 muls(ull x)const{
i1024 res;
ull c=0;
for(int i=0;i<B;i++){
__uint128_t s=(__uint128_t)d[i]*x+c;
res.d[i]=(ull)s;
c=(ull)(s>>64);
}
return res;
}
i1024 operator*(const i1024 &b)const{
i1024 res;
for(int i=0;i<B;i++){
if(b.d[i]==0)continue;
i1024 tmp=this->muls(b.d[i]);
for(int j=B-1;j>=i;j--)tmp.d[j]=tmp.d[j-i];
for(int j=0;j<i;j++)tmp.d[j]=0;
res+=tmp;
}
return res;
}
i1024 div2()const{
i1024 res;
ull rem=0;
for(int i=B-1;i>=0;i--){
__uint128_t cur=((__uint128_t)rem<<64)|d[i];
res.d[i]=(ull)(cur/2),
rem=(ull)(cur%2);
}
return res;
}
i1024 shl(int k)const{
i1024 res=*this;
while(k--){
ull c=0;
for(int i=0;i<B;i++){
ull nc=res.d[i]>>63;
res.d[i]=(res.d[i]<<1)|c,c=nc;
}
}
return res;
}
bool operator==(const i1024 &b)const{
for(int i=0;i<B;i++)if(d[i]!=b.d[i])return 0;
return 1;
}
ull mod2()const{return d[0]&1;}
};
i1024 pw2(int k){
i1024 t(1);
return t.shl(k);
}
string tostr(i1024 x){
vector<int> d;
const ull bs=10;
while(!(x==i1024(0))){
ull r=0;
i1024 nx;
for(int i=B-1;i>=0;i--){
__uint128_t c=(__uint128_t)r<<64|x.d[i];
nx.d[i]=(ull)(c/bs),
r=(ull)(c%bs);
}
d.push_back(r),x=nx;
}
if(d.empty())return "0";
reverse(d.begin(),d.end());
string s;
for(int num:d)s+=(char)(num+48);
return s;
}
int b[200],len;
i1024 n,ml;
bool vis[200][200][2];
i1024 dp[200][200][2];
void fj(i1024 x){
len=0;
if(x==i1024(0)){b[++len]=0;return;}
while(!(x==i1024(0)))b[++len]=x.mod2(),x=x.div2();
for(int i=1;i<=len/2;i++)swap(b[i],b[len-i+1]);
}
i1024 dfs(int pos,int cnt,bool f){
if(pos==len+1)return pw2(cnt);
if(vis[pos][cnt][f])return dp[pos][cnt][f];
int lim=f?b[pos]:1;
i1024 res=i1024(0);
for(int d=0;d<=lim;d++){
bool nf=f&&(d==lim);
res+=dfs(pos+1,cnt+d,nf);
}
dp[pos][cnt][f]=res,vis[pos][cnt][f]=1;
return res;
}
string s;
signed main(){
while(cin>>s){
n=i1024(0);
for(int i=0;i<s.size();i++)n=(n*i1024(10))+(i1024)(s[i]^48);
memset(vis,0,sizeof(vis)),fj(n),
ml=n*(n+i1024(3));
i1024 ans=ml.div2()-dfs(1,0,1)+1;
cout<<tostr(ans)<<'\n';
}
}
```
:::
:::success[非递归版本]
时间复杂度 $O(\log n)$。
```cpp
#include<bits/stdc++.h>
#define int long long
using namespace std;
using ull=uint64_t;
const int B=16;//16*64=1024bit
struct i1024{
ull d[B]{};
i1024(){}
i1024(ull x){d[0]=x;}
i1024 operator+(const i1024 &b)const{
i1024 res;
ull c=0;
for(int i=0;i<B;i++){
__uint128_t s=(__uint128_t)d[i]+b.d[i]+c;
res.d[i]=(ull)s,c=(ull)(s>>64);
}
return res;
}
i1024 &operator+=(const i1024 &b){
*this=*this+b;
return *this;
}
i1024 operator-(const i1024 &b)const{
i1024 res;
ull bw=0;
for(int i=0;i<B;i++){
__uint128_t ta=d[i],tb=(__uint128_t)b.d[i]+bw;
bw=0;
if(ta<tb)ta+=(__uint128_t)1<<64,bw=1;
res.d[i]=(ull)(ta-tb);
}
return res;
}
i1024 &operator-=(const i1024 &b){
*this=*this-b;
return *this;
}
i1024 muls(ull x)const{
i1024 res;
ull c=0;
for(int i=0;i<B;i++){
__uint128_t s=(__uint128_t)d[i]*x+c;
res.d[i]=(ull)s;
c=(ull)(s>>64);
}
return res;
}
i1024 operator*(const i1024 &b)const{
i1024 res;
for(int i=0;i<B;i++){
if(b.d[i]==0)continue;
i1024 tmp=this->muls(b.d[i]);
for(int j=B-1;j>=i;j--)tmp.d[j]=tmp.d[j-i];
for(int j=0;j<i;j++)tmp.d[j]=0;
res+=tmp;
}
return res;
}
i1024 div2()const{
i1024 res;
ull rem=0;
for(int i=B-1;i>=0;i--){
__uint128_t cur=((__uint128_t)rem<<64)|d[i];
res.d[i]=(ull)(cur/2),
rem=(ull)(cur%2);
}
return res;
}
i1024 shl(int k)const{
i1024 res=*this;
while(k--){
ull c=0;
for(int i=0;i<B;i++){
ull nc=res.d[i]>>63;
res.d[i]=(res.d[i]<<1)|c,c=nc;
}
}
return res;
}
bool operator==(const i1024 &b)const{
for(int i=0;i<B;i++)if(d[i]!=b.d[i])return 0;
return 1;
}
i1024 &operator*=(const i1024 &b){
*this=*this*b;
return *this;
}
ull mod2()const{return d[0]&1;}
};
i1024 pw2(int k){
i1024 t(1);
return t.shl(k);
}
string tostr(i1024 x){
vector<int> d;
const ull bs=10;
while(!(x==i1024(0))){
ull r=0;
i1024 nx;
for(int i=B-1;i>=0;i--){
__uint128_t c=(__uint128_t)r<<64|x.d[i];
nx.d[i]=(ull)(c/bs),
r=(ull)(c%bs);
}
d.push_back(r),x=nx;
}
if(d.empty())return "0";
reverse(d.begin(),d.end());
string s;
for(int num:d)s+=(char)(num+48);
return s;
}
int b[200],len,cnt;
i1024 n,ml,ans;
void fj(i1024 x){
len=0;
if(x==i1024(0)){b[++len]=0;return;}
while(!(x==i1024(0)))b[++len]=x.mod2(),x=x.div2();
}
i1024 qpow(i1024 x,int y){
i1024 res=1;
while(y){
if(y&1)res*=i1024(x);
x*=x,y>>=1;
}
return res;
}
string s;
signed main(){
while(cin>>s){
n=i1024(0);
for(int i=0;i<s.size();i++)n=(n*i1024(10))+(i1024)(s[i]^48);
fj(n),ans=i1024(0),cnt=0;
ml=n*(n+i1024(3)),ml=ml.div2();
for(int k=len;k>=1;k--){
if(b[k])ans+=pw2(cnt)*qpow(3,k-1),cnt++;
}
ans=ml-(ans-i1024(1)+pw2(cnt));
cout<<tostr(ans)<<'\n';
}
}
```
:::