Timus Online Judge 1057. Amount of Degrees (Digital dp), timusdp

Source: Internet
Author: User

Timus Online Judge 1057. Amount of Degrees (Digital dp), timusdp

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 exactlyKDifferent integer degreesB.Example.LetX= 15,Y= 20,K= 2,B= 2. By this example 3 numbers are the sum of exactly two integer degrees of number 2: 17 = 24 + 20,
18 = 24 + 21,
20 = 24 + 22. InputThe first line of input contains integersXAndY, Separated with a space (1 ≤XY≤ 231 −1). The next two lines contain integersKAndB(1 ≤K≤ 20; 2 ≤B≤ 10). OutputOutput shoshould contain a single integer-the amount of integers, lyingXAndY, Being a sum of exactlyKDifferent integer degreesB. Sample
Input Output
15 2022
3


/* Question: how to calculate the number of 1 in the degree hexadecimal System of an interval as k? Digital dp, it must be noted that one number is k dp [I] [j] [k], which indicates that the j-hexadecimal system that has reached the I-bit is still k. The specific points of attention are written in the code. medium */# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <queue> # include <stack> # include <vector> # include <set> # include <map> # define L (x) (x <1) # define R (x) (x <1 | 1) # define MID (x, y) (x + y)> 1) # define bug printf ("hihi \ n") # define eps 1e-8typedef long ll; using namespace Std; # define N 35int dp [33] [15] [33]; int degree, k; int bit [N]; int dfs (int pos, int degree, int t, bool bound) {if (t <0) return 0; if (pos = 0) return t? 0: 1; if (! Bound & dp [pos] [degree] [t]> = 0) return dp [pos] [degree] [t]; int up = bound? Min (bit [pos], 1): 1; int ans = 0; for (int I = 0; I <= up; I ++) ans + = dfs (pos-1, degree, t-I, bound & I = bit [pos]); // It must be bit [pos] and cannot be uo if (! Bound) dp [pos] [degree] [t] = ans; return ans;} int solve (int x) {int I, j; int len = 0; while (x) {bit [++ len] = x % degree; x/= degree;} return dfs (len, degree, k, true);} int main () {int I, j, le, ri; memset (dp,-1, sizeof (dp); while (~ Scanf ("% d", & le, & ri) {scanf ("% d", & k, ° ree ); printf ("% d \ n", solve (ri)-solve (le-1);} return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.