问题描述:
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-negativesolutions 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

3
0
2
1073741823

Output

1
2
1073741824

题解:

首先

题目可以简单地描述为:
a - x = a^x的解的个数 其中 ^ 为位运算符 异或
a为输入 x为所需的解

在草稿上列列看 能否找到内在联系
**

**

首先取a = 10 然后以 x = 1 代入观察对比 a - x = a^x 左右边变化情况

**

**
为了方便描述,我们规定: 二进制位中 对应位表示的大小即为 该位的值*2位数-1
10的二进制 1010
1的二进制 0001
考虑左边减法运算结果: 10减去 1 二进制位等于1的位(该例为低阶位)表示的大小
再去其他数字例如5 : 0101
同样 结果为:10减去 5二进制位等于1的位(在此例为 第 1、3位)表示的大小
重点考虑右边:1010
0001
结果: 1011 根据异或运算规律
1.上0 下1 => 1 局部结果为加上对应位表示的大小
2.上1 下1 => 0 局部结果为减去对应位表示的大小
3.上0 下0 =>0 局部不变
4.上1 下0 =>1 局部不变

发现 2. 的情况与左边运算结果一致!
下0的情况 局部结果保持不变
上0的情况 必须下0才能保持不变
因此我们得到一个结论:
所有(记住是所有)对应位 a为1 b也为1 时 a^b = a - b

于是乎 我们要 a - x = a^x 成立
只需要满足 x的对应位为1 而a的对应位也为1
x的对应位为0 则a对应位任取1或0

例如 a: 1010
x1: 1000
x2:0010
x3:1010
x4:0000 都满足以上条件
进一步体会 也就是说 a为1的位 x可任取0或1
a为0的位 x绝对不可取1

那么问题就转化为了一个很简单的排列组合问题
假设a的1的位共有 m个 求x的二进制组合个数
那么显然 x的组合个数有 2m

经过一番推导 原来繁琐的题目 代码只要堪堪数行…
这就是数学和推理的重要性了

#include<bits/stdC++.h>
using namespace std;int main()
{int t = 0,cnt = 0;     //cnt表示a中为1的二进制位的个数long long a = 0;cin >> t;while(t--){cin >> a;for(cnt = 0;a;a >>= 1)  {if(a&1) cnt++;}cout << (long long )pow(2,cnt) << endl;}return 0;
}

萌新不容易
如有错误请多多包涵 多多指教

补充:
判断a的二进制数中 位等于1的个数可以直接
__builtin_popcount(a) // a为32位整数
__builtin_popcountll(a) // a为long long 64位
同理
__builtin_clz(a)
__builtin_clzll(a)

Equations of Mathematical Magic题解相关推荐

  1. 【CF_516_div2_B】Equations of Mathematical Magic

    链接 (减法.位数均为二进制下) 简化: 给出a , 对于方程   问有多少非负解. 如果这是一道noip题,数据肯定有很大一部分是模拟分,所以暴力枚举拿到一半以上应该不成问题. 于是我试了一下,发现 ...

  2. NOIP2016 普及组第四题 魔法阵magic 题解

    题目描述 六十年一次的魔法战争就要开始了,大魔法师准备从附近的魔法场中汲取魔法能量. 大魔法师有m个魔法物品,编号分别为1,2,-,m.每个物品具有一个魔法值,我们用Xi表示编号为i的物品的魔法值.每 ...

  3. UVA10978 Let's Play Magic! 题解

    原文链接:http://www.algorithmist.com/index.php/User:Sweepline/UVa_10978.cpp AC的C语言程序: #include <stdio ...

  4. Codeforces Round #516 (Div. 2Div.1)

    Codeforces Round #516 (Div. 2&&Div.1) (Div.1) 题号 题目 知识点 A Make a triangle! 贪心题 B Equations o ...

  5. [CF]Round 516

    A Make a triangle! 题意:给定三根线段,问最少要延长多少才能拼成一个三角形. 数学题. B Equations of Mathematical Magic 题意:求$a - (a \ ...

  6. NOIP模拟测试30「return·one·magic」

    magic 题解 首先原式指数肯定会爆$long$ $long$ 首先根据欧拉定理我们可以将原式换成$N^{\sum\limits_{i=1}^{i<=N} [gcd(i,N)==1] C_{G ...

  7. NOIP2016 普及组 总结+题目吐槽+代码+简单题解

    提高组回来之后,像往年一样,做了一下普及组的题 先吐槽一下 T1 这题一眼看上去以为可以用不同种类的铅笔,没想到只能用一种种类,我240B搞定 #include<cstdio> int m ...

  8. javascript指南_熟练掌握JavaScript的指南

    javascript指南 So you're trying to learn JavaScript but are inundated with all the different syntax an ...

  9. 数学建模美国赛论文常用句式总结

    数学建模美国赛论文常用句式总结 The expression of - can be expanded as: - -的表达式可扩展为- A is exponentially smaller than ...

  10. 哥德尔预言无穷小微积分是未来的数学分析

    哥德尔预言无穷小微积分是未来的数学分析 二十世纪世界伟大的数学家哥德尔预言非标准分析是未来的数学分析. 哥德尔1974年预言的原文如下: "There are good reasons to ...

最新文章

  1. 如何安装mysql5.7.9_安装mysql-5.7.9-winx64
  2. 24行代码-Leecode 2063. 所有子字符串中的元音——Leecode周赛系列
  3. 开源网站云查杀方案,搭建自己的云杀毒。
  4. 产品经理思维模型:新的增长黑客模型RARRA
  5. mysql 导出dmp文件_一文带你了解MySQL主从复制(Master-Slave)
  6. OpenShift4 - 使用 Service CA 证书增加内部通讯安全
  7. 机器学习基础(四十一)—— KNN
  8. 总结一下linux中的分段机制
  9. BP神经网络需要训练的参数,BP神经网络图像识别
  10. lpddr3 阻抗_LPDDRx的总结
  11. vs项目筛选器显示错乱、只显示部分文件
  12. drcom for linux,Ubuntu Linux 6.10下用Dr.COM(drcom-client)接入网络的问题
  13. Python 三种读文件方法read(), readline(), readlines()及去掉换行符\n
  14. Easter Eggs
  15. CTF密码学总结(一)
  16. iOS 偏好设置 NSUserDefault
  17. 用adobe acrobat修改PDF文档中的文字
  18. 【基础练习】【SPFA】codevs1557 热浪题解
  19. 「产品战略管理 」产品策略工具 - BCG增长 - 共享矩阵
  20. 阿米巴管理模式适合什么类型的企业

热门文章

  1. 不积跬步无以至千里 不积小流无以成江海
  2. 我国研发出勒索病毒防御软件:能阻止其破坏文件
  3. 计算机忽然打开东西特别慢,电脑突然打开网页很慢
  4. java zhs16gbk_oracle 修改字符集 为ZHS16GBK
  5. 纯前端滑块拼图验证组件(多端兼容)
  6. 2018年最新old男孩python全栈第九期课程-大牛编程吧-Python编程精品区-大牛编程吧
  7. 基于U-Net模型的视网膜血管分割
  8. DR选举和BDR选举
  9. 心形一行python_《心》字意思读音、组词解释及笔画数 - 新华字典 - 911查询
  10. F - Endless Walk