[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] ...
随机推荐
- 《SQL与数据库基础》09. 事务
@ 目录 事务 简介 操作 方式一 方式二 四大特性(ACID) 并发事务问题 事务隔离级别 本文以 MySQL 为例 事务 简介 事务是一组操作的集合,它是一个不可分割的工作单位.事务会把所有的操作 ...
- 《数据结构-C语言》单链表
@ 目录 单链表 结构定义 初始化 建立 清空 求表长 判断是否为空表 取值 查找 插入 删除 销毁 遍历打印 测试 单链表 结构定义 #include <stdio.h> #includ ...
- js详细讲解放大镜的实现
实现放大镜的整体思路 1.当鼠标放在图片上的时候,出现蒙层. 2.出现蒙层,让鼠标在蒙层中心 3.限制蒙层移动的范围 4.放大镜移动 最终实现的效果 鼠标放上去的时候,出现一个蒙层. 蒙层的移动范围只 ...
- 五分钟k8s实战-使用Ingress
背景 前两章中我们将应用部署到了 k8s 中,同时不同的服务之间也可以通过 service 进行调用,现在还有一个步骤就是将我们的应用暴露到公网,并提供域名的访问. 这一步类似于我们以前配置 Ngin ...
- Teamcenter RAC 开发之《PlaceHolder》
背景 做个swing表单,有时候想实现一些网页input标签的placeHolder提示,可能本人写vue or html写多,对某些细节有强迫症,所以找小下资料 实现方法(Swingx) 看源码
- Trie字典
Trie树,又叫字典树,前缀树(Prefix Tree),单词查找树,是一种多叉树的结构. {"a","apple","appeal",&q ...
- c语言代码练习11
//1-100数字中9的数量 #define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> int main(){ int x = 0; int ...
- ElasticSearch系列——倒排索引、删除映射类型、打分机制、配置文件、常见错误
文章目录 1 倒排索引 2 删除映射类型 一 前言 二 什么是映射类型? 三 为什么要删除映射类型? 四 映射类型的替代方法 4.1 将映射类型分开存储在索引中 4.2 自定义类型字段回到顶部 五 没 ...
- Django框架——Web应用、基于SOCKET写一个web应用、 手撸简单web框架、http协议、Web框架(手撸自己的Web框架)、django简介以及简单使用
文章目录 1 Web应用 一 Web应用程序是什么 1.1 Web应用程序的优点 1.2 Web应用程序的缺点 1.3 B/S架构优点 二 基于SOCKET写一个web应用 2.1 main.py 2 ...
- Welcome to the Android Open Source Project!
Android is an open-source software stack for a wide range of mobile devices and a corresponding open ...