Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3. ifstream fin("nr_pal.in");
  4. ofstream fout("nr_pal.out");
  5. int n, a, b;
  6. int ncif(int n);
  7. int POW(int x, int n);
  8. int ogl(int n);
  9. int nrpal(int n);
  10. int main()
  11. {
  12. fin >> n;
  13. for (int i = 1; i <= n; ++i)
  14. {
  15. fin >> a >> b;
  16. fout << nrpal(b) - nrpal(a - 1) << '\n';
  17. }
  18. fin.close();
  19. fout.close();
  20. return 0;
  21. }
  22. int ncif(int n)
  23. {
  24. int rez = 0;
  25. if (n == 0)
  26. return 1;
  27. while (n > 0)
  28. {
  29. n /= 10;
  30. ++rez;
  31. }
  32. return rez;
  33. }
  34. int POW(int x, int n)
  35. {
  36. if (n == 0) return 1;
  37. int p = POW(x, n / 2);
  38. p *= p;
  39. if (n & 1)
  40. p *= x;
  41. return p;
  42. }
  43. int ogl(int n)
  44. {
  45. int oglindit = 0;
  46. while (n > 0)
  47. {
  48. oglindit = oglindit * 10 + n % 10;
  49. n /= 10;
  50. }
  51. return oglindit;
  52. }
  53. int nrpal(int n)
  54. {
  55. int c = ncif(n), npal = 1;
  56. if (c % 2 == 1)
  57. {
  58. npal += 2 * (POW(10, c / 2) - 1);
  59. int pal = n / POW(10, c / 2) * POW(10, c / 2) + ogl(n / POW(10, c / 2 + 1));
  60. if (n >= pal)
  61. npal += n / POW(10, c / 2) - POW(10, c / 2) + 1;
  62. else npal += n / POW(10, c / 2) - POW(10, c / 2);
  63. }
  64. else
  65. {
  66. npal += 2 * (POW(10, (c - 1) / 2) - 1) + 9 * POW(10, (c - 1) / 2);
  67. int pal = n / POW(10, c / 2) * POW(10, c / 2) + ogl(n / POW(10, c / 2));
  68. if (n >= pal)
  69. npal += n / POW(10, c / 2) - POW(10, c / 2 - 1) + 1;
  70. else npal += n / POW(10, c / 2) - POW(10, c / 2 - 1);
  71. }
  72. return npal;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement