Iamtui1010

prime

Nov 29th, 2021
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<vector>
  4. #include<fstream>
  5. #include<cmath>
  6.  
  7. #define long long long
  8. #define nln '\n'
  9.  
  10. const long N = 1e6+10;
  11.  
  12. using namespace std;
  13.  
  14. // Global variables: q, a, b, k
  15.  
  16. long q;
  17. vector<long> a, b, k;
  18. fstream f1;
  19.  
  20. void data()
  21. {
  22.     f1.open("prime.inp", ios:: in);
  23.     cin >> q;
  24.     a.resize(q+1, 0);
  25.     b.resize(q+1, 0);
  26.     k.resize(q+1, 0);
  27.     for (long i = 1; i <= q; ++i)
  28.         cin >> a[i] >> b[i] >> k[i];
  29.     f1.close();
  30. }
  31.  
  32. vector<long> sie(N, 1), cou(N, 0);
  33.  
  34. void eratosthenes()
  35. {
  36.     sie[0] = 0;
  37.     sie[1] = 0;
  38.  
  39.     for (long i = 2; i < N; ++i)
  40.     {
  41.         if (sie[i])
  42.         {
  43.             cou[i] = 1;
  44.             for (long j = 2*i; j < N; j += i)
  45.             {
  46.                 sie[j] = 0;
  47.                 ++cou[j];
  48.             }
  49.         }
  50.     }  
  51. }
  52.  
  53. long tot[8][N];
  54.  
  55. void cumulative_total()
  56. {
  57.     for (long i = 2; i < N; ++i)
  58.     {
  59.         ++tot[cou[i]][i];
  60.         for (long j = 1; j <= 7; ++j)
  61.             tot[j][i] += tot[j][i-1];
  62.     }
  63. }
  64.  
  65. vector<long> ans(1, 0);
  66.  
  67. void find()
  68. {
  69.     for (long i = 1; i <= q; ++i)
  70.         ans.push_back(tot[k[i]][b[i]]-tot[k[i]][a[i]-1]);
  71. }
  72.  
  73. void process()
  74. {
  75.     eratosthenes();
  76.     cumulative_total();
  77.     find();
  78. }
  79.  
  80. void view()
  81. {
  82.     for (long i = 1; i <= q; ++i)
  83.         cout << ans[i] << nln;
  84. }
  85.  
  86. int main()
  87. {
  88.     data();
  89.     process();
  90.     view();
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment