codeforces55D

查询给定区间内的beautiful number。  一个数字是beautiful number当且仅当能被自己的各个数字不为0的位整除。

这个dp的状态还是挺难想的。一个数能被自己的各个位整除,那么必须是这些位的最小公倍数的倍数。

那么可以想到的一个状态是dp[i][j][k] 为长度为i的时候,数字是组成的数字是j,各个位的最小公倍数是k, 那么当j%k==0,说明数字j是可行的。

当时j太大了,根本存不下。但是数字1-->9最大的一个最小公倍数是2520, 所以只要 j%2520%k==0就行了。

这是为什么呢 ,因为j可以分解为(k*2520 + x) % k , x=j%2520, 所以只要判断x%k是不是等于0就行了。

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef __int64 LL;
const int mod = ;
const int INF = <<;
/*
dp[i][j][k]
表示前取了前i位,数字为j,各个位的最小公倍数位k
如果j%k==0 那么说明可以 dp[i+1][j*10 + i][lcm(k,i)] = dp[i][j][k] */ LL dp[][][];
int num[];
bool vis[];
int hs[ + ];
int gcd(int a, int b)
{
if (b == )return a;
return gcd(b, a%b);
}
int lcm(int a, int b)
{
return a*b / gcd(a, b);
}
void init()
{
int cnt = ;
for (int i = ; i <= mod; ++i)
if (mod%i == )
hs[i] = cnt++;
} LL dfs(int pos, int preSum, int preLcm, bool flag)
{
if (pos == )return preSum % preLcm==;
if (!flag && dp[pos][preSum][hs[preLcm]] != -)
return dp[pos][preSum][hs[preLcm]];
int limit = flag ? num[pos] : ;
LL ans = ;
for (int i = ; i <= limit; ++i)
{
int nowSum = (preSum * + i) % mod;
int nowLcm = preLcm;
if (i)nowLcm = lcm(nowLcm, i);
ans += dfs(pos - , nowSum, nowLcm, flag&&i == limit);
}
if (!flag)//如果pos为比n的第pos位小,那么说明可以利用已有的结果,如果不是的话,不能,因为可能比n大
dp[pos][preSum][hs[preLcm]] = ans;
return ans;
}
LL calc(LL n)
{ int len = ;
while (n)
{
num[++len] = n % ;
n /= ;
} return dfs(len,,,);
}
int main()
{
init();
int t;
LL l, r;
memset(dp, -, sizeof(dp));//只初始化一次,因为计算的结果可多次利用
scanf("%d", &t);
while (t--)
{
scanf("%I64d%I64d", &l, &r);
printf("%I64d\n", calc(r) - calc(l-));
}
return ;
}

codeforces55D数位dp的更多相关文章

  1. 数位DP入门

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

  2. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

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

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

  4. bzoj1026数位dp

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

  5. uva12063数位dp

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

  6. HDU2089 不要62[数位DP]

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

  7. 数位DP GYM 100827 E Hill Number

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

  8. 数位dp总结

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

  9. 数位DP之奥义

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

随机推荐

  1. IT忍者神龟之中的一个句sql语句——连接同一字段的全部值

    Oracle能够用SYS_CONNECT_BY_PATH字符串聚合函数: SELECT LTRIM(MAX(SYS_CONNECT_BY_PATH(productname, ', ')), ', ') ...

  2. Java总结之容器

    [容器的概念] 容器:Java API所提供的一系列的实例,用于在程序中存放对象. [容器 API] J2SDK所提供的容器API位于java.util包内. {Collection[Set(Hash ...

  3. Swift - 可选类型说明

    可选类型是Swift的一个特色.它表示一个变量有可能有值,也可能没有值(nil). 声明的方式是“数据类型+问号”. 而当要使用一个可选类型的变量是,要在后面加感叹号“!”. 1 2 3 4 5 6 ...

  4. flask开发restful api

    flask开发restful api 如果有几个原因可以让你爱上flask这个极其灵活的库,我想蓝图绝对应该算上一个,部署蓝图以后,你会发现整个程序结构非常清晰,模块之间相互不影响.蓝图对restfu ...

  5. javascript实现图片无缝滚动(scrollLeft的使用方法介绍)

    <!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" conten ...

  6. 如何设置lmt的空间警告阀值

    Example—设置Locally Managed Tablespace的空间警告阀值 The following example sets the free-space-remaining thre ...

  7. 14.4.4 Configuring the Memory Allocator for InnoDB InnoDB 配置内存分配器

    14.4.4 Configuring the Memory Allocator for InnoDB InnoDB 配置内存分配器 当InnoDB 被开发, 内分配齐 提供了与操作系统和运行库往往缺乏 ...

  8. Android常用控件之RatingBar的使用

    RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...

  9. web框架python

    22.python笔记之web框架   一.web框架本质 1.基于socket,自己处理请求 #!/usr/bin/env python3 #coding:utf8 import socket de ...

  10. android画笔错位问题的解决

    下面的画画板的代码: public class MainActivity extends Activity { private ImageView iv; private Bitmap baseBit ...