Good Bye 2015B(模拟或者二进制枚举)

B. New Year and Old Property
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The year 2015 is almost over.

Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn't care about the number of zeros in the decimal representation.

Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?

Assume that all positive integers are always written without leading zeros.

Input

The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak's interval respectively.

Output

Print one integer – the number of years Limak will count in his chosen interval.

Examples
Input
5 10
Output
2
Input
2015 2015
Output
1
Input
100 105
Output
0
Input
72057594000000000 72057595000000000
Output
26
Note

In the first sample Limak's interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property.

题意:

给出区间(a, b)求区间中转化为二进制后恰好有一个0的数的个数;

方法1:(模拟)这是我最先想到的方法,然而代码比较复杂,还错了好多发,果然我还是一个小渣渣;

代码:

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define MAXN 100
 4 using namespace std;
 5 
 6 int get_erjinzhi(ll n, int a[])  //*****将n转化为二进制存储到a数组中
 7 {
 8     int k=0, xx[MAXN];
 9     while(n)
10     {
11         xx[k++]=n%2;
12         n/=2;
13     }
14     for(int i=k-1, j=0; i>=0; i--, j++)
15     a[j]=xx[i];
16     return k;
17 }
18 
19 int cmp(int a[], int yy[], int cnt) //*****比较a,yy的大小
20 {
21     for(int i=0; i<cnt; i++)
22     {
23         if(a[i]>yy[i]) return 1;
24         if(a[i]<yy[i]) return 0;
25     }
26     return 2;
27 }
28 
29 int main(void)
30 {
31     ll x, y, ans=0;
32     int a[MAXN], b[MAXN], aa[MAXN], bb[MAXN];
33     cin >> x >> y;
34     if(x==1&&y==1)
35     {
36         cout << "0" << endl;
37         return 0;
38     }
39     if(x==1) x++;
40     int cnt1=get_erjinzhi(x, a);
41     int cnt2=get_erjinzhi(y, b);
42     for(int i=0; i<cnt1; i++)
43     {
44         aa[i]=1;
45     }
46     aa[1]=0;
47     if(cmp(aa, a, cnt1))
48     {
49         if(cnt1==cnt2)
50         {
51             if(cmp(b, aa, cnt1))
52             ans++;
53         }
54         else ans++;
55     }
56     for(int i=1; i+1<cnt1; i++)
57     {
58         swap(aa[i], aa[i+1]);
59         if(cmp(aa, a, cnt1))
60         {
61             if(cnt1==cnt2)
62             {
63                 if(cmp(b, aa, cnt1)) ans++;
64             }
65             else  ans++;
66         }
67     }
68     for(int i=0; i<cnt2; i++)
69     {
70         bb[i]=1;
71     }
72     bb[1]=0;
73     int k=1;
74     if(cnt1!=cnt2)
75     {
76         while(cmp(b, bb, cnt2)&&k<cnt2)
77         {
78             ans++;
79             swap(bb[k], bb[k+1]);
80             if(cmp(b, bb, cnt2)==2)
81             {
82                 ans++;
83                 break;
84             }
85             k++;
86         }
87     }
88     for(int i=cnt1+1; i<cnt2; i++)
89     {
90         ans+=i-1;
91     }
92     cout << ans << endl;
93     return 0;
94 }



方法2:(二进制枚举,看了别人代码才想到的,再一次证明了我是个渣渣)

思路:

二进制运算的姿势一定要摆对,不然得做好久…
举个例子:
10000-1=1111
1111-1=1110
1111-10=1101
1111-100=1011

代码:

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 int main(void)
 6 {
 7     ll a, b, ans=0;
 8     cin >> a >> b;
 9     for(int i=2; i<=60; i++)
10     {
11         for(int j=0; j<i-1; j++)
12         {
13             ll temp=(1ll<<i)-1-(1ll<<j);
14             if(temp>=a && temp<=b) ans++;
15         }
16     }
17     cout << ans << endl;
18     return 0;
19 }



方法3:(dfs)

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 
 5 ll ans=0, a, b;
 6 
 7 void dfs(ll x, ll flag)
 8 {
 9     if(x>b) return;
10     if(x>=a&&x<=b&&flag) ans++;
11     if(flag==0) dfs(x*2, 1);
12     dfs(x*2+1, flag);
13 }
14 
15 int main(void)
16 {
17     cin >> a >> b;
18     dfs(1, 0);
19     cout << ans << endl;
20     return 0;
21 }



原文地址:https://www.cnblogs.com/geloutingyu/p/5845348.html