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 ≤X≤Y≤ 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
|
/* 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.