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能跑到的最远的点,然后就是求起点和终点所构成的线段与每个雨滴交的时间,注 ...
随机推荐
- ubuntu使用记录
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir ...
- 【python cookbook】【数据结构与算法】16.筛选序列中的元素
问题:提取出序列中的值或者根据某些标准对序列做删减 解决方案:列表推导式.生成器表达式.使用内建的filter()函数 1.列表推导式方法:存在一个潜在的缺点,如果输入数据非常大可能会产生一个庞大的结 ...
- bootstrap导航条在手机上默认展开二级目录,必须用setTimeout才能实现
bootstrap导航条在手机上默认展开二级目录,必须用setTimeout才能实现 $(document).ready(function() { $('.dropdown').hover(funct ...
- hibernate核心接口,和扩展接口。回顾笔记,以前没记,现在补上,纯手工敲的。
hibernate核心接口: 所有的hibernate应用都会访问hibernate的5个核心接口 1,Configuration接口 Configuration用于配置并且根启动Hibernate. ...
- 24、jQuery常用AJAX-API/Java调用MySQL / Oracle过程与函数
1)掌握jQuery常用AJAX-API 2)掌握Java调用MySQL / Oracle过程与函数 一)jQuery常用AJAX-API 目的:简化客户端与服务端进行局部刷新的异步通讯 (1)取 ...
- 用C#.NET编写软件注册机
验证注册码是保护软件产品产权的常用手段.一般过程如下, 1. 软件发行者收集用户特有的信息: 2. 根据用户特有的信息,使用注册机生成注册码并把注册码发给客户: 3. 向软件导入注册码,由软件自 ...
- 2015-09-17 001 存储过程数据操作类 H_data_Helper
using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...
- oracle 导出导入常见问题
oracle 导入导出常见有两种方法 EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用,不能在客户 ...
- WF3.0 CallExternalMethod使用技巧
CallExternalMethod用于工作流向宿主进程中通信 简单的介绍一下它的使用技巧,参照网上的一个questioner源码进行了改进,因为我感觉这个源码提供的通信demo过于繁琐. 看看ser ...
- winform中treeview中节点选中的技巧
我想实现譬如选择某子节点的时候,父节点会自动选中,如果选择父节点,子节点会全部选中,如果子节点全部不选,父节点也要不选. 贴代码 private void tvwMenu_AfterCheck(obj ...