zoukankan      html  css  js  c++  java
  • Good Bye 2021: 2022 is NEAR

    H. Keep XOR Low

    题目描述

    点此看题

    \(n\) 个数 \(a_1,a_2...a_n\),问有多少个子集 \(1\leq b_1<b_2...<b_k\leq n\) 满足下列条件:

    \[a_{b_i}\oplus a_{b_j}\leq x \]

    \(1\leq n\leq 150000,0\leq x<2^{30}\)

    解法

    \(\tt 300iq\) 还出过一道 \(a_{b_i}\oplus a_{b_j}\geq x\) 的题目,但是做法完全不一样,因为这题没有 \(x\leq y\leq z\rightarrow \min(x\oplus y,y\oplus z)\leq x\oplus z\) 类似的条件,我不得不佩服 \(\tt 300iq\) 的出题能力啊。

    异或问题可以从 \(\tt trie\) 树上考虑,结合我们最熟知的 \(\tt trie\) 树计数问题,用它优化利用了紧贴的思想,也就是我们总是往限制最强烈的地方递归。那么本题我们考虑沿用这种思想,设计一个递归函数来完成计算。

    这道题的 \(a_{b_i}\oplus a_{b_j}\leq x\) 显然是一个二元关系,那么我们也尝试"二元地"描述它,设 \(dfs(x,y,d)\) 表示从子树 \(x\) 和子树 \(y\) 中选出子集,至今仍然紧贴限制,并且我们只需要考虑 \(x,y\) 之间产生的限制,所得到的方案数,讨论:

    • 如果 \(x=y\),若 \(x\) 在数位 \(d\) 上有值,那么只需要递归到 \(dfs(ls,rs,d-1)\);否则我们只能递归到 \(dfs(ls,ls,d-1)\)\(dfs(rs,rs,d-1)\),根据递归函数的定义这一部分不需要额为计算贡献。
    • 如果 \(x\not=y\),若 \(x\) 在数位 \(d\) 上有值,那么递归到 \(dfs(ls(x),rs(y),d-1)\)\(dfs(rs(x),ls(y),d-1)\),并且这两部分是独立的需要使用乘法原理;否则递归到 \(dfs(ls(x),ls(y),d-1)\)\(dfs(rs(x),rs(y),d-1)\),并且使用加法原理,根据定义我们是没有计算在 \(ls(x),rs(x)/ls(y),rs(y)\) 都选择了的子序列,所以这部分需要加上去。

    不难发现每一层遍历到的节点数是 \(O(n)\) 的,那么总时间复杂度 \(O(n\log a)\)

    #include <cstdio>
    #include <iostream>
    using namespace std;
    const int M = 5000005;
    const int MOD = 998244353;
    #define int long long
    int read()
    {
    	int x=0,f=1;char c;
    	while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
    	while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
    	return x*f;
    }
    int n,m,cnt=1,ch[M][2],siz[M],pw[M];
    void ins(int x)
    {
    	for(int i=30,p=1;i>=0;i--)
    	{
    		int c=(x>>i)&1;
    		if(!ch[p][c]) ch[p][c]=++cnt;
    		p=ch[p][c];
    		siz[p]++;
    	}
    }
    int dfs(int x,int y,int d)
    {
    	if(!x) return pw[siz[y]]-1;
    	if(!y) return pw[siz[x]]-1;
    	if(x==y)
    	{
    		if(d<0) return pw[siz[x]]-1;
    		if(m>>d&1) return dfs(ch[x][0],ch[x][1],d-1);
    		return (dfs(ch[x][0],ch[x][0],d-1)
    		+dfs(ch[x][1],ch[x][1],d-1))%MOD;
    	}
    	if(d<0) return (pw[siz[x]]*pw[siz[y]]-1)%MOD;
    	if(m>>d&1) return ((dfs(ch[x][0],ch[y][1],d-1)+1)
    	*(dfs(ch[x][1],ch[y][0],d-1)+1)-1)%MOD;
    	int r=(dfs(ch[x][0],ch[y][0],d-1)
    	+dfs(ch[x][1],ch[y][1],d-1))%MOD;
    	r=(r+(pw[siz[ch[x][0]]]-1)*(pw[siz[ch[x][1]]]-1))%MOD;
    	r=(r+(pw[siz[ch[y][0]]]-1)*(pw[siz[ch[y][1]]]-1))%MOD;
    	return r;
    }
    signed main()
    {
    	n=read();m=read();pw[0]=1;
    	for(int i=1;i<=n;i++)
    	{
    		int x=read();ins(x);
    		pw[i]=pw[i-1]*2%MOD;
    	}
    	printf("%lld\n",dfs(1,1,30));
    }
    
  • 相关阅读:
    lintcode 最长上升连续子序列 II(二维最长上升连续序列)
    lintcode 滑动窗口的最大值(双端队列)
    windows下实现Git在局域网使用
    eclipse导入web项目之后项目中出现小红叉解决办法
    HTTP协议基础
    c++面试常用知识(sizeof计算类的大小,虚拟继承,重载,隐藏,覆盖)
    Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
    c语言表白程序代码
    矩阵的逆矩阵,伴随矩阵
    document.getElementById()与 $()区别
  • 原文地址:https://www.cnblogs.com/C202044zxy/p/15759140.html
Copyright © 2011-2022 走看看