codeforces 55d//Beautiful numbers// Codeforces Beta Round #51
题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数。
注意离散化,为了速度更快需存入数组查找。
不要每次memset,记录下已有的长度下符合条件的个数。
数位dp肯定是从高位到低位。
记录数字已经有多大,还有lcm,递归传下去。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
using namespace std;
const double EPS=1e-;
const int SZ=,INF=0x7FFFFFFF;
typedef long long lon;
lon dp[][][],index[];//
vector<lon> ls; lon gcd(lon x,lon y)
{
if(x<y)swap(x,y);
for(;;)
{
lon rem=x%y;
if(rem==)return y;
x=y;
y=rem;
}
} lon lcm(lon x,lon y)
{
return x*y/gcd(x,y);
} void getid()
{
for(lon i=;i<ls.size();++i)
{
index[ls[i]]=i;
}
} void lsh()
{
for(lon i=;i<pow(,);++i)
{
lon cur=;
for(lon j=;j<;++j)
{
if(i&(<<j))cur=lcm(cur,j+);
}
ls.push_back(cur);
}
sort(ls.begin(),ls.end());
ls.erase(unique(ls.begin(),ls.end()),ls.end());
getid();
} lon dfs(lon pos,lon cur,lon prelcm,lon limit,string &str)
{
if(pos==str.size())return cur%prelcm==;
if(!limit&&dp[str.size()-pos-][cur][index[prelcm]]!=-)return dp[str.size()-pos-][cur][index[prelcm]];
lon res=;
lon up=limit?str[pos]-'':;
for(lon i=;i<=up;++i)
{
lon curlcm=prelcm;
if(i)curlcm=lcm(i,curlcm);
res+=dfs(pos+,(cur*+i)%,curlcm,limit&&str[pos]-''==i,str);
}
if(!limit)dp[str.size()-pos-][cur][index[prelcm]]=res;
return res;
} lon work(string &str)
{
return dfs(,,,,str);
} bool chk(string &str)
{
lon num=,curlcm=;
for(lon i=;i<str.size();++i)
{
num=(num*+str[i]-'')%;
if(str[i]!='')curlcm=lcm(curlcm,str[i]-'');
}
return num%curlcm==;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon casenum;
cin>>casenum;
lsh();
memset(dp,-,sizeof(dp));
for(lon time=;time<=casenum;++time)
{
string ll,rr;
cin>>ll>>rr;
lon res=work(rr)-work(ll);
if(chk(ll))++res;
cout<<res<<endl;
}
return ;
}
codeforces 55d//Beautiful numbers// Codeforces Beta Round #51的更多相关文章
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位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
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 D. Beautiful numbers time limit per test 4 seconds me ...
- 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+数学)
题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
- CodeForces 55D Beautiful numbers(数位dp)
数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...
随机推荐
- js 数组操作
toString():把数组转换成一个字符串 toLocaleString():把数组转换成一个字符串 join():把数组转换成一个用符号连接的字符串 shift():将数组头部的一个元素移出 un ...
- 20145316许心远《网络对抗》EXP7网络欺诈技术防范
20145316许心远<网络对抗>EXP7网络欺诈技术防范 实验后回答问题 通常在什么场景下容易受到DNS spoof攻击 公共共享网络里,同一网段可以ping通的网络非常容易被攻击 在日 ...
- jquery基础框架
(function(window,undefined){ var arr = [], push = arr.push, slice = arr.slice; //首先要做的就是封装一个parseHtm ...
- 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别
1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用. 2.多个sessi ...
- Django框架介绍之cookie与session
cookie http请求时无状态的,一个客户端第一次,第二次,第n次访问同一个服务器都是一样的,服务器都会按照一个新的连接处理.但是,有时候客户端需要服务器记住客户端的登录状态,譬如离开一会,回来之 ...
- 在python3下使用OpenCV 显示图像
在Python3下用使用OpenCV比在C,C++里开发不止快捷一点点, 原型开发的时候蛮有用. 这里用的OpenCV 加载图片, 用的imshow画图 # -*- coding: utf-8 -*- ...
- Linux中tomcat日志按日期自动分割
Linux中tomcat日志分割需要用到cronolog 附上cronolog-1.6.2.tar.gz 1. 安装cronolog tar –zxvf cronolog-1.6.2.tar.gz . ...
- 计算TCP链接的RTO超时重传时间
- Python 代码片段收藏
list 列表相关 list 中最小值.最大值 import operator values = [1, 2, 3, 4, 5] min_index, min_value = min(enumerat ...
- Big Number-Asia 2002, Dhaka (Bengal) (计算位数)题解
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...