位运算

  • 位运算的性质:各位之间的运算是相互独立的
  • 因此,分别考虑每一位的方案数,显然比你的考场做法更优
点击查看代码
#include <bits/stdc++.h>
using namespace std;
long long p[20];
int main()
{
	p[0]=1;
	for(int i=1;i<=15;i++)
	{
		p[i]=p[i-1]*2;
	}
	int T;
	cin>>T;
	while(T--)
	{
		int n,m;
		cin>>n>>m;
		long long ans=0;
		for(int x=0;x<p[m];x++)
		{
			bool f=true;
			int y=0;
			for(int k=0;k<m;k++)
			{
				if(((x>>k)&1)==1&&((n>>k)&1)==0)
				{
					f=false;
					break;
				}
				else if(((x>>k)&1)==1&((n>>k)&1)==1)
				{
					y++;
				}
			}
			if(f==true)
			{
				ans=ans+p[m]*p[m]*p[y];
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
posted @ 2024-07-21 11:19  D06  阅读(3)  评论(0编辑  收藏  举报