[Codeforces 258b & 259 D] little elephant and elections digital DP + DFS

Source: Internet
Author: User
The numbers 4 and 7 in the numbers selected by the Party specified by the queue (different) are more than the sum of the numbers owned by the other six parties. StrictMultiple, number of inquiry solutions. For example, when M = 7, the remaining random options will have at least one 4 or 7, which is in conflict with the question. Therefore, when the number of solutions is 0 m = 8, 71 2 3 5 6 8 is a legal solution
Idea: because the number 4 and 7 selected by the elephant's party contains at most the same number as the number of digits m, the number of 4 and 7 contained in the elephant's party is enumerated, use DFS directly for the remaining 6 Parties (using the multiplication principle ). The digital DP can be used to calculate 1 ~ M contains the number of K lucky numbers. Here, we use a memory-based search method for ease of thinking. For details, see the code and comments.
Code:
# Include <cstdio> # include <cstring> # include <cmath> # include <map> # include <algorithm> # include <cstdlib> # define sizes 40000 # define mod 1000000007 llusing namespace STD; long long DP [20] [20]; long res [20]; int bit [20], Bn; long m, n, Pn, ans; void deep (INT now, int _ max, int party, long cur) // now indicates the number of lucky numbers, _ max limit, and Party indicates the serial number, cur is the solution count {If (now> _ max) return; // If (Party = 7) {If (now <_ max) {ans + = C Ur; // Add ans % = MOD;} return ;}for (INT I = 0; I <Bn; I ++) {If (RES [I]) // is it feasible? {res [I] --; deep (now + I, _ max, party + 1, cur * (RES [I] + 1) % mod ); res [I] ++ ;}}long long DFS (long POs, long target, long limit) // POS indicates the number of digits processed, target requires a few lucky numbers. Limit indicates whether there is a limit {long sum = 0; If (Pos = 0) return target = 0; // If (Limit = 0) & (DP [POS] [target]! =-1) return DP [POS] [target]; // long tail = limit? Bit [POS]: 9; // determine the upper limit of enumeration for (INT I = 0; I <= tail; I ++) sum + = DFS (pos-1, target-(I = 4 | I = 7), (Limit = 1) & (bit [POS] = I )); if (Limit = 0) DP [POS] [target] = sum; return sum;} void CAL (long x) {ans = Bn = 0; long long y = x; while (y) {++ Bn; bit [bn] = Y % 10; y/= 10;} For (INT I = 0; I <= Bn; I ++) RES [I] = DFS (bn, I, 1); // calculate the number of optional res in the memory search [0]-= 1; // remove the for (INT I = 1; I <= Bn; I ++) deep (0, I, 1, Res [I]) where a single number is 0; // enumerate the number of lucky numbers} int main () {long m; memset (DP,-1, sizeof DP); scanf ("% i64d", & M ); cal (m); printf ("% i64d \ n", 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.