LightOJ-1007-Mathematically Hard [歐拉函數]


題目傳送門


題意:求下從a到b的所有數的歐拉函數的平方和。歐拉函數:小於n的與n互質的數的個數。

思路:預處理篩選,求前綴和。

#include <bits/stdc++.h>

using namespace std;

unsigned long long ans[5000100];
void init()
{
    ans[0] = 0;
    ans[1] = 1;
    for (int i = 2; i <= 5000000; i++)
        ans[i] = 0;
    for (int i = 2; i <= 5000000; i++)
    {
        if (!ans[i])
        {
            for (int j = i; j <= 5000000; j+=i)
            {
                if (!ans[j])
                    ans[j] = j;
                ans[j] = ans[j]/i*(i-1);
            }
        }
    }
    for (int i = 2; i <= 5000000; i++)
        ans[i]=ans[i]*ans[i]+ans[i-1];
}
int main(void)
{
    int T, cas=1;
    init();
    scanf("%d", &T);
    while (T--)
    {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("Case %d: %llu\n", cas++, ans[r]-ans[l-1]);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章