zoukankan      html  css  js  c++  java
  • CodeForces

    题目链接:https://vjudge.net/problem/CodeForces-55D

    D. Beautiful numbers
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.

    Input

    The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri(1 ≤ li ≤ ri ≤ 9 ·1018).

    Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).

    Output

    Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).

    Examples
    input
    1
    1 9
    output
    9
    input
    1
    12 15
    output
    2

    题解:

    题意:求一段区间内满足“能整除所有位上的数(非0)”的数的个数。

    1.可知:如果一个数能被一个集合内的所有数整除,那么它必定能被这个集合上的数的最小公倍数整除。所以,在处理的时候,我们只需要记录最小公倍数。经过计算,1到9的最小公倍数为2520.

    2.dp[pos][lcm][num]:处理到pos位,从最高位到pos位上的数的最小公倍数为lcm,且从最高位到pos位所形成的数%2520为num。

    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const double eps = 1e-6;
     5 const int INF = 2e9;
     6 const LL LNF = 9e18;
     7 const int mod = 1e9+7;
     8 const int maxn = 20;
     9 
    10 const int MOD = 2520;   //1……9的最小公倍数为2520;
    11 
    12 int digit[20], M[2550];
    13 LL dp[20][50][2550];    //1 …… 9 组合而成的最小公倍数有48个,故离散化后50个足矣。
    14 
    15 LL gcd(LL a, LL b)
    16 {
    17     return b==0?a:gcd(b, a%b);
    18 }
    19 
    20 LL dfs(int pos, LL lcm, LL num, bool lim)
    21 {
    22     if(!pos) return (num%lcm==0);   //除得尽
    23     if(!lim && dp[pos][M[lcm]][num]!=-1) return dp[pos][M[lcm]][num];
    24 
    25     LL ret = 0;
    26     int maxx = lim?digit[pos]:9;
    27     for(int i = 0; i<=maxx; i++)
    28         ret += dfs(pos-1, i?lcm*i/gcd(lcm, i):lcm, (num*10+i)%MOD, lim&&(i==maxx));
    29 
    30     if(!lim) dp[pos][M[lcm]][num] = ret;
    31     return ret;
    32 }
    33 
    34 LL solve(LL n)
    35 {
    36     int len = 0;
    37     while(n)
    38     {
    39         digit[++len] = n%10;
    40         n /= 10;
    41     }
    42     return dfs(len, 1, 0, true);
    43 }
    44 
    45 int main()
    46 {
    47     LL T, n, m;
    48     scanf("%lld", &T);
    49     memset(dp,-1,sizeof(dp));
    50 
    51     int cnt = 0;
    52     /*  原来还可以这样求一个集合的不同组合的最小公倍数的个数。
    53         先算出这个集合所有元素的最小公倍数,那么我们可以得出一个结论:
    54         这个最小公倍数必定能被其他元素组合的最小公倍数整除。根据这个结论,
    55         我们就可以求出一个集合中不同元素组合的最小公倍数的个数
    56     */
    57     for(int i = 1; i<=MOD; i++)
    58         if(MOD%i==0)
    59             M[i] = ++cnt;
    60     while(T--)
    61     {
    62         scanf("%lld%lld",&m,&n);
    63         LL ans = solve(n) - solve(m-1);
    64         printf("%lld
    ", ans);
    65     }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    什么是架构
    jenkins权限插件配置
    解决Error response from daemon: oci runtime error: container_linux.go:247: starting container process
    解决“/tmp/crontab bad minute”问题
    yml链接mysql路径serverTimezone=UTC的那些坑
    java请求头导致特殊字符为空问题
    使用ssh连接WSL
    系统设计与任务分配
    需求规格说明书
    选题报告
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7911004.html
Copyright © 2011-2022 走看看