[ACM] Ural 1057 Amount of degrees (digital statistics)

Source: Internet
Author: User

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≤ XY≤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
input Output
15 2022
3
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            &LT;&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.