Advertisement
Dang_Quan_10_Tin

SPRIME TS10 Da Nang 2021-2022

Jan 6th, 2022
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #define task "SPRIME"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. constexpr int N = 3e6 + 5;
  12.  
  13. int q, cnt[N];
  14. bool IsComposite[N];
  15.  
  16. void Read()
  17. {
  18.     cin >> q;
  19. }
  20.  
  21. // TTổng các chữ số của v
  22. int Sum(int v)
  23. {
  24.     int ans(0);
  25.  
  26.     while (v)
  27.     {
  28.         ans += v % 10;
  29.         v /= 10;
  30.     }
  31.  
  32.     return ans;
  33. }
  34.  
  35. void Solve()
  36. {
  37.     for (int i = 2; i < N; ++i)
  38.     {
  39.         if (!IsComposite[i]) // Nếu i là số nguyên tố
  40.         {
  41.             if (Sum(i) % 5 == 0) // Nếu i là số đặc biệt
  42.                 cnt[i] = 1;
  43.  
  44.             for (int j = i; j < N; j += i)
  45.                 IsComposite[j] = 1;
  46.         }
  47.         cnt[i] += cnt[i - 1];
  48.     }
  49.  
  50.     while (q--)
  51.     {
  52.         int l, r;
  53.         cin >> l >> r;
  54.  
  55.         cout << cnt[r] - cnt[l - 1] << "\n";
  56.     }
  57. }
  58.  
  59. int32_t main()
  60. {
  61.     ios::sync_with_stdio(0);
  62.     cin.tie(0);
  63.     cout.tie(0);
  64.     if (fopen(task ".INP", "r"))
  65.     {
  66.         freopen(task ".INP", "r", stdin);
  67.         freopen(task ".OUT", "w", stdout);
  68.     }
  69.  
  70.     Read();
  71.     Solve();
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement