This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A
#include "nt/convolve.hpp"
#include "nt/totient.hpp"
#include "nt/moebius.hpp"
const int N = 1000000;
int f[N];
moebius mu(N);
auto phi = totient(N);
int main() {
iota(all(f), 0);
auto phi2 = convolve(all(f), all(mu));
rep2(i, 1, phi2.size()) assert(phi[i] == phi2[i]);
phi2 = convolve_multiplicative(all(f), all(mu));
rep2(i, 1, phi2.size()) assert(phi[i] == phi2[i]);
printf("Hello World\n");
}
#line 1 "test/nt/convolve.test.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A
#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 "types.hpp"
template <class It>
using val_t = typename iterator_traits<It>::value_type;
#line 3 "arith/sat.hpp"
template <class T, class U>
auto sat_add(T a, U b) {
using V = common_type_t<T, U>;
V res;
return __builtin_add_overflow((V)a, (V)b, &res)
? (a < 0 ? numeric_limits<V>::min() : numeric_limits<V>::max())
: res;
}
template <class T, class U>
auto sat_sub(T a, U b) {
using V = common_type_t<T, U>;
V res;
return __builtin_sub_overflow((V)a, (V)b, &res)
? (a < 0 ? numeric_limits<V>::min() : numeric_limits<V>::max())
: res;
}
template <class T, class U>
auto sat_mul(T a, U b) {
using V = common_type_t<T, U>;
V res;
return __builtin_mul_overflow((V)a, (V)b, &res)
? ((a < 0) == (b < 0) ? numeric_limits<V>::max()
: numeric_limits<V>::min())
: res;
}
#line 4 "nt/primes.hpp"
vector<bool> sieve(int upto) {
vector<bool> prime(upto + 1, true);
prime[0] = prime[1] = false;
for (int p = 2; p * p <= upto; p++)
if (prime[p])
for (int q = p * p; q <= upto; q += p) prime[q] = false;
return prime;
}
vector<int> primes(int upto) {
vector<int> ans;
vector<bool> sieved = sieve(upto);
for (int n = 2; n <= upto; n++)
if (sieved[n]) ans.push_back(n);
return ans;
}
bool is_prime(int n) {
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int d = 3; sat_mul(d, d) <= n; d += 2)
if (n % d == 0) return false;
return true;
}
#line 4 "nt/convolve.hpp"
// deg defaults to a_last - a
template <class It1, class It2>
auto convolve(It1 a, It1 a_last, It2 b, It2 b_last, int deg = -1) {
int n = a_last - a, m = b_last - b;
if (deg == -1) deg = n;
vector<decltype(a[0] * b[0])> res(deg);
rep2(i, 1, min(n, deg)) rep2(j, 1, m) {
if (i * j >= deg) break;
res[i * j] += a[i] * b[j];
}
return res;
}
// deg defaults to a_last - a
// a[1] = 1, a[nm] = a[n] * a[m] if gcd(n, m) == 1
template <class It1, class It2>
auto convolve_multiplicative(It1 a, It1 a_last, It2 b, It2 b_last,
int deg = -1) {
int n = a_last - a, m = b_last - b;
if (deg == -1) deg = n;
vector<decltype(a[0] * b[0])> res(deg);
copy(b, b + min(m, deg), res.begin());
for (auto p : primes(deg)) {
repr2(i, 1, (deg - 1) / p + 1) {
int n = p * i;
for (int q = p, m = i; ; q *= p, m /= p) {
res[n] += a[q] * res[m];
if (m % p != 0) break;
}
}
}
return res;
}
#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 "nt/moebius.hpp"
class moebius {
public:
moebius(int upto = 0) : mu(upto + 1, 1), prime(upto + 1, true) {
prime[0] = prime[1] = false;
for (int p = 2; p < size(); p++) if (prime[p]) {
mu[p] *= -1;
for (int m = p * 2; m < size(); m += p) mu[m] *= -1, prime[m] = false;
if (1LL * p * p < size()) {
for (int m = p * p; m < size(); m += p * p) mu[m] = 0;
}
}
}
int size() const { return mu.size(); }
int operator[](int i) const { return mu[i]; }
int operator()(int i) const { return mu[i]; }
bool is_prime(int i) const { return prime[i]; }
auto begin() const { return mu.cbegin(); }
auto end() const { return mu.cend(); }
private:
vector<int> mu;
vector<bool> prime;
};
#line 6 "test/nt/convolve.test.cpp"
const int N = 1000000;
int f[N];
moebius mu(N);
auto phi = totient(N);
int main() {
iota(all(f), 0);
auto phi2 = convolve(all(f), all(mu));
rep2(i, 1, phi2.size()) assert(phi[i] == phi2[i]);
phi2 = convolve_multiplicative(all(f), all(mu));
rep2(i, 1, phi2.size()) assert(phi[i] == phi2[i]);
printf("Hello World\n");
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-12 | judge_data |
![]() |
278 ms | 31 MB |