fork(2) download
  1. /*__ _(_) __ _ ___ ___ _ _ __| | __ _ _ _| |_ ___
  2.  / _` | |/ _` |/ _ \/ __| | | |/ _` |/ _` | | | | __/ _ \
  3. | (_| | | (_| | (_) \__ \ |_| | (_| | (_| | |_| | || (_) |
  4.  \__, |_|\__,_|\___/|___/\__,_|\__,_|\__,_|\__,_|\__\___/
  5.  |___/ Accepted Code */
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. int64_t mu(int64_t a, int64_t b)
  10. {
  11. int res=1;
  12. for (int i=1; i<=b; i++)
  13. res*=a;
  14. return res;
  15. }
  16.  
  17. int main()
  18. {
  19. int T;
  20. scanf("%d",&T);
  21. while(T--)
  22. {
  23. int64_t k,m;
  24. cin>>k>>m;
  25.  
  26. int64_t a=k;
  27. int64_t pos=0;
  28.  
  29. while(1)
  30. {
  31. pos+=a-mu(10,int(log10(a)))+1;
  32. a/=10;
  33. if(a==0)break;
  34. }
  35. if(m<pos)
  36. {
  37. printf("0\n");
  38. continue;
  39. }
  40. if(m==pos)
  41. {
  42. cout<<k<<endl;
  43. continue;
  44. }
  45. if(k==mu(10,int(log10(k))))
  46. {
  47. printf("0\n");
  48. continue;
  49. }
  50. a=k-1;
  51. while(1)
  52. {
  53. if(pos+a*10+9-mu(10,int(log10(a*10+9)))+1<=m)
  54. {
  55. a=a*10+9;
  56. pos+=a-mu(10,int(log10(a)))+1;
  57. if(pos==m)
  58. {
  59. cout<<a<<endl;
  60. break;
  61. }
  62. }
  63. else
  64. {
  65. a=mu(10,int(log10(a*10)))+m-pos-1;
  66. cout<<a<<endl;
  67. break;
  68. }
  69. }
  70. }
  71.  
  72. return 0;
  73. }
  74.  
Success #stdin #stdout 0s 3464KB
stdin
1
2 4
stdout
11