bzoj1662: [Usaco2006 Nov]Round Numbers 圆环数

时间:2022-09-11 12:54:50

Description

正如你所知,奶牛们没有手指以至于不能玩“石头剪刀布”来任意地决定例如谁先挤奶的顺序。她们甚至也不能通过仍硬币的方式。 所以她们通过"round number"竞赛的方式。第一头牛选取一个整数,小于20亿。第二头牛也这样选取一个整数。如果这两个数都是 "round numbers",那么第一头牛获胜,否则第二头牛获胜。 如果一个正整数N的二进制表示中,0的个数大于或等于1的个数,那么N就被称为 "round number" 。例如,整数9,二进制表示是1001,1001 有两个'0'和两个'1'; 因此,9是一个round number。26 的二进制表示是 11010 ; 由于它有2个'0'和 3个'1',所以它不是round number。 很明显,奶牛们会花费很大精力去转换进制,从而确定谁是胜者。 Bessie 想要作弊,而且认为只要她能够知道在一个指定区间范围内的"round numbers"个数。 帮助她写一个程序,能够告诉她在一个闭区间中有多少Hround numbers。区间是 [start, finish],包含这两个数。 (1 <= Start < Finish <= 2,000,000,000)

Input

* Line 1: 两个用空格分开的整数,分别表示Start 和 Finish。

Output

* Line 1: Start..Finish范围内round numbers的个数

Sample Input

2 12

Sample Output

6

输出解释:

2 10 1x0 + 1x1
ROUND
3 11 0x0 + 2x1 NOT round
4 100 2x0 + 1x1 ROUND
5 101 1x0 + 2x1
NOT round
6 110 1x0 + 2x1 NOT round
7 111 0x0 + 3x1 NOT round
8 1000
3x0 + 1x1 ROUND
9 1001 2x0 + 2x1 ROUND
10 1010 2x0 + 2x1 ROUND
11 1011
1x0 + 3x1 NOT round
12 1100 2x0 + 2x1 ROUND

题解:
此题很水,按位统计一下就好了。
code:
 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
ok=;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
const int maxn=;
int l,r,c[maxn][maxn],cnt[maxn],a[maxn];
void init(){
for (int i=;i<=;i++) c[i][]=;
for (int i=;i<=;i++) for (int j=;j<=i;j++) c[i][j]=c[i-][j-]+c[i-][j];
cnt[]=;
for (int i=;i<=;i++) for (int j=i-,k=;j>=k;j--,k++) cnt[i]+=c[i-][j];
for (int i=;i<=;i++) cnt[i]+=cnt[i-];
}
int calc(int n){
int t=n,len=,ans=,tmp0=,tmp1=;
memset(a,,sizeof(a));
while (t) a[++len]=t&,t>>=;
for (int i=len;i;i--){
if (i==len) ans+=cnt[i-];
else if (a[i]){for (int j=i-;j+tmp0+>=tmp1+i--j;j--) ans+=c[i-][j];}
if (a[i]) tmp1++; else tmp0++;
}
return ans;
}
int main(){
init();
read(l),read(r);
printf("%d\n",calc(r+)-calc(l));
return ;
}