UKE求助
查看原帖
UKE求助
365948
WZY718605楼主2023/6/26 08:56
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
ll f[25][3005][55][2][2],ys[5005],bj[30005];
int tot;
int cnt,num[2005];
inline ll gcd(ll a,ll b){
    while(b){
        ll t=a; a=b; b=t%b;
    }
    return a;
}
inline ll lcm(ll a,ll b){
    if(a==0||b==0) return 0;
    return a*b/gcd(a,b);
}
inline void init(){
    ys[0]=0; ys[1]=1; tot=1; bj[1]=1;
    for (int i=1;i<(1<<8);i++){
        int j=i,l=1;
        for (int k=2;k<=9;k++){
            if(j&1) l=lcm(k,l);
            j=(j>>1);
        }
        if(bj[l]>0) continue;
        ys[++tot]=l; bj[l]=tot;
    }
    //cout<<tot<<endl;
}
inline ll dfs(int now,int mod1,int ys1,bool up,bool zer){
    //cout<<now<<" "<<mod1<<" "<<ys[ys1]<<" "<<up<<" "<<zer<<endl;
    if(f[now][mod1][ys1][up][zer]>-1) return f[now][mod1][ys1][up][zer];
    if(now==1){
        //cout<<ys1<<" "<<ys[ys1]<<endl;
        if(ys[ys1]>0&&mod1%ys[ys1]>0) return 0;
        if(zer) return 0;
        return 1;
    }
    ll res=0,l;
    for (int i=0;i<=(up?num[now-1]:9);i++){
        if(i>0) l=bj[lcm(ys[ys1],i)];
        else l=ys1;
        //cout<<ys[ys1]<<" "<<ys[l]<<" "<<i<<endl;
        res+=dfs(now-1,(mod1*10+i)%2520,l,up&&i==num[now-1],zer&&i==0);
    }
    return f[now][mod1][ys1][up][zer]=res;
}
inline ll solve(ll x){
    if(x<1) return 0;
    cnt=0;
    while(x){
        num[++cnt]=x%10; x=x/10;
    }
    //cout<<cnt<<endl;
    memset(f,-1,sizeof(f));
    return dfs(cnt+1,0,1,1,1);
}
int main(){
    init();
    ll l,r;
    int T; scanf("%d",&T);
    while(T--){
        scanf("%lld%lld",&l,&r);
        printf("%lld\n",solve(r)-solve(l-1));
    }

    //system("pause");
    return 0;
}
2023/6/26 08:56
加载中...