问区间内有多少数能被这个数的每一位整除

#include <iostream>
#include <functional>
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

const int MAXN=25;
const int MOD=2520;//1~9的lcm为2520
long long dp[MAXN][MOD][48];
int index[MOD+10];//记录1~9的最小公倍数
int bit[MAXN];
int gcd(int a,int b)
{
    if(b==0)return a;
    else return gcd(b,a%b);
}
int lcm(int a,int b)
{
    return a/gcd(a,b)*b;
}

void init()
{
    int num=0;
    for(int i=1;i<=MOD;i++)
        if(MOD%i==0)
            index[i]=num++;
}
long long dfs(int pos,int preSum,int preLcm,bool flag)//到这一位,到当前和,公倍数,有无到头
{
    if(pos==-1)//如果到头了
        return preSum%preLcm==0;//并且能取模,就加一
    if(!flag && dp[pos][preSum][index[preLcm]]!=-1)//没到头并且记录过,记录的都是满的
        return dp[pos][preSum][index[preLcm]];//返回记录值
    long long ans=0;//记录答案
    int endn=flag?bit[pos]:9;//如果之前的没满,上界是9,否则上界就是当前位
    for(int i=0;i<=endn;i++)//对当前这位从0开始
    {
        int nowSum=(preSum*10+i)%MOD;//计算合
        int nowLcm=preLcm;//继承公倍数
        if(i)nowLcm=lcm(nowLcm,i);//计算公倍数
        ans+=dfs(pos-1,nowSum,nowLcm,flag && i==endn);//向下递归
    }
    if(!flag)dp[pos][preSum][index[preLcm]]=ans;//如果没到头,记录的都是满的
    return ans;//返回答案
}
long long calc(long long x)
{
    int pos=0;
    while(x)
    {
        bit[pos++]=x%10;
        x/=10;
    }
    return dfs(pos-1,0,1,1);
}
int main()
{
    int T;
    long long l,r;
    init();
    memset(dp,-1,sizeof(dp));
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d",&l,&r);
        printf("%I64d\n",calc(r)-calc(l-1));
    }
    return 0;
}

转载于:https://www.cnblogs.com/nj-czy/p/5489953.html

CODEFORCES 55D相关推荐

  1. CodeForces - 55D Beautiful numbers

    题目链接:http://codeforces.com/problemset/problem/55/D 题意:求区间[L,R]有多少个Beautiful numbers.Beautiful number ...

  2. Codeforces 55D Beautiful Number (数位统计)

    把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容:  a positive integer number is beautiful if and only if it is  ...

  3. Codeforces 55D Beautiful numbers (数位DP)

    题意:有T组询问,每次询问区间[l, r]中的beautiful number有多少.beautiful number是指这个数可以被组成它的数字整除.例如15是beautiful number,因为 ...

  4. CodeForces - 55D UVALive - 6528

    A. 问L到R有多少能被自己各个数位上的非零数字整除的数字. 关键在于dp的状态:注意到这里有三个关键属性:当前数位,2~9出现的情况(0,1不用管), 原数字取余2520的结果(2~9的最小公倍数) ...

  5. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  6. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  7. [kuangbin]各种各样的题单

    [kuangbin]各种各样的题单 专题1 简单搜索 POJ 1321 POJ 2251 POJ 3278 POJ 3279 POJ 1426 POJ 3126 POJ 3087 POJ 3414 F ...

  8. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  9. linux文件分割命令性能,Linux系统下使用split命令分割大文件 (转载)

    [小蜗牛闲情之作 ] 我想给一个朋友传一个大视频,有几百M,尝试多种传输办法失败后,最后想到的是把视频切开一片片"邮递"过去给他,让它自己组装起来吧. [root@pps publ ...

最新文章

  1. 用koa开发一套内容管理系统(CMS),支持javascript和typescript双语言
  2. LeetCode题解 343.Integer Break
  3. Python 逆向抓取 APP 数据
  4. make、make是什么??
  5. 【蓝桥杯单片机】IIC通讯协议与EEPROM(AT24C02)(官方驱动源码改写)
  6. MySQL数据库基本用法-查询
  7. 人脸表情识别/人脸检测/ML/DL/图像处理博主
  8. Odoo10 自定义模块创建
  9. 《Python》 字典
  10. 三星 GT-I9118拆机换外屏教程
  11. JS:读取excel并显示
  12. 什么是SAP Cloud Platform(SAP云平台) ?
  13. Mac苹果电脑 安装virtualBox
  14. 计量经济学及Stata应用 第五章习题 5.7 使用回归模型进行餐馆选址。数据集Woody3.dta包含33家Woody‘s连锁餐馆的以下变量……
  15. 【怎么突破安全狗和360网站卫士的】
  16. 随机种子 seed()
  17. MSN aborted;0x80004004异常
  18. pycharm文件打包为exe
  19. UGUI实现虚拟手柄功能
  20. 思科交换机基础--4FTP远程备份

热门文章

  1. IE下的li有空白问题
  2. [转][android深入学习]android窗口管理机制
  3. android悬浮动态权限,android应用内悬浮窗-自动贴边,不需要权限!
  4. python难学?对初学者不友好?进来看看,每30s就能学会一个小技巧,你get到了嘛?
  5. 这些Python常用的工具和学习资源你都知道么?
  6. Python人脸识别+手机推送,老板来了你就会收到短信提示
  7. matlab的rvctools,rvctools 机器人工具箱,主要用于 仿真, 型很强。 matlab 270万源代码下载- www.pudn.com...
  8. dispatcherservlet发送2次请求_Spring源码分析(三):MVC请求分发器DispatcherServlet设计实现...
  9. linux fio释放内存,linux使用FIO测试磁盘的iops
  10. python正方形螺旋绘制_利用Python绘制一个正方形螺旋线