Digital DP CF 55D Beautiful numbers

Source: Internet
Author: User

Topic links

Test instructions: Defines "beautiful number" as one digit n divisible by all digits not 0

Analysis: That is, N is a multiple of the least common multiple of all digits of a digital position. LCM (1 to 9) = 2520. n satisfies is a multiple of the approximate 2520. DP[LEN][VAL][LCM] One-dimensional number of digits, one-dimensional value of%2520 (the preservation of the original number is not possible, there is no need, 2520 is feasible least common multiple the largest one), one-dimensional for the current digital LCM, to determine the conditions of satisfaction is val%lcm==0. This problem discretized 2520 of the approximate, otherwise the space will not open.

#include <bits/stdc++.h>typedef long Long ll;ll dp[20][2520][50];int digit[20];int id[2521];int tot;int GCD (int A, int b) {return B? GCD (b, a% B): A;} int LCM (int a, int b) {return A/GCD (A, b) * b;}    void Init_lcm () {tot = 0;        for (int i=1; i<=2520; ++i) {if (2520% i = = 0) {Id[i] = ++tot;    }}}ll DFS (int pos, int val, int LCM, bool limit) {if (pos = =-1) {return val% LCM = = 0;    } ll &now = DP[POS][VAL][ID[LCM]];    if (!limit && Now! =-1) {return now; } int d = limit?    Digit[pos]: 9;    ll ret = 0;        for (int i=0; i<=d; ++i) {int tval = (val * + i)% 2520; int TLCM = I?        LCM/GCD (LCM, i) * I:LCM;    RET + = DFS (pos-1, Tval, TLCM, limit && i = = d);    } if (!limit) {now = ret; } return ret;}    ll solve (ll x) {int len = 0;    if (x = = 0) {digit[len++] = 0;  } else {while (x) {digit[len++] = x% 10;          x/= 10; }} return DFS (len-1, 0, 1, true);}    int main () {INIT_LCM ();    Memset (DP,-1, sizeof (DP)); int T;    scanf ("%d", &t);        while (t--) {ll L, R; Std::cin >> L >> R;    Std::cout << Solve (R)-Solve (l-1) << ' \ n '; } return 0;}

  

Digital DP CF 55D Beautiful numbers

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.