Even Numbers(找规律)(组合数杨辉三角)(二进制)

Yousef loves playing with functions in his free time. Today, he invents the following function:
在这里插入图片描述
Yousef will give you a list of queries, and you need to find the answers for them. For each query, you are given an integer n, and your task is to count the number of integers m in which (0 ≤ m ≤ n) and calc(n,  m) is an even number. Can you?
Input

The first line contains an integer T (1 ≤ T ≤ 10^5) specifying the number of test cases.

Each test case consists of a single line containing an integer n (0 ≤ n ≤ 10^18), as described in the problem statement above.
Output

For each test case, print a single line containing the number of integers m in which (0 ≤ m ≤ n) and calc(n,  m) is an even number.
Example
Input

2
1
2

Output

0
1

分析一下这个函数,发现其实就是组合数的递推公式
问有多少个m(0<=m<=n)使得calc(n,m)是偶数,其实也就是杨辉三角第(n+1)行中偶数的个数
打个表找规律发现偶数规律好难找(╥╯^╰╥),那就换个角度找奇数的规律
n   :    偶数个数  奇数个数[在这里插入图片描述](https://img-blog.csdnimg.cn/20181216142053834.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyOTM2NTE3,size_16,color_FFFFFF,t_70)
在这里插入图片描述
2是1的2倍
2,4是1,2的2倍
2,4,4,8是1,2,2,4的2倍
……
a[0]=1
a[n]=a[n-x]*2 (x是小于等于n的最大的那个2的次幂,也就是2进制中最高位上那个1对应的数)
即2进制中有几个1,奇数个数就是2的几次方
最后用n+1-奇数个数就是偶数个数了(杨辉三角中n对应第n+1行,有n+1个数)

#include<cstdio>
using namespace std;
int main(){
	int T;
	long long n;
	scanf("%d",&T);
	while(T--){
		scanf("%I64d",&n);
		long long m=n;
		int cnt=0;//2进制中1的个数 
//		while(m)        //这种也行
//		{
//			if(m&1) cnt++;
//			m>>=1; 
//		}
		while(m)        //如果要记忆化搜索的话只能用这种
		{   
			cnt++;
			m-=m&(-m);
		}
		long long ans=1ll<<cnt;//奇数的个数 (long long 1) 
		printf("%I64d\n",n+1-ans);//偶数的个数 
	}
	return 0; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值