Perfect number
Time Limit: 2000/1000 ms (Java/others) memory limit: 128000/64000 KB (Java/Others)
Submitstatisticnext Problem
Problem description
8 is a number that Chinese people like very much, but if 3 exists, it will become 38, not very good ..
Can you tell me the number of integers in the range of [L, R] that contain 3 or 8?
Input
The first line is an integer T (T ≤ 10000), representing the number of data groups.
For each group of data, give two integers, L, R (1 ≤ L ≤ r ≤ 1e9)
Output
For each group of data, an integer is given as the answer.
Sample Input
3
1 100
1 3
8
Sample output
34
1
1
Digital motion regulation !!! Explanation in the annotations !!
The AC code is as follows:
# Include <iostream> # include <cstring> using namespace STD; int DP [11] [3]; // There are no three cases, only 3, and only 8 !! Int SW (int A) {int I, L, B; int flag3 = 0, flag8 = 0; // mark the first digit 3 and 8 with no int num [11]; memset (Num, 0, sizeof num); L = 1; B = A; while (B) {num [L ++] = B % 10; B/= 10 ;} int ans = 0; for (I = L-1; I> = 1; I --) {If (! Flag3 &&! Flag8) // when neither 3 nor 8 appears {If (Num [I] <= 3) {ans + = num [I] * (DP [I-1] [1] + dp [I-1] [2]); // In this case, only 3 and 8 exist independently. If (Num [I] = 3) flag3 = 1 ;} else if (Num [I]> 3 & num [I] <= 8) {ans + = DP [I-1] [0] + dp [I-1] [1]; // Add 3 case ans + = (Num [I]-1) * (DP [I-1] [1] + dp [I-1] [2]); // If (Num [I] = 8) flag8 = 1 ;} else {ans + = 2 * DP [I-1] [0] + dp [I-1] [2] + dp [I-1] [1]; // Add 3 or 8 case ans + = (Num [I]-2) * (DP [I-1] [1] + dp [I-1] [2]);} else if (! Flag3 & flag8) {If (Num [I] <= 3) {ans + = num [I] * (DP [I-1] [0] + dp [I-1] [2]); // 8 already exists, do not consider case 3 if (Num [I] = 3) flag3 = 1;} else ans + = (Num [I]-1) * (DP [I-1] [0] + dp [I-1] [2]); // The case where 3 is missing} else if (flag3 &&! Flag8) // similar to the previous {If (Num [I] <= 8) {ans + = num [I] * (DP [I-1] [0] + dp [I-1] [1]); If (Num [I] = 8) flag8 = 1;} else ans + = (Num [I]-1) * (DP [I-1] [0] + dp [I-1] [1]);} return ans;} int main () {int I, j; memset (DP, 0, sizeof DP); DP [0] [0] = 1; for (I = 1; I <= 10; I ++) {DP [I] [0] = DP [I-1] [0] * 8; // without 3 or 8, add the other 8 numbers DP [I] [1] = DP [I-1] [1] * 9 + dp [I-1] [0]; // Add <span style = "font-family: Arial, Helvetica, sans-serif; "> Add 3 without 3 and 8 </span> DP [I] [2] = DP [I-1] [2] * 9 + dp [I-1] [0]; // <span style = "font-family: Arial, Helvetica, sans-serif; "> Add a number other than 3 on the basis of only 8 </span> <span style =" font-family: Arial, Helvetica, sans-serif; "> complement 8 without 3 and 8 </span> // cout <DP [I] [0] <" "<DP [I] [1] <<"" <DP [I] [2] <Endl ;} int t, n, m; CIN> T; while (t --) {CIN> N> m; cout <SW (m + 1)-SW (N) <Endl;} return 0 ;}