codeforces 55d//Beautiful numbers// Codeforces Beta Round #51

时间:2024-09-22 00:05:32

题意:一个数能整除它所有的位上的数字(除了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 ;
}