cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: test/nt/totient.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2286

#include "nt/totient.hpp"
#include "iter/cumsum.hpp"

vector<int> phi = totient(1000000);
vector<ll> sum = cumsuml(all(phi), 0LL);

void solve() {
  int n; scanf("%d", &n);
  printf("%lld\n", sum[n + 1] + 1);
}

int main() {
  int T; scanf("%d", &T);
  while (T--) solve();
}
#line 1 "test/nt/totient.test.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2286

#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "nt/totient.hpp"

vector<int> totient(int upto) {
  vector<int> phi(upto + 1);
  iota(all(phi), 0);
  rep2(p, 2, phi.size()) {
    if (phi[p] == p) {
      for (int m = p; m < phi.size(); m += p)
        phi[m] -= phi[m] / p;
    }
  }
  return phi;
}
#line 3 "types.hpp"

template <class It>
using val_t = typename iterator_traits<It>::value_type;
#line 4 "iter/cumsum.hpp"

template <class It, class T = val_t<It>, class F = plus<T>>
vector<T> cumsuml(It first, It last, T e = T(), const F& f = F()) {
  vector<T> res{e};
  res.reserve(last - first + 1);
  while (first != last) res.push_back(e = f(e, *first++));
  return res;
}

template <class It, class T = val_t<It>, class F = plus<T>>
vector<T> cumsumr(It first, It last, T e = T(), const F& f = F()) {
  vector<T> res{e};
  res.reserve(last - first + 1);
  while (first != last) res.push_back(e = f(e, *--last));
  reverse(res.begin(), res.end());
  return res;
}
#line 5 "test/nt/totient.test.cpp"

vector<int> phi = totient(1000000);
vector<ll> sum = cumsuml(all(phi), 0LL);

void solve() {
  int n; scanf("%d", &n);
  printf("%lld\n", sum[n + 1] + 1);
}

int main() {
  int T; scanf("%d", &T);
  while (T--) solve();
}

Test cases

Env Name Status Elapsed Memory
g++-12 testcase_00 :heavy_check_mark: AC 123 ms 21 MB
Back to top page