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能跑到的最远的点,然后就是求起点和终点所构成的线段与每个雨滴交的时间,注 ...
随机推荐
- filesort是什么意思?
我们碰到有order by 或者group by,或者distinct语句的时候,如果查看执行计划,通常会看到using filesort的字眼,那么这个filesort是不是真是文件排序呢?其实不然 ...
- 将edit ctrL弄的像个dos
case WM_CTLCOLOREDIT: { HWND hShellText = GetDlgItem(hDlg,IDC_TXT_SHELL); if (hShellText == (HWND)lP ...
- 测试过程中LR的关联报错
在测试过程中,录制的脚本会做一些关联.在测试的过程中,常常出现关联失败的情况. 如果最后的结果有检查点,检查点失败而事务失败. 每次出现这样的情况,我都不知道如何办.为了不出现错误,我都在关联函数里面 ...
- CentOS下使用Percona XtraBackup对MySQL5.6数据库innodb和myisam的方法
Mysql卸载从下往上顺序 [root@localhost /]# rpm -e --nodeps qt-mysql-4.6.2-26.el6_4.x86_64[root@localhost /]# ...
- 日常css和js小知识点记录
2015-6-29 1.<meta name="viewport" content="width=device-width,user-scalable=no&quo ...
- JavaEE基础(十八)/集合
1.集合框架(Map集合概述和特点) A:Map接口概述 查看API可以知道: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 B:Map接口和Collection接口的不同 ...
- Python 编码规范
官网规范:https://www.python.org/dev/peps/pep-0008/ 1.不在同一句import中引用多个库 # 正确姿势: import os import sys # 错误 ...
- AR专用汉明码
增强现实技术(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上相应图像.视频.3D模型的技术,这种技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动. ...
- 请求php返回json生成自定义对象
php代码 public function convert_array(){ $arr = array( '0'=>array('name'=>'zc','height'=>173) ...
- A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 77964 Acc ...