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的代码

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月30日 星期三 22时49分02秒
 4File Name :code/cf/goodbye2015/B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const int N=1E4+7;
30LL a,b;
31LL p[N];
32LL c[N];
33LL cal( LL x)
34{
35    return ((x-1LL)*x)/2LL;
36}
37LL solve (LL x)
38{
39    if (x==0LL) return 0;
40    LL res= 0LL;
41    LL cnt = 0LL;
42    LL xx = x;
43    while (xx)
44    {
45	cnt++;
46	p[cnt] = xx%2LL;
47	xx/=2LL;
48    }
49    ms(c,0);
50    res+=cal(cnt-1LL);
51    LL tmp = (1LL<<cnt)-1LL;
52    for ( LL i = 0 ; i <cnt-1 ; i++)
53    {
54	LL happ = 1LL<<i;
55	c[i]=tmp-happ;
56    }
57
58    sort(c,c+cnt-1);
59
60    for ( LL i = 0  ; i< cnt -1 ; i++)
61    {
62	if (x>=c[i]) res++;
63    }
64
65
66    return res;
67}
68int main()
69{
70
71	cin>>a>>b;
72	LL ans = solve(b)-solve(a-1LL);
73	cout<<ans<<endl;
74
75  #ifndef ONLINE_JUDGE  
76  fclose(stdin);
77  #endif
78    return 0;
79}

数位dp的方法:

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年03月18日 星期五 16时33分04秒
 4File Name :code/cf/problem/611B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL l,r;
34int digit[80];
35LL dp[80][80];
36
37
38LL dfs( int pos,int cnt,bool limit,bool prehasnonzero)
39{
40    if (pos==0) return cnt==1;
41    if (!limit&&prehasnonzero&&dp[pos][cnt]!=-1) return dp[pos][cnt];
42
43    int mx = limit?digit[pos]:1;
44    LL res = 0LL ;
45    if (prehasnonzero)
46    {
47	for ( int i =  0 ; i <= mx ; i++)
48	{
49	    res+=dfs(pos-1,i==0?cnt+1:cnt,limit&&i==mx,true);
50	}
51    }
52    else
53    {
54	for ( int i = 0 ; i <= mx; i++)
55	{
56	    res+=dfs(pos-1,0,limit&&i==mx,i==0?false:true);
57	}
58    }
59
60    if (!limit&&prehasnonzero) dp[pos][cnt] = res;
61    return res;
62}
63LL solve  ( LL n)
64{
65    int len =  0 ;
66    ms(digit,0);
67
68    while (n)
69    {
70	digit[++len] = n % 2;
71	n /= 2;
72    }
73    return dfs(len,0,true,false);
74}
75int main()
76{
77	#ifndef  ONLINE_JUDGE 
78	freopen("code/in.txt","r",stdin);
79  #endif
80	ms(dp,-1);
81	cin>>l>>r;
82	//cout<<"solve:"<<solve()<<endl;
83	LL ans = solve(r) - solve (l-1);
84	cout<<ans<<endl;
85
86  #ifndef ONLINE_JUDGE  
87  fclose(stdin);
88  #endif
89    return 0;
90}