今天复习了一些数学方面的东西,线性筛欧拉是个很重要的东西,先贴代码,免得以后又忘了。

#include <iostream>
#include <cstdlib>
typedef long long LL;

int phi[(int)1e7], p[(int)1e7];
int t;

void Phi(int n) {
    for (int i = 2; i <= n; i++) {
        if(!phi[i]) phi[p[t++] = i] = i - 1;
        for (int j = 0; j < t && (LL)i * p[j] <= n; j++) {
            if(i % p[j] == 0) {phi[i * p[j]] = phi[i] * p[j]; break;}
            phi[i * p[j]] = phi[i] * (p[j] - 1);
        }
    }
}

int main() {
    int n;
    std::cin >> n;
    Phi(n);
    for (int i = 1; i <= n; i++) std::cout << phi[i] << ' ';
    system("pause");
    return 0;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
猜你喜欢
  • 2021-08-11
  • 2022-12-23
  • 2021-12-20
  • 2021-11-12
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
相关资源
相似解决方案