Advertisement
rengetsu

VGTU_ACM_ICPC_C

Oct 13th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. // VGTU ACM ICPC selection C
  2. #include <iostream>
  3. using namespace std;
  4. long long int ggg;
  5. long long int n, x;
  6. long long int pages(long long int nd, long long int first)
  7. {
  8.     long long int tenpow = 1;
  9.     int ndig = 0;
  10.     long long int t = first;
  11.     while(t > 0)
  12.     {
  13.         t = t/10; // 10
  14.         tenpow *= 10;
  15.         ndig += 1;
  16.     }
  17.     long long int sum = 0;
  18.     long long int last = first - 1 + nd / ndig; // ndig
  19.     while(last >= tenpow)
  20.     {
  21.         sum += tenpow - first;
  22.         nd -= (tenpow - first) * ndig;
  23.         first = tenpow;
  24.         tenpow *= 10;
  25.         ndig += 1;
  26.         last = first - 1 + nd / ndig; // ndig
  27.     }
  28.     if(nd % ndig > 0)
  29.     {
  30.         return -1;
  31.     }
  32.     return sum + (last - first + 1);
  33. }
  34. int main()
  35. {
  36.     cin >> ggg;
  37.     for(long long int i=0;i<ggg;i++)
  38.     {
  39.         cin >> n >> x;
  40.         cout << pages(n, x) << endl; // n - nd, x - first
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement