HDU6186 CS Course【位运算+前缀和+后缀和】
程序员文章站
2024-02-09 16:50:28
...
CS Course
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1964 Accepted Submission(s): 812
Problem Description
Little A has come to college and majored in Computer and Science.
Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.
Here is the problem:
You are giving n non-negative integers a1,a2,⋯,an, and some queries.
A query only contains a positive integer p, which means you
are asked to answer the result of bit-operations (and, or, xor) of all the integers except ap.
Today he has learned bit-operations in Algorithm Lessons, and he got a problem as homework.
Here is the problem:
You are giving n non-negative integers a1,a2,⋯,an, and some queries.
A query only contains a positive integer p, which means you
are asked to answer the result of bit-operations (and, or, xor) of all the integers except ap.
Input
There are no more than 15 test cases.
Each test case begins with two positive integers n and p
in a line, indicate the number of positive integers and the number of queries.
2≤n,q≤105
Then n non-negative integers a1,a2,⋯,an follows in a line, 0≤ai≤109 for each i in range[1,n].
After that there are q positive integers p1,p2,⋯,pqin q lines, 1≤pi≤n for each i in range[1,q].
Each test case begins with two positive integers n and p
in a line, indicate the number of positive integers and the number of queries.
2≤n,q≤105
Then n non-negative integers a1,a2,⋯,an follows in a line, 0≤ai≤109 for each i in range[1,n].
After that there are q positive integers p1,p2,⋯,pqin q lines, 1≤pi≤n for each i in range[1,q].
Output
For each query p, output three non-negative integers indicates the result of bit-operations(and, or, xor) of all non-negative integers except ap in a line.
Sample Input
3 31 1 1123
Sample Output
1 1 01 1 01 1 0
Source
问题链接:HDU6186 CS Course
问题简述:(略)
问题分析:
这个问题出现了三种运算,分别是与、或和异或。
查询时,输入k,求除了a[k]之外,所有a[i]与、或和异或的结果。这也是该问题要做的计算。
异或运算比较好办,因为x^a^a = x,所以先将所有数异或起来,然后再计算。
解法一:
采用前缀和计算原理,先算出前缀与以及前缀或。同理,也算出后缀与以及后缀或。再计算结果就简单了。
解法二:
异或运算同解法一。
与运算以及或运算,可以通过统计各个数的各位的1的个数之后,再行计算。根据与运算的性质,所有数的某位都为1时则那个位才为1,否则那位为0;同理,根据或运算的性质,所有数中,某位只要有1位为1则那个位为1,否则那位为0。
程序说明:(无)
题记:(略)
参考链接:(略)
AC的C++语言程序(解法一)如下:
/* HDU6186 CS Course */
#include <iostream>
#include <stdio.h>
using namespace std;
const int N = 1e5;
int a[N + 1], andl[N + 2], andr[N + 2], orl[N + 2], orr[N + 2], xora;
int main()
{
int n, q;
while(~scanf("%d%d", &n, &q)) {
// 为计算前缀与,前缀或,后缀与,后缀或等进行初始化
andl[0] = andr[n + 1] = 0;
andl[0] = ~andl[0];
andr[n + 1] = ~andr[n + 1];
orl[0] = orr[n + 1] = 0;
xora = 0;
// 读入数据
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
// 计算前缀与、前缀或和异或值
for(int i = 1; i <= n; i++) {
andl[i] = andl[i - 1] & a[i];
orl[i] = orl[i - 1] | a[i];
xora ^= a[i];
}
// 计算后缀与,后缀或
for(int i = n; i >= 1; i--) {
andr[i] = andr[i + 1] & a[i];
orr[i] = orr[i + 1] | a[i];
}
for(int i = 1; i <= q; i++) {
int k;
scanf("%d", &k);
printf("%d %d %d\n", andl[k - 1] & andr[k + 1], orl[k - 1] | orr[k + 1], xora ^ a[k]);
}
}
return 0;
}
AC的C++语言程序(解法二)如下:
/* HDU6186 CS Course */
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = 1e5;
const int BITS = 32;
int a[N + 1], xora, cnt1[BITS + 1], cnt[BITS + 1];
int main()
{
int n, q;
while(~scanf("%d%d", &n, &q)) {
// 为计算异或和统计的初始化
xora = 0;
memset(cnt1, 0, sizeof(cnt1));
// 读入数据, 计算异或值
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
xora ^= a[i];
}
// 统计各位1的个数
for(int i = 1; i <= n; i++) {
int t = a[i], j = 1;
while(t) {
if(t & 1)
cnt1[j]++;
t >>= 1;
j++;
}
}
for(int i = 1; i <= q; i++) {
int k;
scanf("%d", &k);
// 计算除了第k个数之外,各位的1的个数
memcpy(cnt, cnt1, sizeof(cnt));
int t = a[k], j = 1;
while(t) {
if(t & 1)
cnt[j]--;
t >>= 1;
j++;
}
// 计算与和或的结果
int anda = 0, ora = 0;
for(int i = BITS; i >= 1; i--) {
anda <<= 1;
if(cnt[i] == n - 1)
anda |= 1;
ora <<= 1;
if(cnt[i] > 0)
ora |= 1;
}
printf("%d %d %d\n", anda, ora, xora ^ a[k]);
}
}
return 0;
}
上一篇: 设计模式------Decorator
下一篇: SC命令汇总