B. Equations of Mathematical Magic
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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⊕b)−x=0a−(a⊕b)−x=0的非负整数解的个数。
题目要求解a-(a\oplus x)-x=0a−(a⊕x)−x=0的解xx的个数
移项得a-x=a\oplus xa−x=a⊕x
aa的二进制形式,应该是一个0101串,异或的过程是不能影响到两个不同的位的,所以我们按位考虑
如果这一位是00,那么xx的这一位也应为00,使得异或后答案不会更大
如果这一位是11,那么xx的这一位可以为00或11,对应到减法中就是没减和减掉
所以答案就是2^{count~~1~~in~~a}2count 1 in a
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int ans=1;
scanf("%d",&n);
while(n)
{
if(n%2)
ans*=2;
n/=2;
}
printf("%d\n",ans);
}
return 0;
}