51nod 1232 完美数

题目思路:数位dp,若这个数能被每位的非0数整除,那么这个数一定可以被每一位数的lcm整除,lcm(1,2,3,4,5,6,7,8,9) = 2520,所以可以通过将这个数对2520取模来压缩空间,取模结果计做mod

dp[pos][lcm][mod],显然20*2520*2520仍然过大,所以我们对mod进行离散,再次压缩空间。

#include <iostream>
#include <cmath>
#include <string.h>
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
#define LL long long
#define MAXSIZE 2600
#define INF 0x3f3f3f3f

LL dp[20][50][MAXSIZE];
const LL Mod = 2520;

int bit[MAXSIZE],ha[MAXSIZE];

LL gcd(LL n,LL m)
{
    if(n%m == 0)
        return m;
    return gcd(m,n%m);
}

void Init()
{
    int pos = 1;
    for(int i=1;i<=Mod;i++)
    {
        if(2520%i == 0)
            ha[i] = pos++;
    }
}

LL dfs(int pos,int lcm,int mod,int limit)
{
    if(pos < 0)
    {
        if(mod%lcm == 0)
            return 1;
        return 0;
    }

    if(dp[pos][ha[lcm]][mod] != -1 && !limit)
        return dp[pos][ha[lcm]][mod];

    LL ans = 0;
    int len = limit?bit[pos]:9;
    for(int i=0;i<=len;i++)
    {
        if(i != 0)
        {
            int new_lcm = lcm*i/(gcd(lcm,i));
            int num = (mod*10+i)%Mod;
            ans += dfs(pos-1,new_lcm,num,limit&&i==len);
        }

        else
        {
            int num = mod*10%Mod;
            ans += dfs(pos-1,lcm,num,limit&&i==len);
        }
    }
    if(!limit)
        dp[pos][ha[lcm]][mod] = ans;
    return ans;
}

LL Solve(LL n)
{
    int pos = 0;
    while(n)
    {
        bit[pos++] = n%10;
        n /= 10;
    }
    LL ans = dfs(pos-1,1,0,1);
    return ans;
}

int main()
{
    Init();
    int T;
    LL n,m;
    scanf("%d",&T);
    memset(dp,-1,sizeof(dp));
    while(T--)
    {
        scanf("%lld%lld",&n,&m);
        LL ans = Solve(m) - Solve(n-1);
        printf("%lld\n",ans);
    }
    return 0;
}

时间: 2024-08-07 18:40:31

51nod 1232 完美数的相关文章

51nod 1232 完美数 数位dp

1232 完美数 题目来源: 胡仁东 基准时间限制:2 秒 空间限制:131072 KB 如果一个数能够被组成它的各个非0数字整除,则称它是完美数.例如:1-9都是完美数,10,11,12,101都是完美数,但是13就不是完美数(因为13不能被数字3整除). 现在给定正整数x,y,求x和y之间(包含x和y的闭区间)共有多少完美数. 题目作者为: Input 第1行:一个数T,表示后面用作输入测试的数的数量.(1 <= T <= 10000) 第2 - T + 1行:每行2个数,X, Y中间用空

51nod 1232:完美数

51nod 1232:完美数 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1232 题目大意:如果一个数能够被组成它的各个非$0$数字整除,则称它是完美数.例如:$10$,$11$,$12$,$101$都是完美数,但是$13$就不是完美数(因为$13$不能被数字3整除).现在给定正整数$x$,$y$,求$[x,y]$中共有多少完美数.共有$T$组数据. 数位DP 如果对于$a \equiv r(mod m)

ACdream 1064 完美数

完美数 Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description 8是中国人很喜欢的一个数字,但是如果有3的存在就变成了38,就不是很好了.. 你能告诉我,在[L, R] 的正整数区间内,要么包含3 要么包含 8 的不同的整数有多少个么? Input 第一行一个整数T (T ≤ 10000),代表数据

完美数(打表)

                     完美数                        Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description 任何一个自然数的约数中都有1和它本身,我们把小于它本身的因数叫做这个自然数的真约数. 如6的所有真约数是1.2.3,而且6=1+2+3.像这样,一个数所有真约数的和正好等于这个数,通常把这个数叫做完美数. 古希腊人非常重视完美数.

HNU 12876 Quite Good Numbers 完美数变形

筛法是一种很快的方法,贴代码纪念一下. 做法很像筛法 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #include <string> 8 #include <queue> 9 #include <

ZZUOJ 10507: 非完美数

题目链接:http://acm.zzu.edu.cn:8000/problem.php?id=10507 题目大意:求L到R区间内所有数与小于它自己的约数的差的绝对值的和. 解题思路:拿个筛子筛一筛.注意1的时候还是1以及long long 代码: const int maxn = 1e6 + 5; typedef long long ll; int l, r; ll a[maxn], sum[maxn]; void dowork(){ memset(a, 0, sizeof(a)); for(

Craps赌博游戏、百钱白鸡、七星彩选号、抓小偷、21根火柴、10000以内完美数

Craps赌博游戏 游戏规则:同时扔两颗骰子,第一次扔出的点数数7或则11玩家胜,扔出2.3或则12庄家胜利,否则继续扔骰子. 以后只要扔出和第一次相同的点数玩家胜,扔出7庄家胜. 玩家每次进入有1000的筹码,输完游戏结束! 1 public static void gambleGame() { 2 int a = 0; 3 int money = 1000; 4 for (int j = 0;; j++) { 5 if (money > 0) { 6 System.out.println(&qu

51NOD 1623 完美消除 数位DP

题目描述: 定义数的消除操作为选定[L,R,x],如果数的第L到第R位上的数字都大于等于x,并且这些数都相等,那么该操作是合法的(从低位到高位编号,个位是第一位,百位是第二位……),然后将这些位数上的数减x:否则就是不合法的,不能进行操作.对一个数操作最少的次数使得这个数变成0,这个操作次数称为该数的最小操作数.如:1232的最小操作数为3,一个合法解是[2,2,1],[1,3,2],[4,4,1]. 求L~R中最小操作数为k的数的个数. 例如:132,需要操作3次才能变为0.而131131 =

51nod 1412 AVL数的种类(DP

题意给了n个节点的AVL数 问种类 树的深度不大 那么转移方程很明显了 dp[i][j]   代表的是节点为n深度为j的树的种类 k为左子树的节点个数 //dp[i][j+1] += dp[k][j]*dp[i-k-1][j];//dp[i][j+1] += 2*dp[k][j-1]*dp[i-k-1][j]; #include <stdio.h> #include <iostream> #include <algorithm> #include <string.