Ural1057 amount of degrees

Source: Internet
Author: User

Given X, Y, K, and B, it is required to find the number of positive integers in the range [x, y] so that they satisfy the power of k different B.

For example

  X= 15, Y= 20, K= 2, B= 217 = 24 + 20,
18 = 24 + 21,
20 = 24 + 22. there are three positive integers that meet the requirements to convert the problem, set g (x) to the number of <= x positive integers that meet the meaning of the question, then the difference between the original question = g (y) -G (x-1) Now solves the problem after conversion and expands X in B-base, the goal is to get a B-base string so that it only contains 0 and 1, if the value is less than or equal to X, dynamic planning can be used to resolve the problem by using F [I] [J] [flag] to perform the I-bit operation, with J numbers 1, flag = indicates the total number of satisfied requirements when the value is smaller than or equal to the value. The recurrence formula is easy to obtain, from high to low. Pay attention to the details. The final answer is f [1] [k] [0] + F [1] [k] [1] (here 1 is a percentile bit)
1 # include <iostream> 2 # include <cstring> 3 using namespace STD; 4 int X, Y, K, B; 5 Int Len, bbas [51]; 6 int f [51] [21] [2]; // 1 equals 0 less than 7 int solve (INT p) {8 Len = 0; memset (bbas, 0, sizeof (bbas); 9 memset (F, 0, sizeof (f); 10 int T = P; 11 while (t) {12 bbas [++ Len] = T % B; t = T/B; 13} 14 if (bbas [Len] = 1) {f [Len] [1] [1] = 1; F [Len] [0] [0] = 1 ;} 15 else {f [Len] [1] [0] = 1; F [Len] [0] [0] = 1;} 16 for (INT I = len-1; i> = 1; I --) 17 for (Int J = 0; j <= K; j ++) {18 if (j = 0) {19 F [I] [J] [0] = 1; 20} else {21 if (bbas [I]> 1) {22 f [I] [J] [0] = f [I + 1] [J-1] [1] + F [I + 1] [J-1] [0] + F [I + 1] [J] [1] + F [I + 1] [J] [0]; 23} else {24 if (bbas [I] = 1) {25 f [I] [J] [0] = f [I + 1] [J] [1] + F [I + 1] [J] [0] + F [I + 1] [J-1] [0]; 26 F [I] [J] [1] = f [I + 1] [J-1] [1]; 27} 28 If (bbas [I] = 0) {29 F [I] [J] [0] = f [I + 1] [J] [0] + F [I + 1] [J-1] [0]; 30 f [I] [J] [1] = f [I + 1] [J] [1]; 31} 32} 33} 34} 35 return f [1] [k] [0] + F [1] [k] [1]; 36} 37 int main () {38 CIN> x> Y> K> B; 39 cout <solve (y)-solve (x-1) <Endl; 40 return 0; 41}

 

Ural1057 amount of degrees

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.