題目連接: 題目連接ios
題意:若是一個數二進制n有k位1,那麼f1[n] = k,若是k有s位二進制1,那麼f2[n] = f1[k] = s. 如此往復,直到fx[n] = 1,此時的x就是n的」K值「,如今要求[L,R]內的」K值「爲X的數有多少個。(1<=L<=R<=10^18)ide
解法:首先能夠看到10^18最多隻有61位左右的數,因此咱們只需處理1~61之間每一個數有多少個1,便可知道1~61之間每一個數」K值「是多少。spa
而後就將求[L,R]之間的個數變成求[1,R]-[1,L-1],因此咱們只需數出對於每一個數n,[1,n]之間有多少個數的」K值「爲X便可。3d
對於二進制來講,能夠這樣搜索出來:code
好比<=101001,要知足有k個1的數的個數,那麼咱們從高位往低位掃,掃到第一個1,那麼如今有兩種狀況:blog
1.此處放1:那麼就等於求<=1001時放k-1個1的數的個數遞歸
2.此處放0:那麼後面就隨便放了,爲C[5][k]get
因此如此遞歸的搜索就可得出答案,也能夠用DP作。string
代碼:it
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #define ll long long using namespace std; int Count(ll state) { int cnt = 0; while(state) { if(state & 1LL) cnt++; state >>= 1; } return cnt; } int WEI(ll state) { int cnt = 0; while(state) { cnt++; state >>= 1; } return cnt; } ll C[100][100]; int in[67]; void init() { C[0][0] = 1; for(int i = 1; i < 90; i++) { C[i][0] = 1; for(int j = 1; j <= i; j++) { C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; } } memset(in,0,sizeof(in)); in[1] = 0; for(int i=2;i<=61;i++) in[i] = in[Count(i)]+1; } int X; ll get(ll state,int cnt) { if(state < 0) return 0; int len = WEI(state); if(len < cnt) return 0; // not enough if(cnt == 0) return 1; // no demand return get(state-(1LL<<(len-1)),cnt-1) + C[len-1][cnt]; } ll getsum(ll R,ll L) { ll ans = 0; for(int i=1;i<=61;i++) if(in[i]+1 == X) ans += get(R,i)-get(L-1,i); return ans; } int main() { init(); int i,j; ll L,R; while(scanf("%lld%lld%d",&L,&R,&X)!=EOF && L+R+X) { ll ans = 0; if(X == 0 && L == 1LL) { puts("1"); continue; } if(X == 1 && L == 1LL) ans--; //1's binary code is 1, but 1 is not in (X==1) ans += getsum(R,L); cout<<ans<<endl; } return 0; }