Hdu 4734 F (x) digital dp, hdu4734

Source: Internet
Author: User

Hdu 4734 F (x) digital dp, hdu4734

Definition F (x) =N * 2N-1 +N-1 * 2N-2 +... +2*2 +1*1 (x =)NAN-1AN-2...2A1), given A, B, and the I in the [0, B] range, F (I) <= F ()

.

Idea: Set dp [pos] [k] as the current pos bit, and then (pos + 1) the number of F function values formed by the combination of BITs and the previous bits cannot exceed k. For details, see the code:

/*************************************** * ****************** File name: hdu4734.cpp author: kereo create time: ******************************** * ***********************/# include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <set> # include <map> # include <vector> # include <stack> # include <cmath> # include <string> using namespace std; typedef long ll; const int sig Ma_size = 26; const int N = 10; const int MAXN = 6000 + 50; const int inf = 0x3fffffff; const double eps = 1e-8; const int mod = 100000000 + 7; # define L (x) (x <1) # define R (x) (x <1 | 1) # define PII pair <int, int> # define mk (x, y) make_pair (x), (y) int res; int bit [N], dp [N] [MAXN], a [N], p [N]; int dfs (int pos, int st, int flag) {if (pos =-1) return 1; if (flag & dp [pos] [st]! =-1) return dp [pos] [st]; int ans = 0; int x = flag? 9: bit [pos]; for (int I = 0; I <= x; I ++) {int k = st-(int) p [pos] * I; if (k <0) continue; ans + = dfs (pos-1, k, flag | I <x);} if (flag) dp [pos] [st] = ans; return ans;} int solve (int x) {int len = 0; do {bit [len ++] = x % 10; x/= 10;} while (x ); return dfs (len-1, res, 0);} int main () {int T, kase = 0; scanf ("% d", & T ); p [0] = 1; for (int I = 1; I <10; I ++) p [I] = p [I-1] * 2; memset (dp, -1, sizeof (dp); while (T --) {int A, n; scanf ("% d", & A, & n ); int cnt = 0; res = 0; do {a [cnt ++] = A % 10; A/= 10; res + = p [cnt-1] * a [cnt-1];} while (A); int ans = solve (n); printf ("Case # % d: % d \ n ", ++ kase, ans);} return 0 ;}


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.