Ural 1057 (Amount of Degrees-getting started with digital statistics)

Source: Internet
Author: User

1057. Amount of Degrees
Time 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 exactlyK different integer degrees of B.
Example. Let X = 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.
Input
The first line of input contains integers X and Y, separated with a space (1 ≤ X ≤ Y ≤ 231 −1 ). the next two lines contain integers K and B (1 ≤ K ≤ 20; 2 ≤ B ≤ 10 ).
Output
Output shoshould contain a single integer-the amount of integers, lying between X and Y, being a sum of exactly K different integer degrees of B.
Sample
Input output
15 20
2
2
3
 

Problem Source: Rybinsk State Avia Academy

For more information, see the program remarks. (enter a number)


[Cpp] # include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <cmath>
# Include <cctype>
Using namespace std;
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define Rep (I, n) for (int I = 0; I <n; I ++)
# Define Fork (I, k, n) for (int I = k; I <= n; I ++)
# Define ForD (I, n) for (int I = n; I --)
# Define Forp (x) for (int p = pre [x]; p = next [p])
# Define RepD (I, n) for (int I = n; I> = 0; I --)
# Define MAXK (20 + 10)
# Define MAXB (10 + 10)
# Define MAXLog (32 + 10)
Int x, y, k, B, C [MAXLog] [MAXLog];
Int a [MAXLog];
Int calc (int x, int k, int B) // k num () B
{
Int len = 0, ans = 0;
While (x)
{
A [++ len] = x % B;
X/= B;
}
ForD (I, len)
{
If (a [I]> 1)
{
Ans + = C [I-1] [k] + C [I-1] [k-1]; break; // consider this 1/0 case
}
Else if (a [I] = 1)
{
Ans + = C [I-1] [k]; // consider the case where this is 0
K --;
}
If (k <0) return ans;
}
Return ans;
}
Int main ()
{
// Freopen ("ural1057.in", "r", stdin );
Rep (I, 32 + 1)
{
C [I] [0] = 1; C [I] [1] = I;
Fork (j, 2, I) C [I] [j] = C [I-1] [j] + C [I-1] [J-1];
}
// Cout <C [3] [1] <<''' <C [3] [2] <<''' <C [3] [3];
Scanf ("% d", & x, & y, & k, & B );
Cout <calc (y + 1, k, B)-calc (X-1 + 1, k, B) <endl;


Return 0;
}

# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <cmath>
# Include <cctype>
Using namespace std;
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define Rep (I, n) for (int I = 0; I <n; I ++)
# Define Fork (I, k, n) for (int I = k; I <= n; I ++)
# Define ForD (I, n) for (int I = n; I --)
# Define Forp (x) for (int p = pre [x]; p = next [p])
# Define RepD (I, n) for (int I = n; I> = 0; I --)
# Define MAXK (20 + 10)
# Define MAXB (10 + 10)
# Define MAXLog (32 + 10)
Int x, y, k, B, C [MAXLog] [MAXLog];
Int a [MAXLog];
Int calc (int x, int k, int B) // k num () B
{
Int len = 0, ans = 0;
While (x)
{
A [++ len] = x % B;
X/= B;
}
ForD (I, len)
{
If (a [I]> 1)
{
Ans + = C [I-1] [k] + C [I-1] [k-1]; break; // consider this 1/0 case
}
Else if (a [I] = 1)
{
Ans + = C [I-1] [k]; // consider the case where this is 0
K --;
}
If (k <0) return ans;
}
Return ans;
}
Int main ()
{
// Freopen ("ural1057.in", "r", stdin );
Rep (I, 32 + 1)
{
C [I] [0] = 1; C [I] [1] = I;
Fork (j, 2, I) C [I] [j] = C [I-1] [j] + C [I-1] [J-1];
}
// Cout <C [3] [1] <<''' <C [3] [2] <<''' <C [3] [3];
Scanf ("% d", & x, & y, & k, & B );
Cout <calc (y + 1, k, B)-calc (X-1 + 1, k, B) <endl;
 
 
Return 0;
}

 

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.