首页 > 其他分享 >CF1816F Xor Counting - dp - 分治 -

CF1816F Xor Counting - dp - 分治 -

时间:2023-04-16 11:33:05浏览次数:57  
标签:Xor 奇数 异或 long cdots CF1816F Counting oplus mod

题目链接:https://codeforces.com/contest/1816/problem/F

题解:
一道有趣的题。

  • 首先发现 \(m=1\) 和 \(m\geq 3\) 时结论是平凡的。\(m=1\) 时结论显然,下面讨论一下 \(m\geq 3\) 时:
    首先可以构造 \([x, (n-x)/2, (n-x)/2, \cdots]\),其中 \(x\) 和 \(n\) 同奇偶,显然此时异或值可以取到 \(1\cdots n\) 的所有和 \(n\) 奇偶性相同的值。另一方面,一个重要观察是 \(a_1 \oplus \cdots \oplus a_n\) 的奇偶性和 \(a_1+\cdots+a_n\) 相同,因此异或值至多取到所有和 \(n\) 奇偶性相同的值。因此这块可以 \(O(1)\) 求出。
  • 下面我们讨论一下 \(m=2\) 的情况。
    设 \(f_n\) 表示 \(a_1+a_2=n\) 时的答案,考虑分治:
    如果 \(n\) 为奇数,那么 \(a_1, a_2\) 一奇一偶,不失一般性设 \(a_1, a_2\) 分别为奇数 偶数,如果令 \(b_1=(a_1-1)/2, b_2=a_2/2\),那么 \(b_1+b_2=(n-1)/2, b_1\oplus b_2=2\times(a_1\oplus a_2)+1\),因此我们还需要记一个 \(g_n\) 表示有多少种不同的异或值(因为这个 +1 是对于所有不同的异或值都要算的),\(g_n\) 此时的转移也很简单。综上我们有 \(n\) 为奇数时 \(f_n=2\times f_{n/2-1}+g_{n}\) 以及 \(g_n=g_{(n-1)/2}\)
    如果 \(n\) 为偶数,那么 \(a_1, a_2\) 要么都是奇数,要么都是偶数,还是考虑 /2 分治,此时由于 \(n\) 奇数,所以没有 +1 的项,因此和上一种情况完全相同的推导我们有 \(f_n=2\times (f_{n/2}+f_{n/2-1})\) 以及 \(g_n=g_{n/2}+g_{n/2-1}\)。

直接递推实现即可,复杂度 \(O(\log n)\)。
代码:

// by SkyRainWind
#include <bits/stdc++.h>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define pii pair<int,int>
#define pb push_back

using namespace std;

typedef long long ll;
typedef long long LL;

const int inf = 1e9, INF = 0x3f3f3f3f, mod = 998244353;

map<ll,ll>f,g;
void solve(ll n){
	if(n == 0){
		f[n] = 0, g[n] = 1;
		return ;
	}
	if(f.count(n))return ;
	if(n%2 == 1){
		solve(n/2);
		(f[n] = 2ll*f[n/2]%mod + g[n/2])%=mod;
		g[n] = g[n/2];
	}else{
		solve(n/2);solve(n/2-1);
		f[n] = 2ll * (f[n/2] + f[n/2-1]) % mod;
		g[n] = (g[n/2] + g[n/2-1]) % mod;
	}
}

signed main(){
	int te;scanf("%d",&te);
	while(te --){
		f.clear(), g.clear();
		ll n,m;cin >> n >> m;
		if(m == 1){
			cout << n%mod << '\n';
		}else if(m >= 3){
			if(n%2 == 1)cout << (n+1)/2%mod*((n+1)/2%mod)%mod << '\n';
			else cout << (1+n/2%mod)*(n/2%mod) % mod << '\n';
		}else{
			solve(n);
			cout << f[n] << '\n';
		}
	}

	return 0;
}

标签:Xor,奇数,异或,long,cdots,CF1816F,Counting,oplus,mod
From: https://www.cnblogs.com/SkyRainWind/p/17322823.html

相关文章

  • [ARC127D] Sum of Min of Xor 题解
    先把\(i\)对\(j\)的约束去掉。没有\(\min\)的情况是trival的,发现瓶颈在于如何比较两个数之间的大小。可以发现,对两个二进制数,我们本质上是想要找到它们第一个不同的位置。于是考虑从最高位开始,将\((a_i,b_i)\)按最高位分组为\((0,0),(0,1),(1,0),(1,1)\)四组。每组内......
  • unity xorpay使用HTTP中post方式请求调用接口
    结合:https://www.cnblogs.com/guangzhiruijie/p/16985533.htmlunity自带的UnityWebRequest提供了构成HTTP请求和处理HTTP响应。构造函数:publicUnityWebRequest();publicUnityWebRequest(Uriuri);publicUnityWebRequest(stringurl);publicUnityWebRequest(Uriuri,......
  • buuctf 新年快乐、内涵的软件、xor
    内涵的软件下载解压文件后双击执行,没有任何提示将文件拖进exeinfope 发现查不出壳,并且为32位的文件,拖进ida32,shift+f12查找字符串,找到flag 新年快乐打开ida发......
  • Go Xorm简单使用
    官网相关文档https://xorm.io/zh/docs/chapter-01/1.engine/https://gitea.com/xorm/xorm/src/branch/master/README_CN.mdxorm是一个简单而强大的Go语言ORM库.通过......
  • xor shift
    64位。整数到整数的随机映射。x^=x<13,x^=x>>7,x^=x<<17为了防止被对着卡,可以在前面和后面各让x异或一个随机的常数。拿来树哈希,\(h(x)=hash(\{h_v\})\)dls说自然......
  • [AtCoder] B - Counting Grids
      Thekeyobservationisthatthereisalwaysatmost1cellthatviolatesbothconditions. Proof: ifxviolatesbothconditions,thatmeansallothe......
  • D2. Xor-Subsequence (hard version)
    D2.Xor-Subsequence(hardversion)/*进行转换,可以要存储i^a[i]的值首先确保前面都是相同的然后假设下一位是不相同的,那么会在这里进行更新,都枚举一下就可以了字......
  • D. XOR Permutations
    D.XORPermutations注意多次输入输出不要忘了初始化注意分析代码点击查看代码#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#inc......
  • [CF1788F] XOR, Tree, and Queries
    题目描述Youaregivenatreeof$n$vertices.Theverticesarenumberedfrom$1$to$n$.Youwillneedtoassignaweighttoeachedge.Lettheweight......
  • P3605 [USACO17JAN]Promotion Counting P
    求某节点子树内比该节点的点权大的点的个数 值域上维护树状数组,#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+2,M=N*2;intbin[N],len;......