跳到正文

题解

题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers

Sunrise_up约 19 分钟
原文
文章信息与版本

文章信息

发布
2026年8月3日
收录
2026年8月4日
更新
2026年11月13日
篇幅
约 19 分钟
获赞
9
题目
P17189

版本

  1. v3当前
  2. v2
  3. v1

求点赞 qwq!

思路

nn 这么大,考虑打表找规律:

0,1,1,4,6,9,9,16,22,29,33,42,48,55,55,70,84,99,111,128,142,157,165,186,…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

代码部分提供了打表代码。

好像没啥规律。

由于是统计偶数的个数,正难则反,那就考虑求奇数的个数。

由于前 nn 层共有 n(n+3)2\dfrac{n(n+3)}{2} 项,那么前 nn 层偶数的个数为 n(n+3)2\dfrac{n(n+3)}{2} 减去前 nn 层奇数的个数。

然后继续打表前 nn 层奇数的个数:

2,4,8,10,14,18,26,28,32,36,44,48,56,64,80,82,86,90,98,102,110,118,134,…2,4,8,10,14,18,26,28,32,36,44,48,56,64,80,82,86,90,98,102,110,118,134,\dots

这很有规律。

考虑对相邻两项做差(第 ii 项为原第 ii 项与原第 i−1i-1 项的差,第 11 项不变),求出第 xx 层的奇数的个数:

2,2,4,2,4,4,8,2,4,4,8,4,8,8,16,2,4,4,8,4,8,8,16,…2,2,4,2,4,4,8,2,4,4,8,4,8,8,16,2,4,4,8,4,8,8,16,\dots

容易发现这些数都是 2k2^k。

考虑把 kk 列出来:

1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,…1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,\dots

发现了第 nn 项为二进制下 nn 中 11 的个数,即 popcount⁡(n)\operatorname{popcount}(n)。

正确性

有 Lucas 定理推论:二项式系数 (nk)\displaystyle\binom{n}{k} 为奇数,当且仅当在二进制下 kk 是 nn 的子集(即 k and n=kk \ \mathrm{and}\ n=k,或者说 kk 的每一位 11 都对应 nn 的某一位 11)。

Lucas 定理

当 pp 是质数时,有:

(nm)=(npmp)(n mod pm mod p)(modp)\binom{n}{m}=\binom{\frac{n}{p}}{\frac{m}{p}}\binom{n\bmod p}{m\bmod p}\pmod p

模板 P3807。

因此,第 ii 层(即帕斯卡三角形第 ii 行,数为 (i0),(i1),…,(ii)\displaystyle\binom{i}{0},\binom{i}{1},\dots,\binom{i}{i})中奇数的个数,就等于满足 k⊆ik\subseteq i(二进制子集关系)的 kk 的个数,所以第 ii 层奇数个数就是 2popcount⁡(i)2^{\operatorname{popcount}(i)}。

如果你真不会找规律的话,推荐 OEIS。

所以前 nn 层奇数的个数为 ∑i=1n2popcount⁡(i)\displaystyle\sum_{i=1}^n 2^{\operatorname{popcount}(i)}。

所以前 nn 层偶数的个数为 n(n+3)2−∑i=1n2popcount⁡(i)\displaystyle\frac{n(n+3)}{2}-\sum_{i=1}^n 2^{\operatorname{popcount}(i)}。


对于求 ∑i=1n2popcount⁡(i)\displaystyle\sum_{i=1}^n 2^{\operatorname{popcount}(i)},nn 太大,考虑数位 dp。

考虑定义 dppos,cnt,fdp_{pos,cnt,f} 表示从高位往低位填,填了 pospos 位,该数二进制下有 cntcnt 个 11,要不要管上限是 nn 的限制(若 f=1f=1 则有)。

那么就可以求出所有 [0,n][0,n] 范围内的整数 xx 的 2popcount⁡(x)2^{\operatorname{popcount}(x)} 之和。

因为多统计了 00,所以还要减回去。


换一个做法,其实就是非递归版本的数位 dp。

考虑将 [1,n][1,n] 拆成若干连续的段。

有一个拆法,就是从低位到高位枚举 nn 的每一个为 11 的二进制位(下标从 00 开始),假如处理到第 kk 位,则:

  • 第 kk 位强制为 00。
  • 高于 kk 位的部分与 nn 完全相同。其中 11 的个数记为 cc。
  • 低于 kk 位的共 kk 位,可以任意取 00 或 11。

显然所有段正好覆盖 [0,n−1][0,n-1],要额外记录 nn 的答案,再减去 00 的贡献。

对于低于 kk 位中任意一个数,它有 tt 个 11(均有 t∈[0,k]t\in[0,k]),则整个数 11 的个数为 c+tc+t,贡献 2c+t2^{c+t}。

所以这个段的贡献为:

∑t=0k(kt)2c+t=2c∑t=0k(kt)2t\sum_{t=0}^k\binom{k}{t}2^{c+t}=2^c\sum_{t=0}^k\binom{k}{t}2^{t}

根据二项式定理:

2c∑t=0k(kt)2t=2c3k2^c\sum_{t=0}^k\binom{k}{t}2^{t}=2^c3^k

设 nn 二进制下为 bl…b1b0b_l\dots b_1b_0,所以:

∑i=1n2popcount⁡(i)=(∑i=0n2popcount⁡(i))−1=(∑k=0l[bk=1]⋅2popcount⁡(bl…bk+1)⋅3k)+2popcount⁡(n)−1\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

倒序处理即可。


要用高精度。

代码

打表代码

这份代码依次输出了前 1∼1001\sim100 层偶数的个数。

C++
#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 代码太长了放一个折叠框。

递归版本

时间复杂度 O(log⁡2n)O(\log^2 n)。

C++
#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';
	}
}
非递归版本

时间复杂度 O(log⁡n)O(\log n)。

C++
#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';
	}
}
原文
存档评论2 条抓取于