• 「Luogu3455」[POI2007]ZAP-Queries


    「Luogu3455」[POI2007]ZAP-Queries

    problem

    Solution

    题目要求

    [sum_{x=1}^asum_{y=1}^b[gcd(a,b)=d] ]

    设上式为(f(d)),构造(g(n)=sum_{n|d}f(d)),于是有

    [g(n)=sum_{x=1}^asum_{y=1}^b[n|gcd(a,b)] ]

    易得

    [g(d)=lfloorfrac{a}{d} floorlfloorfrac{b}{d} floor ]

    代入反演一下

    [f(n)=sum_{n|d}mu(frac{d}{n})lfloorfrac{a}{d} floorlfloorfrac{b}{d} floor ]

    (t=frac{d}{n}),代入得

    [f(n)=sum_{t=1}^frac{min(a,b)}{n}mu(t)lfloorfrac{a}{nt} floorlfloorfrac{b}{nt} floor ]

    预处理(mu)的前缀和,整除分块即可

    Code

    #include <cstdio>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #define maxn 50005
    #define N 50000
    using namespace std;
    typedef long long ll;
    
    template <typename T> void read(T &t)
    {
        t=0;int f=0;char c=getchar();
        while(!isdigit(c)){f|=c=='-';c=getchar();}
        while(isdigit(c)){t=t*10+c-'0';c=getchar();}
        if(f)t=-t;
    }
    
    int n;
    int pri[maxn],pcnt,nop[maxn],mu[maxn];
    int a,b,k;
    
    void GetPrime()
    {
        mu[1]=1,nop[1]=1;
        for(register int i=2;i<=N;++i)
        {
            if(!nop[i])pri[++pcnt]=i,mu[i]=-1;
            for(register int j=1;j<=pcnt && i*pri[j]<=N;++j)
            {
                nop[i*pri[j]]=1;
                if(i%pri[j]==0)break;
                else mu[i*pri[j]]=-mu[i];
            }
        }
        for(register int i=1;i<=N;++i)mu[i]+=mu[i-1];
    }
    
    int Calc()
    {
        int re=0,up=min(a,b)/k;
        for(register int l=1,r;l<=up;l=r+1)
        {
            r=min(a/(a/l),b/(b/l));
            re+=(mu[r]-mu[l-1])*(a/(l*k))*(b/(l*k));
        } 
        return re;
    }
    
    int main()
    {
        GetPrime();
        read(n);
        while(n--)
        {
            read(a),read(b),read(k);
            printf("%d
    ",Calc());
        }
        return 0;
    }
    
  • 相关阅读:
    Linux 模块管理
    python 调试方法
    LFS(Linux From Scratch)学习
    Vim完全教程
    OpenSSL基础知识
    关于jiffies回绕以及time_after,time_before
    十分简便的APK反编译(Mac 版本号 具体解释)
    亚马逊是怎样颠覆商业软件高昂价格这座”柏林墙”的
    Android自己定义控件
    Android基础新手教程——4.1.1 Activity初学乍练
  • 原文地址:https://www.cnblogs.com/lizbaka/p/10513433.html
Copyright © 2020-2023  润新知