zoukankan      html  css  js  c++  java
  • hoj2824

    欧拉函数的原始公式是用n连乘(1-1/pi)。pi是n的素因子。我们可以通过一个n^2级筛法的方式去筛,这样就保证了每个合数都会被其所有素因子筛一次,只要在筛的时候在其结果上乘上1-1/pi)即可。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    using namespace std;

    #define maxn 3000005

    double ans[maxn];
    bool prime[maxn];

    int next(int a)
    {
    a
    += 1;
    while (!prime[a] && a < maxn)
    a
    ++;
    if (!(a < maxn))
    return 0;
    return a;
    }

    int main()
    {
    //freopen("D:\\t.txt", "r", stdin);
    for (int i = 0; i < maxn; i++)
    {
    ans[i]
    = i;
    prime[i]
    = true;
    }
    for (long long i = 2; i != 0; i = next(i))
    {
    ans[i]
    *= (1 - 1.0 / i);
    for (long long j = 2; i * j < maxn; j++)
    {
    ans[i
    * j] *= (1 - 1.0 / i);
    prime[i
    * j] = false;
    }
    }
    int a, b;
    while (scanf("%d%d", &a, &b) != EOF)
    {
    long long sum = 0;
    for (int i = a; i <= b; i++)
    sum
    += ans[i];
    printf(
    "%I64d\n", sum);
    }
    return 0;
    }
  • 相关阅读:
    monit官方摘录
    monit配置文件
    monit检测语法
    monit介绍和配置
    ganglia-gmond.conf配置文件
    ganglia问题汇总
    ganglia使用nagios告警
    ganglia-gmetad 配置文件
    监控项目
    监控方案
  • 原文地址:https://www.cnblogs.com/rainydays/p/1988829.html
Copyright © 2011-2022 走看看