# 数位DP

Orz cxlove

 1 /**************************************************************
2     Problem: 1662
3     User: Tunix
4     Language: C++
5     Result: Accepted
6     Time:0 ms
7     Memory:1280 kb
8 ****************************************************************/
9
10 //POJ 3252 && BZOJ 1662
11 #include<cmath>
12 #include<vector>
13 #include<cstdio>
14 #include<cstring>
15 #include<cstdlib>
16 #include<iostream>
17 #include<algorithm>
18 #define rep(i,n) for(int i=0;i<n;++i)
19 #define F(i,j,n) for(int i=j;i<=n;++i)
20 #define D(i,j,n) for(int i=j;i>=n;--i)
21 #define pb push_back
22 using namespace std;
23 int getint(){
24     int v=0,sign=1; char ch=getchar();
25     while(!isdigit(ch)) {if(ch=='-') sign=-1; ch=getchar();}
26     while(isdigit(ch))  {v=v*10+ch-'0'; ch=getchar();}
27     return v*sign;
28 }
29 const int N=1e7+10,INF=~0u>>2;
30 const double eps=1e-8;
31 /*******************template********************/
32 int c[40][40];
33 void init(){
34     c[0][0]=1;
35     F(i,1,32){
36         c[i][0]=c[i-1][0];
37         F(j,1,i) c[i][j]=c[i-1][j]+c[i-1][j-1];
38     }
39 }
40 int solve(int n){
41     int len=0,bit[40],cnt=0;
42     for(;n;n>>=1) bit[++len]=n&1;
43     int ans=0;
44     F(i,1,len-1)
45         D(j,i-1,(i+1)>>1)
46             ans+=c[i-1][j];
47     int one=1,zero=0;
48     D(i,len-1,1)
49         if (bit[i]){
50             zero++;
51             F(j,max(0,(len+1)/2-zero),i-1)
52                 ans+=c[i-1][j];
53             zero--;
54             one++;
55         }
56         else zero++;
57 //  ans+=c[cnt-1][(len+1)>>1];
58     return ans;
59 }
60 int main(){
61     init();
62     int l=getint(),r=getint();
63     printf("%d\n",solve(r+1)-solve(l));
64     return 0;
65 }
View Code

posted @ 2015-03-01 18:45  Tunix  阅读(171)  评论(0编辑  收藏  举报