[ARC156D] Xor Sum 5
Problem Statement
You are given a sequence of non-negative integers and a positive integer .
Find the bitwise of over all sequences of positive integer sequences such that .
What is bitwise ?
The bitwise of non-negative integers and , , is defined as follows:
- When is written in base two, the digit in the 's place () is if exactly one of the digits in that place of and is , and otherwise.
Generally, the bitwise of non-negative integers is defined as . We can prove that this value does not depend on the order of .
Constraints
- 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: , for which is , respectively. Thus, the answer is .
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
看着样例,会发现根据异或的性质,很多情况都会被约掉。
那么推一波生成函数,最终 是否有有贡献,就要看 为奇数还是偶数。
,
同理,
可以对 进行二进制分解后套入这个式子,然后考虑他的实际意义。此时将 拆为 ,那么将题目改为如果将 个数分成很多段,每一段的长度都是2的正整数幂,且填一样的数,答案不变。
这个结论也有感性的理解方式。可以通过二进制分组的方式,给所有不满足这个要求的序列某两个数交换一下,和不变,异或和消掉。所有不满足要求的序列都能一一对应上。
所以知道这个后,可以数位 dp。定义 为考虑到第 位,目前所有进位+填入的 的和为 的方案数。如果 是 二进制分组后的部分,那么就可以枚举新选什么数。同时统计答案时,注意到这里真正的方案数要乘上 的一个次方,所以如果 为奇数或者这里是 的最大的一个二进制位时,才会统计入答案。
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?