zoukankan      html  css  js  c++  java
  • 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
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    《舌尖上的中国》精彩故事
    5年前的笔试题目
    遍历物理模型中的所有表,将表名、表代码、字段名、字段代码全部由小写改成大写
    MongoDB下载文件 百度盘共享
    认识MEAN开发框架[转]
    智能油田
    排课相关参数设置
    spring获取所有被装配类工具
    oracle常用sql集锦
    关于使用easyUI遇到过的一些坑
  • 原文地址:https://www.cnblogs.com/Tuesdayzz/p/5758620.html
Copyright © 2011-2022 走看看