good bye 2015 B

题意:统计在n,m之间的数的二进制表示形式只有一个零的数目。

位运算模拟+dfs

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cstdio>
 6 #include<set>
 7 #include<map>
 8 #include<vector>
 9 #include<cstring>
10 #include<stack>
11 #include<cmath>
12 #include<queue>
13 #include <bits/stdc++.h>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 #define ll long long
17 #define clc(a,b) memset(a,b,sizeof(a))
18 const int maxn=1000000;
19 
20 ll n,m;
21 ll ans;
22 void dfs(ll a,ll b)
23 {
24     if(a>m)
25         return ;
26     if(a>=n&&a<=m&&b==1)
27         ans++;
28     if(b==0)
29         dfs(a*2,1);
30     dfs(a*2+1,b);
31 }
32 
33 int main()
34 {
35     while(~scanf("%I64d%I64d",&n,&m))
36     {
37         ans=0;
38         dfs(1,0);
39         printf("%I64d
",ans);
40     }
41     return 0;
42 }
View Code
原文地址:https://www.cnblogs.com/ITUPC/p/5154188.html