测了几百万组数据,应该是没错了。。。。。貌似是爆栈
#include <iostream> #include <algorithm> using namespace std; long long count2(long long n,long long p); long long count3(long long n,long long p); long long l,r,k; long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL}; long long c[]={-1,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL}; long long cc[20][4]; long long count1(long long n,long long p) {if(n<0) return 0; if(n<10) { return 1+n-(n>=p); } long long i=0; while(b[i+1]<=n) i++; long long a=n/b[i]; if(a==p){ return (n/b[i])*count1(c[i],p); } else if(a>p) { return (n/b[i]-1)*count1(c[i],p)+count1(n%b[i],p); } else{ return (n/b[i])*count1(c[i],p)+count1(n%b[i],p); } } long long f2(long long n,long long a2,long long d,long long p) { if(n<0) return 0; int m=n/b[d]; if(n/b[d]==0&&a2==0) return 0; if(n<10&&m!=0) return n+1-(n>=a2); if(m==0){ return count2(n,p); } else{ if(m>a2) { return (m-1)*count2(c[d],p)+count2(n%b[d],p); }else if(m==a2) { return (m)*count2(c[d],p); }else{ return m*count2(c[d],p)+count2(n%b[d],p); } } } long long count2(long long n,long long p) { if(n<0) return 0; if(n<p) return n+1; if(n<100) return n+1-(n>=p); int a=p/10; int a2=p%10; int i=0; while(b[i+1]<=n) i++; if(n/b[i]>a) { return (n/b[i]-1)*count2(c[i],p)+count2(n%b[i],p)+f2(c[i],a2,i-1,p); }else if(n/b[i]==a){ return (n/b[i])*count2(c[i],p)+f2(n%b[i],a2,i-1,p); }else { return (n/b[i])*count2(c[i],p)+count2(n%b[i],p); } } int f3(long long n,long long a1,long long a2,long long d,long long p ) {if(n<0) return 0; int m=n/b[d]; if(n/b[d]==0&&a2==0) return 0; if(n<10&&m!=0) return n+1-(n>=a2); if(m==0){ return count3(n,p); } else{ if(m>a2) { if(m>a1){ return (m-2)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p ); } else if(m==a1){ return (m-1)*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p ); } else{ return (m-1)*count3(c[d],p)+count3(n%b[d],p); } }else if(m==a2) { if(m>a1){ return (m-1)*count3(c[d],p)+f3(c[d],a1,a2,d-1,p); } else { return (m)*count3(c[d],p); } }else{ if(m>a1){ return (m-1)*count3(c[d],p)+count3(n%b[d],p)+f3(c[d],a1,a2,d-1,p); } else if(m==a1){ return m*count3(c[d],p)+f3(n%b[d],a1,a2,d-1,p); } else return m*count3(c[d],p)+count3(n%b[d],p); } } } long long count3(long long n,long long p) {if(n<0) return 0; if(n<p) return n+1; if(n<100) return n+1-(n>=p); int a=p/10; int a2=p%10; int i=0; while(b[i+1]<=n) i++; if(n/b[i]>a) { return (n/b[i]-1)*count3(c[i],p)+count3(n%b[i],p)+f3(c[i],a,a2,i-1,p); }else if(n/b[i]==a){ return (n/b[i])*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p); }else { return (n/b[i])*count3(c[i],p)+count3(n%b[i],p); } } long long countn(long long n,long long p) { if(n<0) return 0; long long d=0,m=p,res=0; while(m!=0) {m=m/10;d++;} if(d==1) { res=count1(n,p); }else{ int a=p/10; int b=p%10; if(a==b) res=count2(n,p); else res=count3(n,p); } return res; } int main() { int t; cin>>t; while(t--) { cin>>l>>r>>k; if(l>r) swap(l,r); cout<<r-l+1-countn(r,k)+countn(l-1,k)<<endl; } return 0; }