1057. Amount of Degreestime limit:1.0 second
Memory limit:64 MB
Create a code to determine the amount of integers, lying in the set [
X;
Y] and being a sum of exactly
KDifferent integer degrees of
B.
Example.Let
X=15,
Y=20,
K=2,
B=2. By this example 3 numbers is the sum of exactly of the number of degrees of number 2:17 = 24+20,
18 = 24+21,
20 = 24+22. Inputthe first line of input contains integers
Xand
Y, separated with a space (1≤
X≤
Y≤2 to? 1). The next lines contain integers
Kand
B(1≤
K≤20; 2≤
B≤10). Outputoutput should contain a single integer-the amount of integers, lying between
Xand
Y, being a sum of exactly
KDifferent integer degrees of
B. Sample
problem Source:The first question of Rybinsk State Avia Academy Digital Statistics. Liu Cong's paper: a brief discussion on the http://wenku.baidu.com/view/d2414ffe04a1b0717fd5dda8.html of statistics in digital category
Finish the problem. Think the statistics are really very strange. There is no need to enumerate a number of numbers to infer that when inferring a number, the smaller number is inferred, the efficiency is high. There are also combinations of applications. When the high position is determined, the next few will be able to take whatever you want.
In the title, when B is not binary:
Why did you do it? A number can be converted into the form given in the test instructions, its B-number in the coefficients should not be 0, that is 1, assuming that the coefficient >1 from left to right, then this number is certainly not in line with test instructions, it should be made into 1, which makes the original number smaller, in order not to miss some of the number, All the bits behind the changed bits should be changed to 1, so that the largest and not more than N of the B-binary consists of only 0 and 1. This will be done in accordance with the binary system.
Code:
#include <iostream> #include <string.h>using namespace Std;int x,y,k,b;int c[40][40];void init ()//combination number {C[0] [0]=1; for (int i=1;i<=31;i++) {c[i][0]=c[i-1][0]; for (int j=1;j<=i;++j) c[i][j]=c[i-1][j]+c[i-1][j-1]; }}int change (int n) {int b[40]; int len=0; while (n) {b[len++]=n%b; N/=b; } int ans=0; for (int i=len-1;i>=0;i--) {if (b[i]>1) {for (int j=i;j>=0;j--) ans+= (1 <<J); Break } else ans+= (b[i]<<i); } return ans; int cal (int x,int k) {int tot=0,ans=0; for (int i=31;i>0;i--) {if (x& (1<<i))//I-bit is 1 (starting from 0), then I am left with a number, followed by the first number is 0, from the i-1 of the number of randomly pick K-tot {++tot; if (tot>k) break; x=x^ (1<<i);//1 changes to 0} if ((1<< (i-1)) <=x) Ans+=c[i-1][k-tot]; } if (TOT+X==K)//Consider x this number itself ++ans; return ans;} int main () {init (); while (cin>>x>>y>>k>>b) cout<<cal (Change (Y), K)-cal (change (X-1), K) <<endl; return 0;}
[ACM] Ural 1057 Amount of degrees (digital statistics)