FJNU 1153 Fat Brother And XOR(胖哥与异或)
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(胖哥与异或)的更多相关文章
- FJNU 1154 Fat Brother And His Love(胖哥与女神)
FJNU 1154 Fat Brother And His Love(胖哥与女神) Time Limit: 2000MS Memory Limit: 257792K [Description] [ ...
- FJNU 1155 Fat Brother’s prediction(胖哥的预言)
FJNU 1155 Fat Brother’s prediction(胖哥的预言) Time Limit: 1000MS Memory Limit: 257792K [Description] [ ...
- FJNU 1152 Fat Brother And Integer(胖哥与整数)
FJNU 1152 Fat Brother And Integer(胖哥与整数) Time Limit: 1000MS Memory Limit: 257792K [Description] [题 ...
- FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼)
FJNU 1156 Fat Brother’s Gorehowl(胖哥的血吼) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目 ...
- FJNU 1151 Fat Brother And Geometry(胖哥与几何)
FJNU 1151 Fat Brother And Geometry(胖哥与几何) Time Limit: 1000MS Memory Limit: 257792K [Description] [ ...
- FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)
FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术) Time Limit: 1000MS Memory Limit: 257792K [Description ...
- FJNU 1159 Fat Brother’s new way(胖哥的新姿势)
FJNU 1159 Fat Brother’s new way(胖哥的新姿势) Time Limit: 1000MS Memory Limit: 257792K [Description] [题目 ...
- 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, ...
- HDU 4637 Rain on your Fat brother 线段与半圆和线段交 简单题
题意: 应该不难读懂. 做法: 我们可以把雨滴看做静止不动,然后maze(这题的那个人)就是往左上方运动就可以了,计算出maze能跑到的最远的点,然后就是求起点和终点所构成的线段与每个雨滴交的时间,注 ...
随机推荐
- delphi注册/反注册OCX
uses ShellAPI; function ExecAndWait(const ExecuteFile, ParamString : string): boolean; var SEInfo: T ...
- 三层架构中bll层把datatable转换为实体model的理解
看了很多人的项目,很多都是用到三层架构,其中BLL层中有一种将DataTable转换为实体的方法.一直没有明白为啥要这样做,今天特意去搜索了一下,如果没有答案我是准备提问,寻求解答了.还好找到一个相关 ...
- Linux 的 Crontab 命令运用(转)
cron来源于希腊单词chronos(意为“时间”),是linux系统下一个自动执行指定任务的程序.例如,你想在每晚睡觉期间创建某些文件或文件夹的备份,就可以用cron来自动执行. 服务的启动和停止 ...
- stc89c52开发板遥控器解码 红外线发射 内置 eeprom 存储 串口显示编码
单片机,大概三年前,就买了一本 <爱上单片机> 最后就学会,用面包板了,编程书上基本没讲. 看原理图,看时序图,看数据手册, 都没讲. 而且书上自带的代码写的很烂. 1,缩近控制不好 2, ...
- javaWeb el表达式和jstl快速入门案例
<%@page import="de.bvb.domain.Person"%> <%@page import="de.bvb.domain.Addres ...
- Java程序编译和运行的过程【转】
转自:http://www.360doc.com/content/14/0218/23/9440338_353675002.shtml Java整个编译以及运行的过程相当繁琐,本文通过一个简单的程序来 ...
- textarea 在浏览器中禁用拖动和固定大小
HTML 标签 textarea 在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定 textarea 的尺寸,大小就不会改变,不过更好的办法是使用 CSS 的 height 和 ...
- HDU 2136:Computer(树形DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 Computer Description A school bought the fi ...
- Mysql ibdata 丢失或损坏如何通过frm&ibd 恢复数据
mysql存储在磁盘中,各种天灾人祸都会导致数据丢失.大公司的时候我们常常需要做好数据冷热备,对于小公司来说要做好所有数据备份需要支出大量的成本,很多公司也是不现实的.万一还没有做好备份,数据被误删除 ...
- hdwiki中模板的使用说明
HDwiki所有模版文件都在根目录view下的default文件里,以admin_开头的是后台的模版文件,其它不是以admin_开头的,就是所有的前台文件.具体列表如下:首页模版文件: index. ...