Equations of Mathematical Magic

B. Equations of Mathematical Magic

http://codeforces.com/contest/1064/problem/B

time limit per test       1 second

memory limit per test      256 megabytes

 

 

 

Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for.

Arkadi and Boris Strugatsky. Monday starts on Saturday

Reading the book "Equations of Mathematical Magic" Roman Oira-Oira and Cristobal Junta found an interesting equation: a−(a⊕x)−x=0a−(a⊕x)−x=0 for some given aa, where ⊕⊕ stands for a bitwise exclusive or (XOR) of two integers (this operation is denoted as ^ or xor in many modern programming languages). Oira-Oira quickly found some xx, which is the solution of the equation, but Cristobal Junta decided that Oira-Oira's result is not interesting enough, so he asked his colleague how many non-negative solutions of this equation exist. This task turned out to be too difficult for Oira-Oira, so he asks you to help.

Input

Each test contains several possible values of aa and your task is to find the number of equation's solution for each of them. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of these values.

The following tt lines contain the values of parameter aa, each value is an integer from 00 to 230−1230−1 inclusive.

Output

For each value of aa print exactly one integer — the number of non-negative solutions of the equation for the given value of the parameter. Print answers in the same order as values of aa appear in the input.

One can show that the number of solutions is always finite.

Example

input

Copy

3
0
2
1073741823

output

Copy

1
2
1073741824

Note

Let's define the bitwise exclusive OR (XOR) operation. Given two integers xx and yy, consider their binary representations (possibly with leading zeroes): xk…x2x1x0xk…x2x1x0 and yk…y2y1y0yk…y2y1y0. Here, xixi is the ii-th bit of the number xx and yiyi is the ii-th bit of the number yy. Let r=x⊕yr=x⊕y be the result of the XOR operation of xx and yy. Then rr is defined as rk…r2r1r0rk…r2r1r0 where:

ri={1, if xi≠yi0, if xi=yiri={1, if xi≠yi0, if xi=yi

For the first value of the parameter, only x=0x=0 is a solution of the equation.

For the second value of the parameter, solutions are x=0x=0 and x=2x=2.

 

题目大意

  • 就是解一个方程:a−(a⊕x)−x=0  (a已知)

    解题思路

  • a−(a⊕x)−x=0 <==> a−(a^x)−x=0 <==> x^a = x-a
  • 如果运算符 ‘^’与 ‘-’起到相同的效果的话就是一个解
  • 分析四种比较;
  • 1^1=0 1-1=0
  • 1^0=1 1-0=1
  • 0^0=0 0-0=0,
  • 0^1=1 0-1=-1
  • 当a的二进制表示,如果这个位是1,则x对应的二进制对应的位数是0或者1都行;如果这个位是0,则x对应的二进制对应的位数只能是0。
  • 所以,a的二进制表示有几个1就有2的几次方的情况。

AC代码

- #include <iostream>
- #include <cstdio>
- #include <cmath>
- using namespace std;

- int T;
- int main(){
-     int a;
-     int b;
-     cin>>T;
-     while(T--){
-         int ans=1;
-         scanf("%d",&a);
-         int di=0;
-         while(a){
-             if(a&1){
-                 a/=2;
-                 di++;
-             }
-             else {
-                 a/=2;
-             }
-         }
-         ans=pow(2,di);
-         printf("%d\n",ans);
-     }
- } 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值