SPOJ 7001 VLATTICE【莫比烏斯反演】

題目鏈接:

http://www.spoj.com/problems/VLATTICE/

題意:

1x,y,zn ,問有多少對(x,y,z) 使得gcd(x,y,z)=1

分析:

歐拉搞不了了,我們用莫比烏斯來搞一搞。
同樣,我們設
f(d) :滿足gcd(x,y,z)=dx,y,z 均在給定範圍內的(x,y,z) 的對數。
F(d) :滿足d|gcd(x,y,z)x,y,z 均在給定範圍內的(x,y,z) 的對數。
顯然F(d)=[n/d][n/d][n/d] ,反演後我們得到

f(x)=x|dμ(d/x)[n/d][n/d][n/d]

直接求解f(1) 即可。
特別注意座標軸上的點和座標平面上的點。

代碼:

/*
-- SPOJ 7001
-- mobius
-- Create by jiangyuzhu
-- 2016/5/30
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stack>
using namespace std;
typedef long long ll;
#define sa(n) scanf("%d", &(n))
#define sal(n) scanf("%I64d", &(n))
#define pl(x) cout << #x << " " << x << endl
#define mdzz cout<<"mdzz"<<endl;
const int maxn = 1e6 + 5 ;
int tot = 0;
int miu[maxn], prime[maxn], f[maxn];
bool flag[maxn];
void mobius()
{
    miu[1] = 1;
    tot = 0;
    for(int i = 2; i < maxn; i++){
        if(!flag[i]){
            prime[tot++] = i;
            miu[i] = -1;
        }
        for(int j = 0; j < tot && i * prime[j] < maxn; j++){
            flag[i * prime[j]] = true;
            if(i % prime[j]) miu[i * prime[j]] = -miu[i];
            else{
                miu[i * prime[j]] = 0;
                break;
            }
        }
    }
}
int main (void)
{
    mobius();
    int T;sa(T);
    int n;
    for(int kas = 1; kas <= T; kas++){
       scanf("%d", &n);
       ll ans = 3;
       for(int i = 1; i <= n; i++){
         ans += miu[i] * 1ll * (n/ i) * (n / i) * (n / i + 3);
       }
       printf("%lld\n", ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章