2023春训练13

训练地址:vjudge

D.Three Days Ago

怎么形容这个问题呢?就是说,给你一个由数字组成的字符串S,判断其中存在多少个[l,r],使得从\(S_l\)\(S_r\)中每种数字的个数都是偶数。
因为数字总共就10种,所以可以用十位二进制的来判断从1到i位的数字存在结果。遍历S,每位都用异或来获得当前位的代表值,如果说这个代表值前面已经出现过了n次,那么最终的结果就可以加上n-1种分组。同时注意,如果说遍历的异或值刚好等于0,那么其实我们需要在统计数组中提前加一个0,不然就整体少了一个匹配的0(因为第一个异或得0就是一种结果,但是前面的说明不能体现这种特殊性)。
详见代码:

//>>>Qiansui
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
//#define int long long

inline ll read()
{
	ll x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
using namespace std;
const int maxm=2e5+5,inf=0x3f3f3f3f,mod=998244353;

int gcd(int a,int b){
	if(a<b) swap(a,b);
	if(b==0) return a;
	else return gcd(b,a%b);
}
string ss;

void solve(){
	cin>>ss;
	map<int,int> q;
	int t=0,a=0;
	ll ans=0;
	++q[0];//key!
	for(int i=0;i<ss.size();++i){
		t=ss[i]-'0';
		a^=(1<<t);
		++q[a];
		ans+=q[a]-1;
	}
	cout<<ans<<'\n';
	return ;
}

signed main(){
	// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int _=1;
//	cin>>_;
	while(_--){
		solve();
	}
	return 0;
}

E.Kth Number

题解——题解
再理解理解!

posted on 2023-03-27 21:09  Qiansui  阅读(14)  评论(0编辑  收藏  举报