P1112 wave count, p1112 wave count
Description
The number of waves is the number of alternating conversions between a pair of numbers, such as 1212121. The number of double waves is the number of waves in two hexadecimal notation, for example, decimal number 191919 is the number of waves in decimal notation, And the decimal number 121212 is also a number of waves. Therefore, decimal number 191919 is a double number of waves.
Similarly, we can define the number of three waves. The number of three waves is the number of waves in three different hexadecimal formats, and even the number of four waves, such as decimal 300 = 606 (in decimal format) = 363 (hexadecimal) = 454 (octal) = 1A1 (hexadecimal )..., Your task is to find out the double, triple, and four-wave numbers within the specified range.
Input/output format:
A single row contains five decimal integers separated by spaces. The first two numbers represent the hexadecimal range (2 • 32 ), the numbers 3 and 4 indicate the specified range (1 • 10000000). The fifth number is one of 2, 3, and 4, indicating the number of waves to be searched.
Output Format:
Output The number of waves of the specified weight in the specified range in decimal form from small to large. A row outputs a number.
Input and Output sample input sample #1:
10 11 190000 960000 2
Output sample #1:
191919383838575757767676959595
Question:
If the brute-force enumeration method is used, the time cannot be used. We can directly construct the number of waves, so we only need to enumerate the digits, the two repeated numbers, and the length of the number of waves.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> const int maxn = 10000000 + 1; using namespace std; int l1, l2, r1, r2, num, mxl, mnl; int cnt [maxn]; int h (int a, int B, int k, int len) {// construct, b's number of waves with the length of len in k-base: int x = 0; for (int I = 1; I <= len; I ++) {if (I & 1) x = x * k + a; // (I & 1) equals (I % 2) else x = x * k + B;} return x ;} int clen (int x, int k) {// evaluate the length of x in k-base int cnt = 0; while (x) {x = x/k; cnt ++;} return cnt;} int main () {Scanf ("% d", & l1, & r1, & l2, & r2, & num); for (int k = l1; k <= r1; k ++) for (int I = 1; I <k; I ++) // you must start from 1, the first digit cannot be 0 for (int j = 0; j <k; j ++) if (I! = J) {mnl = clen (l2, k); mxl = clen (r2, k); for (int l = mnl; l <= mxl; l ++) {int tmp = h (I, j, k, l); if (tmp> = l2 & tmp <= r2) // must be added here, what if the constructed number exceeds the range of the array? Cnt [tmp] ++ ;}}for (int I = l2; I <= r2; I ++) if (cnt [I] = num) printf ("% d \ n", I); return 0 ;}