fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define ll unsigned long long
  5.  
  6. int main() {
  7. ll n;
  8. cin >> n;
  9. ll x1 = n, cnt = 0;
  10. // Считаем количество цифр
  11. while (x1) x1 /= 10, cnt++;
  12. int d[cnt], i = 1;
  13. x1 = n;
  14. // Заполняем массив цифр
  15. while (x1) d[cnt - i++] = x1 % 10, x1 /= 10;
  16. // Заменяем цифры после 8 на 799999...
  17. bool f = false;
  18. for (int i = 0; i < cnt; i++)
  19. if (f)
  20. d[i] = 9;
  21. else if (d[i] == 8)
  22. d[i] = 7, f = true;
  23. // Считаем степени девятки
  24. ll pow9[19];
  25. pow9[0] = 1;
  26. for (int i = 1; i <= 18; i++)
  27. pow9[i] = pow9[i - 1] * 9;
  28. // Считаем ответ
  29. ll res = 0;
  30. for (int i = cnt - 1; i >= 0; i--)
  31. res += pow9[cnt - i - 1] * (d[i] - (d[i] >= 8));
  32. cout << res <<endl;
  33. }
  34.  
Success #stdin #stdout 0s 15232KB
stdin
88888888888888
stdout
20334926626631