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;
}
#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;
}
相关文章推荐
- Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP
- CodeForces 55D Beautiful numbers(数位dp)
- CodeForces 55D. Beautiful numbers (思维+数位DP)
- 【codeforces】55D. Beautiful numbers 数位DP
- Codeforces 653B Bear and Compressing 【数位dp】
- codeforces-55D-Beautiful numbers(数位DP)
- Codeforces 55D Beautiful numbers(数位dp)
- codeforces--507D--The Maths Lecture(数位dp)
- Codeforces 55D - Beautiful numbers(数位dp)好
- [Codeforces 55D]Beautiful numbers(数位DP)
- 【数位DP】 【CodeForces 55D】
- codeforces 628D 数位dp
- Codeforces-55D Beautiful numbers (数位DP)
- Codeforces-55D - Beautiful numbers - 数位DP
- CodeForces - 258B King's Path(数位dp)
- CodeForces 55D Beautiful numbers(数位dp)
- CodeForces 54C-First Digit Law(数位,概率dp)
- [数位DP] Codeforces 809C. Find a car
- 【数位dp && 优化数组范围】CodeForces - 55D Beautiful numbers
- codeforces 55D - Beautiful numbers 数位DP+DFS