Gym 101915A(模拟)

传送门

题面:

A. Printing Books

time limit per test

2.0 s

memory limit per test

64 MB

input

standard input

output

standard output

Nour likes comic books so much that she decided to write her own comic book. Once she had this amazing idea, she started working on it immediately.

After a few days she finished her very first comic book. Since Nour is a workaholic, she wrote a huge comic book during these days with a lot of pages. Since Nour likes to be special in everything she started numbering her comic book from number X. In other words the first page has number X written on it, the second page has number X + 1 written on it, and so on.

Like we all know, Nour enjoys giving her friend Ahmed a hard time. That's why one day on their way to work, she asked her friend Ahmed the following question:

"Say Ahmed, can you guess the number of pages of my new comic book? I used a total number of digits equal to N, and started numbering my comic book from the number X, or say that such a thing is impossible. Since you are only an experimental physicist I'll give you an example: the number 99 has 2 digits on it. So if I wrote only 2 pages starting from the number 99, I would have used 5 digits to number my comic book".

Ahmed is so busy driving, and also he is really holding his nerves not to throw Nour out of the window right now. So he turned to you to help him solve this idiotic question from his annoying friend.

Input

The first line contains an integer T, the number of test cases.

Each line of the following T lines describes a single test case. Each test case contains 2 space separated integers N, X (1 ≤ N, X ≤ 1015).

Output

For each test case print a single line, containing a single integer, denoting the number of pages in Nour's comic book, or print -1 if such a thing is impossible.

Example

input

Copy

2
11 5
12 5

output

Copy

8
-1

题意:

    你从x这个数开始数,统计每个数i的位数,直到位数恰好等于n,问你位数恰好到n时,能够经过几个数字,如果不能完全经过数字的话则输出-1.

题目分析: 

    模拟即可,先将每一个数位上的最小的值,以及位数先预处理出来,之后枚举每一个位数,稍加判断模拟即可。

代码:

#include <bits/stdc++.h>
#define maxn 50
using namespace std;
typedef long long ll;
ll a[maxn],b[maxn];
void init(){
    a[1]=10,b[1]=1;
    for(int i=2;i<=20;i++){
        a[i]=a[i-1]*10;
        b[i]=b[i-1]+1;
    }
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--){
        ll n,x;
        ll ans=0;
        scanf("%lld%lld",&n,&x);
        for(int i=1;i<=20;i++){
            ll tmp=(a[i]-x)*b[i];
            if(x<=a[i]){
                if(n>=tmp){
                    n-=tmp;
                    ans+=a[i]-x;
                    x=a[i];
                }
                else{
                    if(n%b[i]==0) ans+=n/b[i];
                    else ans=-1;
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
}
原文地址:https://www.cnblogs.com/Chen-Jr/p/11007186.html