[ARC156D] Xor Sum 5
Problem Statement
You are given a sequence of $N$ non-negative integers $A=(A_1,A_2,\dots,A_N)$ and a positive integer $K$.
Find the bitwise $\mathrm{XOR}$ of $\displaystyle \sum_{i=1}^{K} A_{X_i}$ over all $N^K$ sequences of $K$ positive integer sequences $X=(X_1,X_2,\dots,X_K)$ such that $1 \leq X_i \leq N\ (1\leq i \leq K)$.
What is bitwise $\mathrm{XOR}$?
The bitwise $\mathrm{XOR}$ of non-negative integers $A$ and $B$, $A \oplus B$, is defined as follows:
- When $A \oplus B$ is written in base two, the digit in the $2^k$'s place ($k \geq 0$) is $1$ if exactly one of the digits in that place of $A$ and $B$ is $1$, and $0$ otherwise.
For example, we have $3 \oplus 5 = 6$ (in base two: $011 \oplus 101 = 110$).
Generally, the bitwise $\mathrm{XOR}$ of $k$ non-negative integers $p_1, p_2, p_3, \dots, p_k$ is defined as $(\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k)$. We can prove that this value does not depend on the order of $p_1, p_2, p_3, \dots, p_k$.
Constraints
- $1 \leq N \leq 1000$
- $1 \leq K \leq 10^{12}$
- $0 \leq A_i \leq 1000$
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
$N$ $K$
$A_1$ $A_2$ $\dots$ $A_N$
Output
Print the answer.
Sample Input 1
2 2
10 30
Sample Output 1
40
There are four sequences to consider: $(X_1,X_2)=(1,1),(1,2),(2,1),(2,2)$, for which $A_{X_1}+A_{X_2}$ is $20,40,40,60$, respectively. Thus, the answer is $20 \oplus 40 \oplus 40 \oplus 60=40$.
Sample Input 2
4 10
0 0 0 0
Sample Output 2
0
Sample Input 3
11 998244353
314 159 265 358 979 323 846 264 338 327 950
Sample Output 3
236500026047
看着样例,会发现根据异或的性质,很多情况都会被约掉。
那么推一波生成函数,最终 \(S\) 是否有有贡献,就要看 \([x^S](\sum\limits_{i=1}^nx^{a_i})^K\) 为奇数还是偶数。
\((x_1+x_2+\cdots+x_n)^2=x_1^2+x_2^2+\cdots+x_n^2+2(\cdots)=x_1^2+x_2^2+\cdots+x_n^2\),
同理,\((x_1+x_2+\cdots+x_n)^{2^k}=x_1^{2^k}+x_2^{2^k}+\cdots+x_n^{2^k}\)
可以对 \(K\) 进行二进制分解后套入这个式子,然后考虑他的实际意义。此时将 \(K\) 拆为 \(2^{k_1}+2^{k_2}+\cdots+2^{k_n}\),那么将题目改为如果将 \(K\) 个数分成很多段,每一段的长度都是2的正整数幂,且填一样的数,答案不变。
这个结论也有感性的理解方式。可以通过二进制分组的方式,给所有不满足这个要求的序列某两个数交换一下,和不变,异或和消掉。所有不满足要求的序列都能一一对应上。
所以知道这个后,可以数位 dp。定义 \(dp_{i,j}\) 为考虑到第 \(i\) 位,目前所有进位+填入的 \(a\) 的和为 \(j\) 的方案数。如果 \(i\) 是 \(K\) 二进制分组后的部分,那么就可以枚举新选什么数。同时统计答案时,注意到这里真正的方案数要乘上 \(n\) 的一个次方,所以如果 \(n\) 为奇数或者这里是 \(K\) 的最大的一个二进制位时,才会统计入答案。
#include<bits/stdc++.h>
using namespace std;
const int N=4005;
long long k,ans;
int n,a[N],dp[55][N];
int main()
{
scanf("%d%lld",&n,&k);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
dp[0][0]=1;
for(int i=0;i<=50;i++)
{
for(int j=0;j<1024;j++)
{
if(!dp[i][j])
continue;
if(k>>i&1)
{
for(int p=1;p<=n;p++)
{
if((j+a[p]&1)&&(n&1||(1LL<<i+1)>k))
ans^=1LL<<i;
dp[i+1][j+a[p]>>1]^=1;
}
}
else
{
if((j&1)&&(n&1||(1LL<<(i+1))>k))
ans^=1LL<<i;
dp[i+1][j>>1]^=1;
}
}
}
printf("%lld",ans);
return 0;
}
[ARC156D] Xor Sum 5的更多相关文章
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- 字典树-百度之星-Xor Sum
Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...
- HDU 4825 Xor Sum 字典树+位运算
点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) ...
- 2014百度之星第三题Xor Sum(字典树+异或运算)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- Xor Sum 01字典树 hdu4825
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
- hdu 4825 Xor Sum (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...
- HDU--4825 Xor Sum (字典树)
题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...
- hdu 4825 Xor Sum trie树
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Proble ...
- hdu 4825 Xor Sum(trie+贪心)
hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...
- UVALive4682 XOR Sum
UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...
随机推荐
- 【微信自动化】使用c#实现微信自动化
引言 上个月,在一个群里摸鱼划水空度日,看到了一个老哥分享的一个微信自动化的一个类库,便下载了他的Demo,其本意就是模拟鼠标来操作UI,实现UI自动化:然后自己在瞎琢磨研究,写了一个简单的例子,用来 ...
- (2023.7.24)软件加密与解密-2-1-程序分析方法[XDbg].md
body { font-size: 15px; color: rgba(51, 51, 51, 1); background: rgba(255, 255, 255, 1); font-family: ...
- 物理服务器不重启分配raid
一.MegaCli 命令的安装及使用 目录 一.MegaCli 命令的安装及使用 1.下载rpm包 2.安装 3.安装完,就会在/opt/下创建个MegaRAID目录,文件都在里面 4.添加软连接 5 ...
- 用一个示例来学习DockerFile
在Docker的世界里,我们经常会听到Dockerfile这个词.那么,什么是Dockerfile?它如何工作?本文将简要介绍Dockerfile的基本概念,原理以及一些常用的Dockerfile命令 ...
- Solution -「HNOI 2010」城市建设
Description Link. 修改边权的动态 MST. Solution 讲清楚点. 修改边权的 MST,考虑对时间分治.设我们当前操作的操作区间是 \([l,r]\),直接暴力找 MST 是不 ...
- ConcurrentHashMap底层源码分析
ConcurrentHashMap源码底层分析 1.ConcurrentHashMap初始化 jdk8之后,ConcurrentHashMap采用了HashMap的底层结构(数据,链表,红黑树),在此 ...
- IP协议的发展历程
1. IP协议 1.1为什么需要IP协议 好像ip地址就像每个人的家门号一样家喻户晓,被大家默认用来作为寻址的门牌号,起初,设计IP地址也是为了寻找某台主机,但是作为世界上家喻户晓的IPv4,大家不应 ...
- Flask框架——请求扩展、flask中间件、蓝图、分析线程和协程
文章目录 01 请求扩展 01 before_first_request :项目启动后第一次请求的时候执行 02 before_request:每次请求之前执行 03 after_request:每次 ...
- 每天5分钟复习OpenStack(三)
每天5分钟复习OpenStack(三) 为什么要拉起kvm 虚拟机要熟悉这些操作? 作为一个运维工程师,将来有大量的时间是在制作镜像,镜像的制作就是在kvm虚拟化环境拉起kvm 管理的虚拟机的过程,安 ...
- Django+celery+eventlet+flower+redis异步任务创建及查询实现
1.环境版本:Django 3.2.12celery 5.3.4eventlet 0.33.3flower 2.0.1redis 3.5.3项目名称:new_project 2.celery配置(se ...