cf 611 B ||codeforces goodbye 2015 B. New Year and Old Property (数学或者数位dp)
http://codeforces.com/contest/611/problem/B
题意:问a到b(1E18),二进制表示中只有一个0的数有多少个。
思路:这么大的数。。。不是有循环节就是math problems. UD:20160318讲道理还有可能是数位dp好不好。。。
我们发现可以很容易得算出1到x的二进制表示中只有一个0 的数有多少个。
problem solved.
20160318update:学了数位dp后又看到这题。。。这题显然是数位dp啊。。。亏我找规律搞了出来2333.
后面附上数位dp方法AC的代码
/* ***********************************************
Author :111qqz
Created Time :2015年12月30日 星期三 22时49分02秒
File Name :code/cf/goodbye2015/B.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const int N=1E4+7;
3LL a,b;
4LL p[N];
5LL c[N];
6LL cal( LL x)
7{
8 return ((x-1LL)*x)/2LL;
9}
10LL solve (LL x)
11{
12 if (x==0LL) return 0;
13 LL res= 0LL;
14 LL cnt = 0LL;
15 LL xx = x;
16 while (xx)
17 {
18 cnt++;
19 p[cnt] = xx%2LL;
20 xx/=2LL;
21 }
22 ms(c,0);
23 res+=cal(cnt-1LL);
24 LL tmp = (1LL<<cnt)-1LL;
25 for ( LL i = 0 ; i <cnt-1 ; i++)
26 {
27 LL happ = 1LL<<i;
28 c[i]=tmp-happ;
29 }
sort(c,c+cnt-1);
1 for ( LL i = 0 ; i< cnt -1 ; i++)
2 {
3 if (x>=c[i]) res++;
4 }
1 return res;
2}
3int main()
4{
1 cin>>a>>b;
2 LL ans = solve(b)-solve(a-1LL);
3 cout<<ans<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}
数位dp的方法:
/* ***********************************************
Author :111qqz
Created Time :2016年03月18日 星期五 16时33分04秒
File Name :code/cf/problem/611B.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6LL l,r;
7int digit[80];
8LL dp[80][80];
1LL dfs( int pos,int cnt,bool limit,bool prehasnonzero)
2{
3 if (pos==0) return cnt==1;
4 if (!limit&&prehasnonzero&&dp[pos][cnt]!=-1) return dp[pos][cnt];
1 int mx = limit?digit[pos]:1;
2 LL res = 0LL ;
3 if (prehasnonzero)
4 {
5 for ( int i = 0 ; i <= mx ; i++)
6 {
7 res+=dfs(pos-1,i==0?cnt+1:cnt,limit&&i==mx,true);
8 }
9 }
10 else
11 {
12 for ( int i = 0 ; i <= mx; i++)
13 {
14 res+=dfs(pos-1,0,limit&&i==mx,i==0?false:true);
15 }
16 }
1 if (!limit&&prehasnonzero) dp[pos][cnt] = res;
2 return res;
3}
4LL solve ( LL n)
5{
6 int len = 0 ;
7 ms(digit,0);
1 while (n)
2 {
3 digit[++len] = n % 2;
4 n /= 2;
5 }
6 return dfs(len,0,true,false);
7}
8int main()
9{
10 #ifndef ONLINE_JUDGE
11 freopen("code/in.txt","r",stdin);
12 #endif
13 ms(dp,-1);
14 cin>>l>>r;
15 //cout<<"solve:"<<solve()<<endl;
16 LL ans = solve(r) - solve (l-1);
17 cout<<ans<<endl;
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}