URAL 2052 Physical Education (digital DP)

Source: Internet
Author: User



Test instructions: Give a natural number sequence, according to the sum of all the digits of each number as the first keyword, the size of each number as the second keyword in ascending order, the number of positions unchanged.

Train of thought: first can prove that for the number of digits and for all the number of I, there can be at most one position unchanged, this can be intuitively guessed, because if there is a number position unchanged, then for the ordered sequence, the number of the following numbers of all the growth rate is greater than the natural number sequence, so there is no second.

Suppose we currently find the number of digits and the interval for i as [L, R], so that query (a, b) represents the number of digits and number of B in the interval of 1 to a, with su[i-1] to denote the number of digits and the number of 1 to i-1.

Then for the interval [L, L+query (L-1, I)-1], there must be no positional constant number, because these numbers are smaller than L,

Then we'll narrow the interval to [Su[i-1]+query (L-1, i) +1, Su[i-1]+query (R, I)], because at most one position is the same number, so the interval can be narrowed down, and this will be done by hand.

The exit of the recursion is L==r and su[i-1]+query (L-1, i) +1==su[i-1]+query (r,i), which returns 1, otherwise returns 0. That is, this number in the L position is the same number as the position.

Now that the problem is converted to query, for this problem, we use an auxiliary array dp[][][] to help us calculate, Dp[i][j][k] indicates the number of a bit I is J number and K, and the first bit is the highest bit of this number.

For query, each time if the number of the current bit is smaller than the number of the given n, then the following can be arbitrarily selected, so that you can solve the problem of query, see the code.

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack > #include <string> #include <map> #include <set> #define EPS 1e-6#define LL long longusing namespace std;//const int MAXN = + 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 <=; i++) {for (int j = 0; J <= 9; j + +) {for (int t = 0; t <= Bayi; 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++;returnAns;} void Init_su () {for (int i = 1; I <= Bayi; 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++) CO UT << 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 <=; i++) cout << i << ":" << Solve (su[i -1]+1, Su[i], i) << endl;cout << ans << endl;} return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL 2052 Physical Education (digital DP)

Related Article

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.