ural1057 Amount of degrees 位数统计
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 int f[33][33]; 6 void init(){ 7 f[0][0] =1; 8 for(int i=1;i<=31;i++){ 9 f[i][0] = f[i-1][0]; 10 for(int j=0;j<=i;j++) 11 f[i][j] = f[i-1][j]+f[i-1][j-1]; 12 } 13 } 14 15 int change(int x,int b){ 16 string s; 17 while(x){ 18 s = (char)(x%b+'0')+s; 19 x = x/b; 20 } 21 for(int i=0;i<s.size();i++) 22 if(s[i]>'1'){ 23 for(int j=i;j<s.size();j++) 24 s[j] = '1'; 25 break; 26 } 27 x =0; 28 for(int i=0;i<s.size();i++){ 29 x = x|((s[s.size()-1-i]-'0')<<i); 30 } 31 return x; 32 } 33 34 int cal(int x,int k){ 35 int tot =0,ans =0; 36 for(int i=31;i>0;i--){ 37 if(x&(1<<i)){ 38 tot++; 39 if(tot>k) break; 40 x = x^(1<<i); 41 } 42 if((1<<(i-1))<=x){ 43 ans += f[i-1][k-tot]; 44 } 45 } 46 if(tot + x==k) ans++; 47 return ans; 48 } 49 50 int main() 51 { 52 init(); 53 int x,y; 54 int k,b; 55 cin>>x>>y>>k>>b; 56 x= change(x,b); 57 y = change(y,b); 58 int resx = cal(x-1,k); 59 int resy = cal(y,k); 60 cout<<resy-resx<<endl; 61 return 0; 62 }