CF 55D - Beautiful numbers(数位DP)
题意:
如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers。
求区间 [a,b] 中 Beautiful numbers 的个数。
分析:先分析出,2~9 的最大的最小公倍数是 2520({5,7,8,9}),先预处理出所有可能的最小公倍数m[c]
dp[i][d][c]表示长度i, 余数d,各位上的数的最小公倍数是m[c]的个数。
#include<cstdio>
#include<cstring>
#define mod 2520
ll dp[][][];
int bit[],m[],mnum;
int gcd(int a,int b){
int tmp;
while(a%b){
tmp=b;
b=a%b;
a=tmp;
}
return b;
}
int lcm(int a,int b){
return a*b/gcd(a,b);
}
//查最小公倍数的标号
int tfind(int x){
int ll=,rr=mnum;
while(ll<=rr){
int mid=(ll+rr)>>;
if(m[mid]<x)ll=mid+;
else rr=mid-;
}
return ll;
}
void init(){
memset(dp,-,sizeof(dp));
mnum=;
for(int i=;i<=mod;++i)
if(mod%i==)
m[++mnum]=i;
}
ll dfs(int i,int d,int c,int e){
if(i==)return d%m[c]?:;
if(!e&&dp[i][d][c]!=-)return dp[i][d][c];
int l=e?bit[i]:;
ll num=;
for(int j=;j<=l;++j)
{
int td=(d*+j)%mod;
int tc=c;
if(j)tc=tfind(lcm(m[c],j));
num+=dfs(i-,td,tc,e&&(j==l));
}
return e?num:dp[i][d][c]=num;
}
ll solve(ll x){
int len=;
while(x){
bit[++len]=x%;
x/=;
}
return dfs(len,,,);
}
int main()
{
int t;
scanf("%d",&t);
init();
ll x,y;
while(t--){
scanf("%I64d%I64d",&x,&y);
printf("%I64d\n",solve(y)-solve(x-));
}
return ;
}
CF 55D - Beautiful numbers(数位DP)的更多相关文章
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- 【数位dp】CF 55D Beautiful numbers
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
随机推荐
- REST Design Concerns
Less Requests, More data; one of the core RESTful API design paradigms is the concept of less API re ...
- CSS样式表引用方式
最近讲课中,有些学员对调用样式表老是有含糊不清?大体说来有四种方式: 1.外部文件引用方式;(推荐使用) 2.使用@import引用外部CSS文件; 3.内部文档头方式也叫内嵌法调用; 4.直接插入式 ...
- 【概率】poj 2096:Collecting Bugs
Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...
- Log4net Level
ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); l ...
- API断点大全
1.限制程序功能函数 EnableMenuItem 允许.禁止或变灰指定的菜单条目EnableWindow 允许或禁止鼠标和键盘控制指定窗口和条目(禁止时菜单变灰) 2.对话框函数 CreateDia ...
- PHP 5.3连接sql server 2008 R2
我的机器为: xp sp3 sql server 2008 developer apache 2.2.2 php 5.3 从5.3开始,php就不再提供mssql.dll了,所以要php连接sql ...
- hdu 3952
因为一个小错 不过 好不爽........ #include <iostream> #include <fstream> using namespace std; struc ...
- uva 108
降维 枚举行累加 然后求单行上最大连续和 #include <iostream> #include <cstring> #include <cstdio> # ...
- 改善用户体验之wordpress添加图片弹出层效果 (插件 FancyBox)
下面说说在改善用户体验之wordpress添加图片弹出层效果.效果图如下: 像这篇文章如何在百度搜索结果中显示网站站点logo? 文章内有添加图片,没加插件之前用户点击图片时,是直接_black打 ...
- 如何将DJANGO轻量级化
看看这本书,应该有收获. 不用DJANGO-ADMIN STARTPROJECT XXX 一个文件,一样可以写出可以运行的原生DJANGO哟. import hashlib import sys im ...