题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页。99就是两位数字,100就是三位数字。
思路:直接模拟即可,我用了一个high和一个low,因为我把数字按位数分成了几个部分,1-9,10-99,100-999等(实际上high会等于1000),这样就会low = 100,high = 999之类的了,如果遇到边界,比如刚开始时,low和high有所调整,比如low=x,high=999,以方便计算。
1 #include<iostream> 2 #include<algorithm> 3 #include<vector> 4 #include<stack> 5 #include<queue> 6 #include<map> 7 #include<set> 8 #include<cstdio> 9 #include<cstring> 10 #include<cmath> 11 #include<ctime> 12 #define fuck(x) cout<<#x<<" = "<<x<<endl; 13 #define ls (t<<1) 14 #define rs ((t<<1)+1) 15 using namespace std; 16 typedef long long ll; 17 typedef unsigned long long ull; 18 const int maxn = 100086; 19 const int inf = 2.1e9; 20 const ll Inf = 999999999999999999; 21 const int mod = 1000000007; 22 const double eps = 1e-6; 23 const double pi = acos(-1); 24 25 ll pre[20]; 26 ll num[20]; 27 ll get_n(ll x) 28 { 29 ll ans = 0; 30 while(x){ 31 x/=10; 32 ans++; 33 } 34 return ans; 35 } 36 37 int main() 38 { 39 int T; 40 scanf("%d",&T); 41 while(T--){ 42 ll n,x; 43 scanf("%lld%lld",&n,&x); 44 ll di =get_n(x); 45 ll low = x; 46 ll high=1; 47 for(int i=1;i<=di;i++){ 48 high*=10; 49 } 50 ll ans= 0; 51 for(int i=di;i<=16;i++){ 52 ll t = (high - low)*i; 53 if(t<=n){n-=t;ans+=high-low;} 54 else{ 55 if(n%i==0){ans+=n/i;} 56 else{ans=-1;}break; 57 } 58 low = high; 59 high*=10; 60 } 61 printf("%lld\n",ans); 62 } 63 return 0; 64 }