Advertisement
bibaboba12345

Untitled

Nov 7th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. // clang-format off
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <bitset>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <random>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #include <deque>
  13. #include <cassert>
  14.  
  15. const int N = 2e5 + 7, A = 26, C = 2, MOD = 998244353;
  16. using namespace std;
  17. using ll = long long;
  18. using ld = long double;
  19.  
  20. const ld EPS = 1e-9;
  21.  
  22. long long b;
  23.  
  24. string n;
  25.  
  26. set<long long> dp;
  27.  
  28. long long cnt(int len, int b) {
  29. long long cnt = 9 / b;
  30. for (int i = 0; i < len-1; i++) {
  31. cnt *= (9 / b + 1);
  32. }
  33. return cnt;
  34. }
  35.  
  36. long long binpow(long long x, long long step) {
  37. if (step == 0) {
  38. return 1;
  39. }
  40. if (step % 2) {
  41. return x * binpow(x, step - 1);
  42. }
  43. long long h = binpow(x, step / 2);
  44. return h * h;
  45. }
  46.  
  47. long long cnt2(string s, int b) {
  48. long long answ = 0;
  49. for (int i = 0; i < s.size(); i++) {
  50. for (int c = 0; c < s[i] - '0'; c += b) {
  51. if (i == 0 && c == 0) {
  52. continue;
  53. }
  54. answ += binpow((9 / b + 1), s.size() - 1 - i);
  55. }
  56. if ((s[i] - '0') % b != 0) {
  57. break;
  58. }
  59. }
  60. return answ;
  61. }
  62.  
  63. void solve() {
  64. cin >> n >> b;
  65. long long answ = 0;
  66. for (int len = 1; len < n.size(); len++) {
  67. answ += cnt(len, b);
  68. }
  69. answ += cnt2(n, b);
  70. int f = 1;
  71. for (int i = 0; i < n.size(); i++) {
  72. if ((n[i] - '0') % b != 0) {
  73. f = 0;
  74. }
  75. }
  76. cout << answ + f<< "\n";
  77. }
  78.  
  79. signed main() {
  80. #ifdef _DEBUG
  81. freopen("input.txt", "r", stdin);
  82. #else
  83. std::ios::sync_with_stdio(false);
  84. cin.tie(0);
  85. #endif
  86. int t;
  87. cin >> t;
  88. while (t--) {
  89. solve();
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement