zoukankan      html  css  js  c++  java
  • HDU 6186 CS Course (连续位运算)

    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
    ", andr[2], orr[2], xorl[n]^a[pp]);
    			else if(pp==n)printf("%d %d %d
    ", andl[pp-1], orl[pp-1], xorl[n]^a[pp]);
    			else printf("%d %d %d
    ", andl[pp-1]&andr[pp+1], orl[pp-1]|orr[pp+1], xorl[n]^a[pp]);
    		}
    		
    	}
        return 0;
    } 


  • 相关阅读:
    【java开发系列】—— 集合使用方法
    【java开发系列】—— spring简单入门示例
    解决win7远程桌面连接时发生身份验证错误的方法
    eoLinker-AMS接口管理系统
    CentOS 配置mysql允许远程登录
    Linux上安装ZooKeeper并设置开机启动(CentOS7+ZooKeeper3.4.10)
    Cent OS home下中文目录改成英文目录
    解决redis-cli command not found问题
    Centos7使用yum安装Mysql5.7.19的详细步骤(可用)
    取消centOS7虚拟机锁屏
  • 原文地址:https://www.cnblogs.com/wrjlinkkkkkk/p/9552002.html
Copyright © 2011-2022 走看看