Codeforces 1064B.Equations of Mathematical Magic(规律+幂运算) 快速幂

在这里插入图片描述
第一行有一个整数t,代表接下来会有几个a

接下来t行,每行一个非负整数a(a的值不超过230-1),你需要计算每一个a能解出多少个非负整数x

a二进制下所包含1的个数cnt
2cnt即为结果 可以快速幂 O(logn) 速度快
&二进制下末尾1的个数 or 判断奇偶性

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cctype>
using namespace std;

#define PI acos(-1.0)
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int INF32M=0x3f3f3f3f;
const ll INF64M=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+5;

//特点 快 求可爆int下a^b 
ll quickpow(ll x,ll y)	//2^29 a二进制下最多29个1 不爆int ll具有普遍性

{
	ll ans=1;
	while(y)	//y=11=1011 x^11
	{
		if(y&1)
			ans=ans*x;
		x=x*x;	//x^1*x^2*x^8 a^b b个a相乘 O(b)=O(n) x^11 11次 
		y>>=1;	//快速幂 3次 log11=3 
	}
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	int T,t,n;
	cin>>T;
	while(T--)
	{
		n=0;	//每一组数据 2进制下1的个数清0 
		cin>>t;
		while(t!=0)	//t二进制下1的个数 
		{
			if(t&1)
			{
				n++;
			}
			t>>=1;
		}
		cout<<quickpow(2,n)<<endl; 	//cnt最大为29 int int 返回ll 
		//2^30-1 1后30个0 29个1
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值