HDU 6186 CS Course (连续位运算)

本文介绍了一道关于位运算查询的算法题目,包括and、or、xor操作,并提供了两种解题思路及代码实现。法一通过保存所有元素的每一位bit的和来判断是否需要更改结果;法二利用位运算的特性,预处理前缀和后缀的and/or结果,快速求解除特定数外的位运算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CS Course

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 430    Accepted Submission(s): 222


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 , 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 .
 

 

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.



Then n non-negative integers  follows in a line,  for each i in range[1,n].

After that there are q positive integers in q lines,  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  in a line.
 

 

Sample Input
 
  
3 3 
1 1 1
3
 

 

Sample Output
 
  
1 1 0 
1 1 0 
1 1 0
 

 

Source
 

 

Recommend
liuyiding

 

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6186

题意:从连续的and/or/xor中去掉某一个。。

法一:保存所有元素的某一位bit的和,查找ap有没有因为自己的某一位而改变全部的or/and,如果有,就改回来

法二:三个位运算都有交换率律结合律,所以可以保存前缀连续and/or和后缀连续and/or,注except第一个或者最后一个元素的时候要特判一下。

另外,因为异或具有性质:a^b^b = a ,所以可以将直接将1~n的异或再异或要去掉的ap即可

法二代码

#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = 100000 + 100;

int n,q;
int a[maxn];
int andl[maxn], andr[maxn], orl[maxn], orr[maxn], xorl[maxn], xorr[maxn];
int main(){

    while(~scanf("%d %d", &n, &q)){
    	//memset(a,-1,sizeof(a));
    	for(int i = 1; i <= n; i++){
    		scanf("%d", &a[i]);
		}
		andl[1] = orl[1] = xorl[1] = a[1];
		andr[n] = orr[n] = a[n];
    	for(int i = 2; i <= n; i++){
    		andl[i] =andl[i-1]&a[i];
			orl[i] = orl[i-1]|a[i];
			xorl[i] = xorl[i-1]^a[i] ;
		}
		for(int i = n-1; i >= 1; i--){
			andr[i] = andr[i+1]&a[i];
			orr[i] = orr[i+1]|a[i];
		}
		for(int i = 0; i < q; i++){
			int pp;
			scanf("%d", &pp);
			if(pp==1)printf("%d %d %d\n", andr[2], orr[2], xorl[n]^a[pp]);
			else if(pp==n)printf("%d %d %d\n", andl[pp-1], orl[pp-1], xorl[n]^a[pp]);
			else printf("%d %d %d\n", andl[pp-1]&andr[pp+1], orl[pp-1]|orr[pp+1], xorl[n]^a[pp]);
		}
		
	}
    return 0;
} 


转载于:https://www.cnblogs.com/wrjlinkkkkkk/p/9552002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值