题目描述
Given three integers A,B,C Count the number of pairs <x ,y> (with 1<=x<=A and 1<=y<=B)
such that at least one of the following is true:
- (x & y) > C
- (x ^ y) < C
("and", "xor" are bit operators)
输入描述:
The first line of the input gives the number of test cases, T(T<=100). T test cases follow. For each test case, the only line contains three integers A B and C 1 <= A, B, C <= 1e9
输出描述:
For each test case, the only line contains an integer that is the number of pairs satisfying the condition given in the problem statement.
输入
复制
3
3 4 2
4 5 2
7 8 5
输出
复制
5
7
31
数位DP解决就行
对于每一位能进行转移的有三个状态
①与等抑或等
②与小抑或等
③与等抑或大
其余状态都是可以直接求出答案或者不符合题目条件的。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<fstream>
#include<queue>
#include<bitset>
using namespace std;
const int maxn = 100005;
const int inf = 0x3f3f3f3f;
long long dp[2][2][40][3][2][2]; //前二维表示两个数的limit情况,第四维0表示该位之前与等抑或等,1表示该位之前与小抑或等,2表示该位之前与等抑或大
//第五六维表示该位前面是否都为0
long long a, b, c;
long long A[40],B[40],C[40];
long long numA[40],numB[40],power[40];
void init()
{
for(int i = 1; i <= 32; i++)
{
A[i] = (a>>(i-1))&1;
B[i] = (b>>(i-1))&1;
C[i] = (c>>(i-1))&1;
}
long long tmp1 = 0, tmp2 = 0, p = 1;
numA[0] = numB[0] = power[0] = 0;
for(int i = 1; i <= 31; i++)
{
if(A[i]) tmp1+=p;
if(B[i]) tmp2+=p;
numA[i] = tmp1;
numB[i] = tmp2;
power[i]= power[i-1]+p;
p = p<<1;
}
memset(dp,-1,sizeof(dp));
}
long long get(int pos, int la, int lb, int iszeroA, int iszeroB,int ci, int cj, int flag)
{
long long tmp1 = numA[pos-1],tmp2 = numB[pos-1];
if(la)
tmp1 = power[pos-1];
if(lb)
tmp2 = power[pos-1];
if(!iszeroA)
tmp1+=1;
if(!iszeroB)
tmp2+=1;
// cout<<"## " <<pos<<" "<<ci<<" "<<cj<<" "<<flag<<" "<< tmp1<<" "<< tmp2<<endl;
return tmp1*tmp2;
}
long long dfs(int pos, long long limitA, long long limitB, int flag,int iszeroA, int iszeroB)
{
if(pos == 0) return 0;
if(dp[limitA][limitB][pos][flag][iszeroA][iszeroB]!=-1) return dp[limitA][limitB][pos][flag][iszeroA][iszeroB];
long long maxx1 = max(limitA,A[pos]);
long long maxx2 = max(limitB,B[pos]);
int lA,lB,f,zA,zB;
long long ans = 0;
for(int i = 0; i <= maxx1; i++)
{
for(int j = 0; j <= maxx2; j++)
{
if(i==maxx1&&!limitA)
lA = 0;
else lA = 1;
if(j==maxx2&&!limitB)
lB = 0;
else lB = 1;
if(iszeroA&&i==0)
zA = 1;
else zA = 0;
if(iszeroB&&j==0)
zB = 1;
else zB = 0;
if(flag == 0)
{
if((i&j)>C[pos])
ans+=get(pos,lA,lB,zA,zB, i, j, flag);
else if((i^j)<C[pos])
ans+=get(pos,lA,lB,zA,zB, i, j, flag);
else if((i&j)<C[pos]&&(i^j)>C[pos])
continue;
else if((i&j)<C[pos])
ans+=dfs(pos-1,lA,lB,1,zA,zB);
else if((i^j)>C[pos]){
ans+=dfs(pos-1,lA,lB,2,zA,zB);
}
else
ans+=dfs(pos-1,lA,lB,0,zA,zB);
}
else if(flag == 1)
{
if((i^j)<C[pos])
ans+=get(pos,lA,lB,zA,zB, i, j, flag);
else if((i^j)==C[pos])
ans+=dfs(pos-1,lA,lB,1,zA,zB);
}
else
{
if((i&j)>C[pos])
ans+=get(pos,lA,lB,zA,zB, i, j, flag);
else if((i&j)==C[pos])
ans+=dfs(pos-1,lA,lB,2,zA,zB);
}
}
}
return dp[limitA][limitB][pos][flag][iszeroA][iszeroB] = ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%lld%lld%lld", &a, &b, &c);
init();
long long ans = dfs(32,0,0,0,1,1);
printf("%lld\n", ans);
}
return 0;
}