URAL 2052 Physical Education (Digital dp)
??
Question: A natural number series is given. The sum of all digits of each number is used as the first keyword, and the size of each number is used as the second keyword in ascending order. What is the number of unchanged positions.
Train of Thought: first, we can prove that there is only one position unchanged for the number and all the numbers of I. This can be intuitively guessed, because if there is a digit position unchanged, then, for the sorted sequence, the growth rate of all numbers after this number is greater than that of the natural number sequence, so it is impossible to have a second one.
Assume that we have obtained the range where the number and I are [l, r], so that query (a, B) indicates the number of digits and B in the range from 1 to, use su [I-1] to represent the number of digits and the number of numbers from 1 to I-1.
For the range [l, l + query L-1, I)-1], there must be no numbers with unchanged positions, because these numbers are smaller than l,
Then we narrow down the range to [su [I-1] + query (L-1, I) + 1, su [I-1] + query (r, I)], because there is only one constant number at most, this interval can be reduced all the time, and this recursion can be done.
Recursive exit is l = r and su [I-1] + query (l-1, I) + 1 = su [I-1] + query (r, I), then return 1, otherwise, 0 is returned. That is to say, the number at the l position is a constant number at the position.
Now the problem is converted to query. For this problem, we use an auxiliary array dp [] [] [] to help us calculate the problem, dp [I] [j] [k] indicates the sum of the numbers where the I-th digit is j and k, and the I-th digit is the highest digit of this number.
For a query, if the number of the Current BIT is smaller than the number of the given n, you can select the number at the end of the query. In this way, you can solve the query problem. For details, see the code.
#include
#include
#include
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
#define eps 1e-6#define LL long longusing namespace std;//const int maxn = 100 + 5;//const int INF = 0x3f3f3f3f;int n, dp[20][20][100], su[100];void init() {for(int i = 0; i <= 9; i++) dp[1][i][i] = 1;for(int i = 2; i <= 10; i++) {for(int j = 0; j <= 9; j++) {for(int t = 0; t <= 81; t++) {for(int k = 0; k <= 9; k++)if(t>=j) dp[i][j][t] += dp[i-1][k][t-j];}}}}int query(int r, int digit_sum) {int digit[20], len=0;int tmp = r;while(tmp) {digit[++len] = tmp % 10;tmp /= 10;}int cur_sum = 0;int ans = 0;for(int i = len; i >= 1; i--) {for(int j = 0; j < digit[i]; j++) {if(digit_sum-j-cur_sum >= 0) ans += dp[i][j][digit_sum-cur_sum];}cur_sum += digit[i];}if(cur_sum == digit_sum) ans++;return ans;}void init_su() {for(int i = 1; i <= 81; i++) su[i] = su[i-1] + query(n, i);}int solve(int l, int r, int digit_sum) {int left_border = su[digit_sum-1] + query(l-1, digit_sum)+1;int right_border = su[digit_sum-1] + query(r, digit_sum);if(left_border > right_border) return 0;if(left_border==right_border) {if(su[digit_sum-1]+query(left_border-1,digit_sum)+1 == su[digit_sum-1] + query(right_border, digit_sum)) return 1;return 0;}return solve(left_border, right_border, digit_sum);}int main() {//freopen(input.txt, r, stdin);init();while(cin >> n) {init_su();//for(int i = 1; i <= 9; i++) cout << i << : << query(n, i) << endl;int ans = 0;for(int i = 1; i <= 81; i++) ans += solve(su[i-1]+1, su[i], i);//for(int i = 1; i <= 10; i++) cout << i << : << solve(su[i-1]+1, su[i], i) << endl;cout << ans << endl;}return 0;}