FZU2179/Codeforces 55D beautiful number 数位DP
题目大意:
求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字
思路:
整除每一位。只需要整除每一位的lcm即可
但是数字太大,dp状态怎么表示呢
发现 1~9的LCM 是2520 ....也就是说只要对这个数mod2520 剩下的余数能整除lcm就可以整除了。。
计数的时候还有一个技巧,具体见注释
此外这个题还卡常数了,预处理lcm才过了。。
代码如下:
#include <iostream>
#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<ctype.h>
using namespace std;
#define LCM 2520
long long dp[][LCM+][][];
long long n;
int m;
int state[],hs[],s;
bool vi[];
int d[];
int Lcm[][];
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int lcm(int a,int b)
{
if(b==)
return a;
return a/gcd(a,b)*b;
} void ini()
{
m=;
while(n)
{
d[++m]=n%;
n/=;
}
reverse(d+,d+m+);
}
void DP()
{
memset(dp,,sizeof(dp));
//先放最高位
for(int i=;i<d[];i++)
{
dp[][i][hs[i]][]=; //dp...[0]存储的是前i位的数小于所求数的方案
}
dp[][d[]][hs[d[]]][]=; //dp...[1]存储的是前i位的数等于所求数的方案
//从高位往下转移
for(int i=;i<=m;++i)
{
for(int j=;j<;++j)
dp[i][j][hs[j]][]=; //从大于1的数位开始放1~9都肯定小于所求数
for(int j=;j<;++j)
{
for(int k=;k<s;++k)
{
if(dp[i-][j][k][]==&&dp[i-][j][k][]==)
continue;
for(int t=;t<=;++t)
{
int sum=(j*+t)%LCM;
int L=Lcm[k][t];
dp[i][sum][hs[L]][]+=dp[i-][j][k][]; //在当前位放的数小于等于所求数的当前为数的情况可以继承高位都等于所求数的方案
//否则,只能继承高位小于所求数的方案
if(t<d[i]) //当前位放的数小于所求数的当前位
{
dp[i][sum][hs[L]][]+=dp[i-][j][k][];
}
if(t==d[i]) //当前位放的数等于所求数的当前位
{
dp[i][sum][hs[L]][]+=dp[i-][j][k][];
} }
}
}
}
}
void setstate()
{
memset(vi,,sizeof(vi));
s=;
int tmp;
for(int i=;i<(<<);i++)
{
tmp=;
for(int j=;j<;j++)
{
if((<<j)&i)
{
tmp=lcm(j+,tmp);
}
}
if(vi[tmp]==)
{
state[s]=tmp;
hs[tmp]=s++;
vi[tmp]=;
}
}
for(int i=;i<s;i++)
{
for(int j=;j<=;j++)
Lcm[i][j]=lcm(state[i],j);
}
}
long long cal()
{
long long ans=; for(int i=;i<;i++)
{
for(int j=;j<s;j++)
{
if(i%state[j]==)
ans+=dp[m][i][j][]+dp[m][i][j][];
}
}
return ans;
}
long long solve(long long x)
{
n=x;
ini();
DP();
return cal();
}
int main()
{
long long a,b;
setstate();
int t;
scanf("%d",&t);
while(t--)
{
//scanf("%I64d%I64d",&a,&b);
scanf("%I64d",&a);
//cout<<solve(b)-solve(a-1)<<endl;
cout<<solve(a)<<endl;
}
return ;
}
FZU2179/Codeforces 55D beautiful number 数位DP的更多相关文章
- 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+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- 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
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- beautiful number 数位DP codeforces 55D
题目链接: http://codeforces.com/problemset/problem/55/D 数位DP 题目描述: 一个数能被它每位上的数字整除(0除外),那么它就是beautiful nu ...
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
- HDU 5179 beautiful number 数位dp
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5179 bc(中文): http://bestcoder.hdu.edu.cn/contes ...
随机推荐
- [Angular 2] Mapping Streams to Values to Affect State
While you have multiple streams flowing into your scan operator, you'll need to map each stream to t ...
- C++中struct和class的区别 [转]
一. C++中的struct对C中的struct进行了扩充,它已经不再只是一个包含不同数据类型的数据结构了,它已经获取了太多的功能. struct能包含成员函数吗? 能! struct能继承吗? ...
- apk文件伪装zip64格式案例
软件样本:http://files.cnblogs.com/files/mmmmar/FMRMemoryCleaner.apk 在论坛看在网友求助把一个小的app去广告,下载一看是清理内存的,刚开始让 ...
- 综合使用LruCache和DiskLruCache 缓存图片
Activity public class MainActivity extends Activity { private GridView mPhotoWall; private P ...
- wildfly部署solr.war
1.添加solr/home配置,有两种途径: 一个是修改solr.war包的web.xml,路径如下:solr-4.7.2.rar\WEB-INF\web.xml,添加如下内容:
- php代码结尾不要添加结尾标记
如果文件内容是纯 PHP 代码,最好在文件末尾删除 PHP 结束标记.这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图.
- Java中如何判断当前环境是大端字节顺序还是小端字节顺序
Java非字节类型的基本类型,除了布尔型都是由组合在一起的几个字节组成的.这些数据类 型及其大小总结在表 2-1 中. 表:基本数据类型及其大小 数据类型 大小(以字节表示) Byte 1 Char ...
- LINQ动态查询类--[DynamicLinqExpressions]
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.L ...
- 武汉科技大学ACM:1008: 零起点学算法64——回型矩阵
Problem Description 输出n*m的回型矩阵 Input 多组测试数据 每组输入2个整数 n和m(不大于20) Output 输出n*m的回型矩阵,要求左上角元素是1,(每个元素占2个 ...
- X-Y Problem
X-Y Problem 对于X-Y Problem的意思如下: 1)有人想解决问题X2)他觉得Y可能是解决X问题的方法3)但是他不知道Y应该怎么做4)于是他去问别人Y应该怎么做? 简而言之,没有去问怎 ...