FJNU 1153 Fat Brother And XOR(胖哥与异或)

Time Limit: 1000MS   Memory Limit: 257792K

【Description】

【题目描述】

Fat brother had master ACM, recently he began to study the operation of XOR (the operation “^”). He thought of a very interesting question: select arbitrary k positive integers from the n positive integers, then XOR the selected k digital, sum all cases of XOR. Now he wants to quickly calculate the results. Maybe the results will be great, just modulo 20162333.

For example, 3 integers: 1 2 3, select arbitrary 2 positive integers, the all cases:

{1,2}, {2, 3}, {1, 3}. So the results is {(1 ^ 2) + (2 ^ 3) + (1 ^ 3)} %20162333

胖哥是个ACM大牛,最近他在学习异或操作(运算符为“^”)。他想到了个有趣的问题:从n个正整数中任选k个,然后对这k个数异或,对所有异或的结果求和。现在他想快速计算结果。最终结果可能很大,于是模20162333。

例如,3个整数:1 2 3,选择任意2个正整数,所有情况为:

{1,2}, {2, 3}, {1, 3}。因此结果为{(1 ^ 2) + (2 ^ 3) + (1 ^ 3)} %20162333

【Input】

【输入】

There are multiple test cases. The first line of input contains an integer T (T <= 20) indicating the number of test cases. For each test case:

The first line contains two integer n, k (1 <= k <= n <= 1000)

The second line contains n integer ai (1 <= ai <= 1000000000)

多组测试用例。

第一行是一个整数T(T <= 20)表示测试用例的数量。对于每个测试用例:

第一行有两个整数,k(1 <= k <= n <= 1000)

第二行有n个整数ai(1 <= ai <= 1000000000)

【Output】

【输出】

For each test case, output the sum % 20162333

对于每个测试用例,输出和%20162333

【Sample Input - 输入样例】

【Sample Output - 输出样例】

2

3 2

1 2 3

6 2

6 6 6 6 6 6

6

0

【Hint】

【提示】

The

first sample test case: (1 ^ 2) + (2 ^ 3) + (1 ^ 3) = 6

The

second sample test case: (6 ^ 6) * 15 = 0

第一个样例:

(1 ^ 2) + (2 ^ 3) + (1 ^ 3) = 6

第二个样例:

(6 ^ 6) * 15 = 0

【题解】

这道题需要以二进制的观点来看待每个ai

举个例子:

Xai表示二进制的下ai的第X位(为方便表示从0开始)

(a1 ^ a2) + (a2 ^ a3) + (a1 ^ a3)

=[(0a1 ^ 0a2) + (0a2 ^ 0a3) + (0a1 ^ 0a3)]*20 +

[(1a1 ^ 1a2) + (1a2 ^ 1a3) + (1a1 ^ 1a3)]*21 +

[(2a1 ^ 2a2) + (2a2 ^ 2a3) + (2a1 ^ 2a3)]*22 +

…………………………………………………………………………… +

[(Xa1 ^ Xa2) + (Xa2 ^ Xa3) + (Xa1 ^ Xa3)]*2X

此时只剩0和1了

我们可以很轻易地知道:0 ^ 0 = 0, 1 ^ 0 = 1, 1 ^ 1 = 0

当然了,a ^ b = b ^ a

所以除了1 ^ 0之外,其他结果为0的情况都是无用的。

因此,题目转换为:求二进制下,1 ^ 0 这种情况出现的次数。

统计ai中二进制第X位出现几次1,保存到bit[x]

第X位提供的1 = 2X * 有效情况数

有效情况数就是由bit[X]个1与 n-bit[X]个0组合出来的

并且只有取奇数个1的时候,异或的结果才能为1(1 ^ 1 = 0)

由此可以得出

当前有效情况数:

最后利用递推公式快速求组合数c(n,m)=c(n-1,m-1)+c(n-1,m)

(当然你把这个公式当成杨辉三角也是可以的:))

【代码 C++】

 #include<cstdio>
#include <cstring>
#define mx 1005
#define mod 20162333
int main(){
int t, n, k, i, j, w, opt, bit[], c[mx][mx];
for (i = ; i < mx; ++i){
for (c[i][] = c[i][i] = j = ; j < i; ++j)
c[i][j] = (c[i - ][j] + c[i - ][j - ]) % mod;
}
while (~scanf("%d", &t)){
while (t--){
memset(bit, , sizeof(bit));
scanf("%d%d", &n, &k);
for (i = ; i < n; ++i){
scanf("%d", &j);
for (w = ; j; j >>= ) bit[w++] += j & ;
}
for (i = opt = ; i < ; ++i){
for (j = ; j <= k; j += ){
opt += (1LL << i) *c[bit[i]][j] % mod*c[n - bit[i]][k - j] % mod;
opt %= mod;
}
}
printf("%d\n", opt);
}
}
return ;
}
 #include<cstdio>
