您的位置:首页 > 其它

codeforces 55d A.Beautiful numbers 数位DP -

2016-10-06 22:16 549 查看
题目地址:http://vjudge.net/contest/124282#problem/A

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
const int mod=2520;
LL d[25][mod][50];
map<int, int> idxLcm;
int digit[25];
void init()
{
int p=0;
for(int i=1;i<=mod;i++)
if(mod%i==0) idxLcm[i]=p++;
}
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
LL DFS(int pos,int premod,int prelcm,bool limit)
{
if(pos==-1) return premod%prelcm==0;

LL& ans=d[pos][premod][idxLcm[prelcm]];
if(!limit&&ans!=-1) return ans;
LL temp=0;
int up=limit?digit[pos]:9;
for(int i=0;i<=up;i++){
int newmod=(premod*10+i)%mod;
int newlcm=prelcm;
if(i) newlcm=lcm(prelcm,i);
temp+=DFS(pos-1,newmod,newlcm,limit&&i==digit[pos]);
}
if(!limit) ans=temp;
return temp;

}
LL solve(LL x){
int len=0;
while(x){
digit[len++]=x%10;
x/=10;
}
digit[len]=0;
return DFS(len-1,0,1,true);
}
int main(int argc, char const *argv[])
{
init();
LL x,y,t; cin>>t;
memset(d,-1,sizeof(d));
while(t--){
cin>>x>>y;
cout<<solve(y)-solve(x-1)<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: