终于一遍就写对了. 第一次没有注意读题导致了一个没有注意到什时候要开始统计.
Code
#include <iostream>
#include <string>
#define int long long
using namespace std;
#define F(i, a, b) for(int i=(a); i<=(b);i++)
#define Fd(i, a, b) for(int i=(a);i>=(b);i--)
int f[60][60][60][3][3];
int num[60];
int dp(int x, int zeros, int ones, int zero, int lim){
#define rem f[x][zeros][ones][zero][lim]
if(x==0) {
if(zeros >= ones){
return 1;
}else{
return 0;
}
}
if( rem != -1 ) return rem;
int sum = 0; int up = lim ? num[x]:1;
F(i, 0, up){
if(i==0){
if(zero ==0 )
sum += dp(x-1, zeros + 1, ones, (zero && (i==0)), (lim && (i==up)));
else sum += dp(x-1, zeros, ones, (zero && (i==0)), (lim && (i==up)));
}
else if(i==1) sum += dp(x-1, zeros, ones + 1, (zero && (i==0)), (lim && (i==up)));
}
rem = sum;
return sum;
}
int solve(int x){
memset(f,-1,sizeof f);
int pos=0;
while(x){
num[++pos]=x%2;
x/=2;
}
return dp(pos,0,0,1,1);
}
signed main(){
int n, m; cin>>n>>m;
cout<<solve(m)-solve(n-1)<<endl;
}
标签:int,lim,sum,zero,ones,zeros,Numbers,POJ3252,Round
From: https://www.cnblogs.com/augpath/p/16925892.html