Codeforces - 55D Beautiful numbers (数位dp+数论)

时间:2022-10-12 15:01:44

题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数

分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L-1)前缀和相减的形式。如果一个数能被所有位上数的最小公倍数(lcm)整除,便是符合要求的数。

但是直接传递一个数n的话,dp数组肯定开不下。那么怎么让其传递的值更小呢。

先了解一个等式:  sum%(x*n)%x == sum%x

可以将一个数枚举到第pos位之前的值视作sum,x是所有位上数的lcm。那么我们可以在dfs递归的时候传递sum%(x*n),这样就能在维护sum%x性质的同时,使sum变得很小。

x*n怎么取呢?1-9的最小公倍数是2520,显然它作为此处的x*n在空间复杂度上是支持的。

此时可以确定dp数组是三维的。dp[i][j][k],其代表枚举到第i位,前面所有位组成的数%2520的余数是j,前面所有位上的lcm是k的数位状态下符合要求的个数。

还有一个问题,20*2520*2520的数组是开不下的。但是第三维有大部分的数都是不会出现的,所以可以对2520的因数离散化,数量是小于50的,空间复杂度是可以承受的。

#include<bits/stdc++.h>
using namespace std;
const int maxn =;
typedef long long ll;
ll dp[maxn][][];
int a[maxn];
int dic[]; ll gcd(ll a,ll b)
{
if(b==) return a;
return gcd(b,a%b);
} void init()
{
int MOD=,cnt=;
for(int i=;i<=MOD;++i){
if(MOD%i==)
dic[i]=cnt++;
}
memset(dp,-,sizeof(dp));
} ll dfs(int pos,int mod=,int lcm = ,bool limit=true)
{
if (pos==-) return mod%lcm==;
int id =dic[lcm];
if(!limit && dp[pos][mod][id]!=-) return dp[pos][mod][id];
int up = limit?a[pos]:;
ll res=;
for(int i=;i<=up;++i){
if(i) res+=dfs(pos-,(mod*+i)%,lcm*i/gcd(lcm,i),limit && i==a[pos]);
else res+=dfs(pos-,(mod*)%,lcm,limit && i==a[pos]);
}
if(!limit) dp[pos][mod][id] = res;
return res;
} ll solve(ll n)
{
int pos=;
while(n){
a[pos++]=n%;
n/=;
}
return dfs(pos-);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
init();
int T;
scanf("%d",&T);
while(T--){
ll L,R;
scanf("%lld%lld",&L,&R);
printf("%lld\n",solve(R)-solve(L-));
}
return ;
}

Codeforces - 55D Beautiful numbers (数位dp+数论)的更多相关文章

  1. codeforces 55D - Beautiful numbers&lpar;数位DP&plus;离散化&rpar;

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

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

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

  3. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  4. codeforces 55D&period; Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  5. FZU2179&sol;Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  6. CF 55D&period; Beautiful numbers&lpar;数位DP&rpar;

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

  7. CodeForces 55D &quot&semi;Beautiful numbers&quot&semi;(数位DP&plus;离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  8. Codeforces 55D&period; Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers &lpar;数位DP&rpar;

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. SQL&lowbar;Server&lowbar;2008修改sa密码的方法

    转载自:http://blog.csdn.net/templar1000/article/details/20211191 1. 先用Window身份验证方式登陆进去,选择数据库实例,右键选择属性—— ...

  2. &period;offsetLeft&comma;&period;offsetTop

    *{ margin:0; padding:0} div {padding: 40px 50px;} #div1 {background: red;} #div2 {background: green; ...

  3. Xamarin&period;Forms介绍

    On May 28, 2014, Xamarin introduced Xamarin.Forms, which allows you to write user-interface code tha ...

  4. hdu 1713 相遇周期

    求分数的最小公倍数.对于a/b c/d 先化简为最简分数,分数最小公倍数=分子的最小公倍数/分母的最大公约数. ;}

  5. Linux curl命令参数详解--转载

    linux curl是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态. 一.Linux ...

  6. web端和手机端测试有什么不同

    面试中经常被问到web端测试和手机端测试有什么相同点和区别呢?现在总结一下这个问题,如有不对敬请指正 web端和手机端测试有什么区别 1.相同点 不管是web测试还是手机App测试,都离不开测试的相关 ...

  7. Mac OS&colon; How to keep network connection alive after sleep

    Do the following: Find out what the network interface is for your wifi. Mine is "en1" for ...

  8. bzoj3687

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 700  Solved: 319[Submit][Status][Discuss] ...

  9. 【原】Java程序调用远程Shell脚本

    此程序的目的是执行远程机器上的Shell脚本. [环境参数]远程机器IP:192.168.234.123用户名:root密码:rootShell脚本的路径:/home/IFileGenTool/Bak ...

  10. break与continue语句

    1.break:立即退出循环 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...