首页 > 其他分享 >「题解」Codeforces 1322B Present

「题解」Codeforces 1322B Present

时间:2022-11-02 10:56:56浏览次数:45  
标签:typedef ch int 题解 sum 1322B Codeforces include mod

看上去异或里面套了层加法不好拆位,但是依然可以对每个二进制位处理。

现在考虑第 \(k\) 位,那么产生贡献的数字对一定满足以下条件之一:

  • 第 \(k\) 位相同且前 \((k-1)\) 位进位;
  • 第 \(k\) 位不同且前 \((k-1)\) 位不进位。

那就按照前 \((k-1)\) 位排序,然后能产生进位的一定是一个后缀,所以维护一个前缀第 \(k\) 位是 \(1\) 的个数,然后双指针就能算合法的数字对个数了。

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n';
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n';
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=998244353;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
const int N=400010;
int bit(int x){
	return 1<<x;
}
int all(int x){
	return (1<<x)-1;
}
int n,a[N],s[N];
signed main(){
	#ifdef do_while_true
//		assert(freopen("data.in","r",stdin));
//		assert(freopen("data.out","w",stdout));
	#endif
	read(n);
	for(int i=1;i<=n;i++)read(a[i]);
	int ans=0;
	for(int o=0;o<=24;o++){
		ll sum=0;
		if(!o){
			for(int i=1;i<=n;i++)s[i]=((a[i]>>o)&1)+s[i-1];
			for(int i=1;i<=n;i++){
				if(a[i]&bit(o))
					sum+=i-1-s[i-1];
				else
					sum+=s[i-1];
			}
			if(sum&1)ans|=bit(o);
		}
		else{
			sort(a+1,a+n+1,[&](const int &x,const int &y){return (x&all(o))<(y&all(o));});
			for(int i=1;i<=n;i++)s[i]=((a[i]>>o)&1)+s[i-1];
			int p=n+1;
			for(int i=1;i<=n;i++){
				while(p>=1&&(((a[p-1]&all(o))+(a[i]&all(o)))&bit(o)))--p;
				//[p,n]进位
				//[1,p-1]不进位
				if(a[i]&bit(o)){
					sum+=p-1-s[p-1];
					sum+=s[n]-s[p-1];
				}
				else{
					sum+=s[p-1];
					sum+=(n-p+1)-(s[n]-s[p-1]);
				}
				sum-=p<=i;
			}
			sum/=2;
			if(sum&1)ans|=bit(o);
		}
	}
	cout << ans << '\n';
    #ifdef do_while_true
//		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}

标签:typedef,ch,int,题解,sum,1322B,Codeforces,include,mod
From: https://www.cnblogs.com/do-while-true/p/16850286.html

相关文章

  • 「题解」Codeforces 930E Coins Exhibition
    看到题就先容斥。然后容斥系数太难算了就寄了,大概要分好几种情况讨论,于是就弃了。不容斥也能做。考虑限制将串划分成了若干段,然后一段一段dp.有没有什么好的方法描述这个......
  • 20221102模拟赛题解
    A-Holy思路由于要让最小值最大,不难想到二分答案。二分后将原矩阵中大于等于\(mid\)的值设为\(1\),小于的设为\(0\),然后将每一行压成二进制,存在两行满足要求当且仅当两......
  • CSP-S 2022 题解
    「CSP-S2022」假期计划\(1\toa\tob\toc\tod\to1\)中\(a,b,c,d\)是\(4\)个不同的景点是突破点,数据范围允许枚举其中的两个。便很自然想到枚举中间的\(b,c\),并......
  • 2022年4月第十三届蓝桥杯省赛C组C语言 习题解析(每日一道)
    试题B:特殊时间   【问题描述】           2022年2月22日22:20是一个很有意义的时间,年份为2022,由3个2和1个0组   成,如果将月和日......
  • 洛谷 P8820 [CSP-S 2022] 数据传输 题解
    首先考虑对于每一次询问暴力DP。设\(f_{u,i}\)表示从\(s\)开始,传到一个到\(u\)距离为\(i\)的点,需要的最短时间。当\(k=3\)时,可能会传到不在\(s\tot\)路......
  • Codeforces - 1391C - Cyclic Permutations(思维 + 组合数学 + 数论 + 图论、*1500)
    1391C-CyclicPermutations(⇔源地址)目录1391C-CyclicPermutations(⇔源地址)tag题意思路AC代码错误次数:0tag⇔思维、⇔组合数学、⇔数论、⇔......
  • Codeforces Round #611 (Div. 3) D
    D.ChristmasTrees显然对于一个中间的点要是不能向两边最近的扩展我们直接判定他没有用处了这样我们就有了bfs的做法我们先把原点放进去要是能扩展我们显然可以直接......
  • Codeforces Round #667 (Div. 3) ABCD
    https://codeforces.com/contest/1409A.YetAnotherTwoIntegersProblem题目大意:k∈[1;10]我们每次可以选择a:=a+kora:=a−k问a要经历多少次操作变成b?input......
  • Codeforces 1730 D
    Codeforces1730D题意定义一次“操作”为把字符串$a$的前$k$个字母与字符串$b$的后$k$个字母交换。问能不能经过有限次操作后,让$a=b$。注:$......
  • spring-boot 配置 swagger常见版本不匹配问题解决方案
    https://stackoverflow.com/questions/70036953/spring-boot-2-6-0-spring-fox-3-failed-to-start-bean-documentationpluginsboo一般出现在2.6.*的springboot版本,这里解......