#include <cstring>
#define mx 1005
#define mod 20162333
int read_int(){
int add = getchar() - '';
int a = getchar();
while (a >= '' && a <= '') add = add * + a - '', a = getchar();
return add;
}
int main(){
int t, n, k, i, j, w, opt, bit[], *c[mx];
for (i = ; i < mx; ++i){
c[i] = new int[i + ];
for (c[i][] = c[i][i] = j = ; j < i; ++j)
c[i][j] = (c[i - ][j] + c[i - ][j - ]) % mod;
}
while (t = read_int(), t>){
while (t--){
memset(bit, , sizeof(bit));
n = read_int(); k = read_int();
for (i = ; i < n; ++i){
for (w = , j = read_int(); j; j >>= ) bit[w++] += j & ;
}
for (i = opt = ; i < ; ++i){
for (j = ; j <= k; j += ){
if (bit[i] < j || n - bit[i] < k - j) continue;
opt += (1LL << i) *c[bit[i]][j] % mod*c[n - bit[i]][k - j] % mod;
opt %= mod;
}
}
printf("%d\n", opt);
}
}
return ;
}

伪·优化

FJNU 1153 Fat Brother And XOR(胖哥与异或)的更多相关文章

  1. FJNU 1154 Fat Brother And His Love(胖哥与女神)

    FJNU 1154 Fat Brother And His Love(胖哥与女神) Time Limit: 2000MS   Memory Limit: 257792K [Description] [ ...

  2. FJNU 1155 Fat Brother’s prediction(胖哥的预言)

    FJNU 1155 Fat Brother’s prediction(胖哥的预言) Time Limit: 1000MS   Memory Limit: 257792K [Description] [ ...

  3. FJNU 1152 Fat Brother And Integer(胖哥与整数)

    FJNU 1152 Fat Brother And Integer(胖哥与整数) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题 ...

  4. FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼)

    FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题目 ...

  5. FJNU 1151 Fat Brother And Geometry(胖哥与几何)

    FJNU 1151 Fat Brother And Geometry(胖哥与几何) Time Limit: 1000MS   Memory Limit: 257792K [Description] [ ...

  6. FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)

    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术) Time Limit: 1000MS   Memory Limit: 257792K [Description ...

  7. FJNU 1159 Fat Brother’s new way(胖哥的新姿势)

    FJNU 1159 Fat Brother’s new way(胖哥的新姿势) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题目 ...

  8. CF 979D Kuro and GCD and XOR and SUM(异或 Trie)

    CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...

  9. HDU 4637 Rain on your Fat brother 线段与半圆和线段交 简单题

    题意: 应该不难读懂. 做法: 我们可以把雨滴看做静止不动,然后maze(这题的那个人)就是往左上方运动就可以了,计算出maze能跑到的最远的点,然后就是求起点和终点所构成的线段与每个雨滴交的时间,注 ...

随机推荐

  1. 160930、Javascript的垃圾回收机制与内存管理

    一.垃圾回收机制-GC Javascript具有自动垃圾回收机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存. 原理:垃圾收集器会定期(周期性 ...

  2. Makefile 使用总结【转】

    转自:http://www.cnblogs.com/wang_yb/p/3990952.html 1. Makefile 简介 Makefile 是和 make 命令一起配合使用的. 很多大型项目的编 ...

  3. 织梦系统中出现DedeTag Engine Create File False提示原因及解决方法

    今天更新网站时dedecms系统时,遇到一个问题:DedeTag Engine Create File False  出现这样的提示. 其实这也不算是什么错误,我个人觉得最重要的一点就是根目录下没有给 ...

  4. PMO到底什么样?

    PMO到底什么样? 当将来项目办理单位彻底健全了,达到最老练的程度的时分项目办理单位应当干哪些活,有哪些大块功能,也即是关于一个全部的PMO它的功能跟人物都包含啥? 下面这个模型精确的说是英国的项目办 ...

  5. js函数自执行

    在javascript里,任何function在执行的时候都会创建一个执行上下文,因为function声明的变量和function有可能只在该function内部,这个上下文,在调用function的 ...

  6. SDUT 2608:Alice and Bob

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...

  7. Camera 图像处理原理分析

    1         前言 做为拍照手机的核心模块之一,camera sensor效果的调整,涉及到众多的参数,如果对基本的光学原理及sensor软/硬件对图像处理的原理能有深入的理解和把握的话,对我们 ...

  8. Auty自动化测试框架第一篇——生成执行列表

    [本文出自天外归云的博客园] 在Auty的scripts文件夹中编写一个create_selection.py文件,用于在同级目录下针对同级目录scripts下的所有脚本生成一个selection.t ...

  9. 用WebDriver实现基于jira过滤器视图的统计自动化

    在Jira上通过过滤器我们可以做出多种视图,以方便统计我们想要收集的结果.比如:我想查看所有分派给我的任务.在Jira上,我保存了一个过滤器,叫做“分派给我的所有任务”.这个过滤器可以过滤出所有分配给 ...

  10. 2016年10月28日 星期五 --出埃及记 Exodus 19:13

    2016年10月28日 星期五 --出埃及记 Exodus 19:13 He shall surely be stoned or shot with arrows; not a hand is to ...