天啊,期末考终于结束了,真的好久没有打代码了!
今天日常感叹一句,fxy也太菜了吧!徐老师也太强了吧!
大意就是,求一个[l,r]区间里所有能被【自己所含有的数字】整除的数字的个数。
1.要用sum%(x*n)%x==sum%x来处理
2.最小公倍数的处理(二进制存不下丫
3.徐老师太强了
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
const int M=2520;
ll num[50];
ll hash[2600];
ll dp[20][2620][55];
ll gcd(ll a, ll b){
return b==0?a:gcd(b,a%b);
}
ll dfs(ll dig, ll top, ll rest, ll lcm){
if (dig==-1)return rest%lcm==0;
if (!top&&dp[dig][rest][hash[lcm]]!=-1) return dp[dig][rest][hash[lcm]];
ll upper= top?num[dig]:9;
ll ans=0;
for (int i=0;i<=upper;i++){
ans+=dfs(dig-1,(top&&i==num[dig]),(rest*10+i)%M,i==0?lcm:lcm*i/gcd(lcm,i));
}
if (!top) dp[dig][rest][hash[lcm]]=ans;
//cout<<dig<<" "<<top<<" "<<rest<<" "<<li<<" "<<res<<endl;
return ans;
}
ll solve(ll n){
memset(num,0,sizeof(num));
int di=-1;
while (n){
num[++di]=n%10;
n/=10;
}
return dfs(di,true,0,1);
}
void init(){
memset(hash,0,sizeof(hash));
int sum=0;
for (int i=1;i<=M;i++){
if (M%i==0) hash[i]=++sum;
}
}
int main(){
init();
int T;
memset(dp,-1,sizeof(dp));
cin>>T;
ll l,r;
while (T--){
cin>>l>>r;
cout<<solve(r)-solve(l-1)<<endl;
}
return 0;
}