popcount相关性质+从低往高的数位dp:CF1734F

https://www.luogu.com.cn/problem/CF1734F

popcount有个性质:popcount(x)^popcount(y)=popcount(x^y)

考虑数位dp,发现很难

然后我们发现可以从低往高dp(当做套路)

只不过是否达到上界变成是否超出去

popcount相关性质+从低往高的数位dp:CF1734F_第1张图片

#include
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//mt19937 rand(time(0));
//mt19937_64 rand(time(0));
//srand(time(0));
//#define N
//#define M
//#define mo
int n, m, i, j, k, T;
int dp[100][2][2][2]; 
int a[100], b[100], x, y, z, u, v, w; 

void calc(int x, int *a) {
	int k=-1; 
	while(x) a[++k]=x%2, x/=2; 
}

signed main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	T=read();
	while(T--) {
		memset(dp, 0, sizeof(dp)); 
		memset(a, 0, sizeof(a)); 
		memset(b, 0, sizeof(b)); 
		n=read(); m=read()-1; 
		calc(n, a); calc(m, b); 
//		for(i=0; i<100; ++i) printf("%d", a[i]); printf("\n"); 
//		for(i=0; i<100; ++i) printf("%d", b[i]); printf("\n"); 
		dp[0][0][0][0]=1; 
		for(i=0; i<=62; ++i) 
			for(x=0; x<=1; ++x)  //此位之前popcount 奇偶性 
				for(y=0; y<=1; ++y) // 进位
					for(z=0; z<=1; ++z)  {
//						printf("dp[%d][%d][%d][%d]=%lld\n", i, x, y, z, dp[i][x][y][z]); 
						for(u=0; u<=1; ++u) {
							v=(u+a[i]+y)%2; w=(u+a[i]+y)/2; 
							v=(u^v^x); 
//							if(u>b[i] && z) continue; 
							if(u<b[i]) k=0; 
							if(u==b[i]) k=z; 
							if(u>b[i]) k=1; 
							dp[i+1][v][w][k]+=dp[i][x][y][z]; 
//							printf("> dp[%d][%d][%d][%d]=%lld %lld %lld\n", i+1, v, w, k, dp[i+1][v][w][k], u, a[i]); 

						}
					}
		// dp[63][1][0][0]
		printf("%lld\n", dp[63][1][0][0]); 
			
	}

	return 0;
}


你可能感兴趣的:(二进制,数位dp)