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;
}