我把三种情况分开了。。。
好难写啊 特别是对于p是两位的 并且十位和个位不一样的情况
好难写啊 特别是对于p是两位的 并且十位和个位不一样的情况
#include <iostream> #include <algorithm> using namespace std; long long count2(int n,int p); long long count3(int n,int p); long long l,r,k; long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL}; long long c[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL}; long long count1(int n,int p) { if(n<0) return 0; if(n<10) { return 1+n-(n>=p); } int i=0; while(b[i+1]<n) i++; int a=n/b[i]; if(a==p){ return count1(n-b[i]+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(int n,int a2,int d,int p) { if(n/b[d]==0&&a2==0) return 0; if(n<10) return n+1-(n>=a2); int m=n/b[d]; 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(int n,int p) { if(n<p) return 0; 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)+f2(n%b[i],a2,i-1,p); }else if(n/b[i]==a){ return (n/b[i])*count2(c[i],p); }else { return (n/b[i]-1)*count2(c[i],p)+f2(n%b[i],a2,i-1,p); } } int f3(int n,int a1,int a2,int d,int p ) { if(n/b[d]==0&&a2==0) return 0; if(n<10) return n+1-(n>=a2); int m=n/b[d]; 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(n%b[d],a1,a2,d-1,p); } else { return (m)*count3(c[d],p); } }else{ if(m>a1){ return (m-1)*count3(c[d],p)+f3(n%b[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(int n,int p) { if(n<p) return 0; 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)+f3(n%b[i],a,a2,i-1,p); }else if(n/b[i]==a){ return (n/b[i])*count3(c[i],p); }else { return (n/b[i]-1)*count3(c[i],p)+f3(n%b[i],a,a2,i-1,p); } } long long countn(int n,int p) { if(n<0) return 0; long long d=0,m=p,res=0; while(m!=0) {m=m/10;d++;} if(d==1) { //cout<<p<<endl; 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; }
#include <iostream> #include <algorithm> using namespace std; long long count2(int n,int p); long long count3(int n,int p); long long l,r,k; long long b[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000LL}; long long c[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL}; long long count1(int n,int p) { if(n<0) return 0; if(n<10) { return 1+n-(n>=p); } int i=0; while(b[i+1]<n) i++; int a=n/b[i]; if(a==p){ return count1(n-b[i]+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(int n,int a2,int d,int p) { if(n/b[d]==0&&a2==0) return 0; if(n<10) return n+1-(n>=a2); int m=n/b[d]; 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(int n,int p) { 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(int n,int a1,int a2,int d,int p ) { if(n/b[d]==0&&a2==0) return 0; if(n<10) return n+1-(n>=a2); int m=n/b[d]; 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(n%b[d],a1,a2,d-1,p); } else { return (m)*count3(c[d],p); } }else{ if(m>a1){ return (m-1)*count3(c[d],p)+f3(n%b[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(int n,int p) { 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(int n,int p) { if(n<0) return 0; long long d=0,m=p,res=0; while(m!=0) {m=m/10;d++;} if(d==1) { //cout<<p<<endl; 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; }
#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[]={0,9,99,999,9999,99999,999999,9999999,99999999,999999999LL,9999999999LL,99999999999LL}; long long count1(long long n,long long p) { 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/b[d]==0&&a2==0) return 0; if(n<10) return n+1-(n>=a2); int m=n/b[d]; 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<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/b[d]==0&&a2==0) return 0; if(n<10) return n+1-(n>=a2); int m=n/b[d]; 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<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; }
#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; }
#include<stdio.h> int count(int a[][10], int p0, int p1, int n) { int b[8], bn, c = 0, i, m; if(n <= 0) return 0; for(m = n, bn = 0; m; m /= 10) b[bn++] = m % 10; for(m = --bn; m >= 0; m--) { for(i = 0; i < b[m]; i++) if(i != p0 || p1 && (m == bn || b[m + 1] != p1)) c += a[m][i]; if(b[m] == p0 && (!p1 || m < bn && b[m + 1] == p1)) break; } if( m < 0 && (bn || p1 || b[0] != p0) && (!bn || b[0] != p0 || p1 && b[1] != p1) ) c++; return n - c + 1; } int cal(int L, int R, int p) { int a[8][10], p0, p1, c, i, j; p0 = p % 10; p1 = p / 10; for(c = j = 0; j <= 9; j++) c += a[0][j] = p != j; for(i = 1; i < 8; i++) for(a[i][0] = c, c = 0, j = 0; j <= 9; c += a[i][j++]) a[i][j] = (j==p) ? 0 : (j==p1) ? a[i][0] - a[i-1][p0] : a[i][0]; return count(a, p0, p1, R) - count(a, p0, p1, L - 1); } int main() { int T, L, R, p; for(scanf("%d", &T); T--; printf("%d\n", cal(L, R, p))) scanf("%d%d%d", &L, &R, &p); return 0; }