Codeforces 55D Beautiful Numbers (digital dp+ math)

Source: Internet
Author: User
Tags gcd

Title Link: Http://codeforces.com/problemset/problem/55/D

Test instructions: A beautiful number is a number that can be divisible by the number of each of its digits.

Given an interval, the number of beautiful numbers is obtained.

This is obviously a digital DP, which satisfies a number that can be divisible by the LCM of all bits.

The dp[len][mod][lcm],mod is usually set to represent the remainder, and the LCM represents the LCM of the front len bit.

But if the direct naked mod will be very complex, then want to lcm{0,1,2,3,4,5,6,7,8,9}=2520;

and Lcm{a,b,c,d ...} {a,b,c,d ... Indicates the number of individual digits) to be lcm{0,1,2....9 after going to the weight}

Divisible. We are asking for SUM%LCM (A,b,c,d.} ==0, so just meet

SUM%LCM (0,1,2,... 9}%LCM (A,b,c,d ...} ==0 can be. The MoD can then be expressed as

SUM%LCM (0,1,2,... 9} is the number. But mod<=2520 && lcm<=2520 this

Must not survive, so to consider how to deal with the LCM, after all, it is obvious that the 0~9 maximum common multiple species will not

Of more than 48. So you can consider discretization of the LCM,

If 2520% num = = 0-lcm[num]=temp++;

So the three-dimensional DP can be set to dp[20][2520][48];

#include <iostream> #include <cstring> #include <cmath> #include <cstdio>using namespace std; typedef long LONG ll;const int mmax = 2520;ll N, m, Dp[20][mmax][50];int temp, dig[20], Lcm[mmax + 10];ll gcd (ll A, l L b) {return b > 0 gcd (b, a% B): A;} ll LCM (ll A, ll b) {return A/GCD (A, b) * b;}    void init () {temp = 0;        for (int i = 1; I <= Mmax; i++) {if (mmax% i = = 0) {Lcm[i] = temp++;        } else {Lcm[i] = 0;    }}}ll dfs (int len, int count, int mod, int flag) {if (!len) {return mod% count = = 0;    } if (!flag && dp[len][mod][lcm[count]]! =-1) {return dp[len][mod][lcm[count]]; } int t = flag?    Dig[len]: 9;    ll sum = 0;        for (int i = 0; I <= t; i++) {int nextmod = (mod * + i)% Mmax;        int nextcount;        if (i = = 0) {Nextcount = count;        } else {nextcount = (int) LCM (count, i); }       Sum + = DFS (len-1, Nextcount, NEXTMOD, flag && i = = t);    } if (!flag) dp[len][mod][lcm[count]] = sum; return sum;}    ll Gets (ll x) {memset (dig, 0, sizeof (DIG));    int len = 0;    if (x = = 0) {Dig[++len] = 0;        } while (x) {Dig[++len] = x 10;    x/= 10; } return Dfs (len, 1, 0, 1);}    int main () {int t;    scanf ("%d", &t);    Init ();    Memset (DP,-1, sizeof (DP));        while (t--) {scanf ("%i64d%i64d", &n, &m);    printf ("%i64d\n", gets (m)-gets (n-1)); } return 0;}

Codeforces 55D Beautiful Numbers (digital dp+ math)

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.