传送门:Chriswho

题意:求区间[1,n]内能整除自己本身各位数字的数的个数。

分析:这题跟CF 55D Beautiful numbers一样的,一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而1到9的最小公倍数为2520,为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,但这里我们只需记录它对2520的模即可,dp[pos][sum][lcm]表示非限制条件下(limit==0),当前在第pos位模2520余sum且前面各位数字的最小公倍数为lcm的符合条件的数的总数。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 1010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
LL dp[][][];
int dig[],num[];
LL times=;
int gcd(int a,int b)
{
return a%b==?b:gcd(b,a%b);
}
LL dfs(int pos,int sum,int lcm,int limit)
{
if(!pos){//times++;
return sum%lcm==;}
if(!limit&&~dp[pos][sum][num[lcm]])return dp[pos][sum][num[lcm]];
int len=limit?dig[pos]:;
LL ans=;
for(int i=;i<=len;i++)
{
int newlcm;
if(i==)newlcm=lcm;
else newlcm=lcm/gcd(i,lcm)*i;
ans+=dfs(pos-,(sum*+i)%,newlcm,limit&&len==i);
}
if(!limit)dp[pos][sum][num[lcm]]=ans;
return ans;
}
LL solve(LL x)
{
int len=;
while(x)
{
dig[++len]=x%;
x/=;
}
return dfs(len,,,);
}
int check(LL n)
{
LL x=n,flag=;
while(x)
{
int s=x%;
x/=;
if(s==)continue;
if(n%s)flag=;
}
return flag;
}
LL fact(LL x)
{
LL res=;
for(int i=;i<=x;i++)
{
if(check(i))res++;
}
return res;
}
void init()
{
FILL(dp,-);
int cnt=;
for(int i=;i<=;i++)
if(%i==)num[i]=++cnt;
}
int main()
{
LL n;
int T;
init();
scanf("%d",&T);
while(T--)
{
scanf("%I64d",&n);
printf("%I64d\n",solve(n)-);
// printf("%I64d\n",fact(b)-fact(a-1));
// printf("%I64d\n",times);
}
}

FZU2179(数位dp)的更多相关文章

  1. 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

    [BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...

  2. bzoj1026数位dp

    基础的数位dp 但是ce了一发,(abs难道不是cmath里的吗?改成bits/stdc++.h就过了) #include <bits/stdc++.h> using namespace ...

  3. uva12063数位dp

    辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...

  4. HDU2089 不要62[数位DP]

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. 数位DP GYM 100827 E Hill Number

    题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...

  6. 数位dp总结

    由简单到稍微难点. 从网上搜了10到数位dp的题目,有几道还是很难想到的,前几道基本都是模板题,供入门用. 点开即可看题解. hdu3555 Bomb hdu3652 B-number hdu2089 ...

  7. 数位DP入门

    HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...

  8. 数位DP之奥义

    恩是的没错数位DP的奥义就是一个简练的dfs模板 int dfs(int position, int condition, bool boundary) { ) return (condition ? ...

  9. 浅谈数位DP

    在了解数位dp之前,先来看一个问题: 例1.求a~b中不包含49的数的个数. 0 < a.b < 2*10^9 注意到n的数据范围非常大,暴力求解是不可能的,考虑dp,如果直接记录下数字, ...

  10. 数位DP

    题意:(hdu 4734) 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目 ...

随机推荐

  1. 基于visual Studio2013解决面试题之1003字符串逆序

     题目

  2. c++中一个类所占用的空间

    看到阿里的一道笔试题: #pragma pack(2) class A { int i; union U { char buff[13]; int i; }u; void foo() { } type ...

  3. Android原生APP内分享

    Android原生APP内分享,可实现数据分享以及assets文件夹分享及私有文件分享 项目地址:https://github.com/json-pu/AndroidAppShare.git

  4. linux命令:使用man, 导出man

    要查一个命令怎么使用,使用"man 命令", eg: man find, man ls; "info 命令"貌似也可以看, info find, info ls ...

  5. Appium 的安装启动

    Appium 的安装方式根据官网提供的是使用nodejs 安装,但是官方给出了三种方法 参考网址: appium下载地址: https://bitbucket.org/appium/appium.ap ...

  6. 管道是如何处理HTTP请求的?

    管道是如何处理HTTP请求的? 我们知道ASP.NET Core请求处理管道由一个服务器和一组有序的中间件组成,所以从总体设计来讲是非常简单的,但是就具体的实现来说,由于其中涉及很多对象的交互,我想很 ...

  7. nodejs+socket.io即时聊天实例

    在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...

  8. Perl入门(四)Perl的正則表達式

    正則表達式是Perl语言的特色.主要的语法不是非常难,可是编写一个符合需求.高效的正則表達式.还是有一些挑战的. Perl的三种匹配模式 1.查找 语法:m/正則表達式内容/; 作用:查找匹配内容中是 ...

  9. 禁用viewstate

    <asp:Dropdownlist/>禁用viewstate以后 public partial class _Default : System.Web.UI.Page     {      ...

  10. win2003的IIS無法使用,又一次安裝提示找不到iisadmin.mfl文件

    我的系統是win2003 繁體版 sp2,現在iis無法使用,我同事的也是,也不知道是不是跟在網域中有關係,因為我用虛擬機的繁體系統win2003 R2版iis能够正常使用,不過曾经那台電腦也是在網域 ...