A - Printing Books (SDUT 2018 Autumn Individual Contest - F)

滴答滴答---题目链接

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

2
11 5
12 5

Output

8
-1
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int di(long long n)
{
    int c=0;

    while(n)
    {
        c++;
        n/=10;
    }
    return c;
}
int main()
{
    int t;
    int x;
    long long s,n,k;
    long long a[100];
    a[0]=0;
    for(int i=1; i<=17; i++)
        a[i]=a[i-1]*10+9;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&k,&n);
        x=di(n);
        s=0;
        while(1)
        {
            if(k>(a[x]-n+1)*x)
            {
                s+=a[x]-n+1;
                k-=(a[x]-n+1)*x;
                n=a[x]+1;
                x++;
            }
            else
            {
                if(k%x==0)
                {
                    printf("%lld\n",k/x+s);
                }
                else printf("-1\n");
                break;
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值