newb_ie

LightOJ - Mathematically Hard

Mar 19th, 2021 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 5 * 1e6 + 1;
  5. unsigned long long phi[maxN];
  6.  
  7. void preComputePhi () {
  8.     for (unsigned long long i = 1; i < maxN; ++i) phi[i] = i;
  9.     for (unsigned long long i = 2; i < maxN; ++i) {
  10.         if (phi[i] == i) {
  11.             for (unsigned long long j = i; j < maxN; j += i) {
  12.                 phi[j] -= phi[j] / i;
  13.             }
  14.         }
  15.     }
  16.     for (int i = 1; i < maxN; ++i) phi[i] = phi[i] * phi[i];
  17.     for (int i = 1; i < maxN; ++i) phi[i] += phi[i - 1];
  18. }
  19.  
  20. int main () {
  21.      ios::sync_with_stdio(false);
  22.      cin.tie(nullptr) ;
  23.      cout.tie(nullptr);
  24.      int T;
  25.      cin >> T;
  26.      preComputePhi();
  27.      for (int test_case = 1; test_case <= T; ++test_case) {
  28.          int a,b;
  29.          cin >> a >> b;
  30.          cout << "Case " << test_case << ": " << (phi[b] - phi[a - 1]) << '\n';
  31.      }
  32.      //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  33. }
  34.  
Add Comment
Please, Sign In to add comment