zoukankan      html  css  js  c++  java
  • TOJ 3176 Challenge from XOR

    Description

    Mr. AngelClover just learnt XOR on his Computer Class. XOR is a bit arithmetic operator that returns one value if one, but not both, of its operands is one. It is also called Exclusive-OR. Now Mr. AngelClover wonders if it is possible to pick up some numbers from a certain number set, such that the result of Exclusive-OR is zero. And he wants the numbers to be as few as possible but there should be at least one.
    Can you help him?

    Input

    The first line is a positive integer N, which means the number of the number set. And there are N lines followed, in each there is a single positive integer, which is the number in the set. Each number in the set is an integer between 0 and 2^16 inclusive. 0< N <=30. (Please use standard input, and don’t read or write files.)

    Output

    There is only one line in the output, which is the smallest number as described above. If Mr. AngelClover can NOT find such numbers after XOR is zero, output -1. (Please use standard output, and don’t read or write files.)

    Sample Input

    4
    1
    2
    3
    4
    

    Sample Output

    3

    Hint

    Numbers which are picked up are 1, 2, 3.

    For 40% input data cases, N <= 15.

    Source

    NKPC5

    题目意思就是选几个数进行异或运算结果为0的最小个数 。一开始用觉得数据不大,用深搜果断超时。

    后来想想觉得应该让个数最小深搜会产生大量没用的结果而且要全部遍历,改用广搜。

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <queue>
     4 using namespace std;
     5 
     6 int n;
     7 int a[40];
     8 int ans;
     9 
    10 struct Node{
    11     int val;
    12     int step;
    13     int bs;
    14 };
    15 
    16 int bfs(){
    17     queue<Node> Q;
    18     Node n1;
    19     n1.val=0;
    20     n1.step=0;
    21     n1.bs=0;
    22     Q.push(n1);
    23     while(!Q.empty()){        
    24         Node now=Q.front();
    25         Q.pop();
    26         for(int i=now.bs; i<n; i++){
    27             int  v=a[i]^now.val;
    28             if(v==0)return now.step+1;
    29             else{
    30                 Node n2;
    31                 n2.val=v;
    32                 n2.step=now.step+1;
    33                 n2.bs=i+1;
    34                 Q.push(n2);
    35             }
    36         }
    37     }
    38     return -1;
    39 }
    40 
    41 int main(int argc, char *argv[])
    42 {
    43     while( scanf("%d",&n)!=EOF ){
    44         for(int i=0; i<n; i++){
    45             scanf("%d",&a[i]);
    46         }
    47         ans=bfs();
    48         printf("%d
    ",ans);
    49     }
    50     return 0;
    51 }
  • 相关阅读:
    Ubuntu 16 安装redis客户端
    crontab 参数详解
    PHP模拟登录发送闪存
    Nginx配置端口访问的网站
    Linux 增加对外开放的端口
    Linux 实用指令之查看端口开启情况
    无敌的极路由
    不同的域名可以指向同一个项目
    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
    Redis 创建多个端口
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3567915.html
Copyright © 2011-2022 走看看