SPOJ PGCD(莫比乌斯反演)

传送门:Primes in GCD Table

题意:给定两个数,其中,求为质数的有多少对?其中的范围是

分析:这题不能枚举质数来进行莫比乌斯反演,得预处理出∑υ(n/p)(n%p==0).

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 10000000
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
    char ch=getchar();int x=0,f=1;
    while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch<=‘9‘&&ch>=‘0‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
bool vis[N+5];
int mu[N+5],prime[N+5],sum[N+5],num[N+5];
void Mobius()
{
    memset(vis,false,sizeof(vis));
    mu[1]=1;
    int tot=0;
    for(int i=2;i<=N;i++)
    {
        if(!vis[i])
        {
            prime[tot++]=i;
            mu[i]=-1;
        }
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>N)break;
            vis[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                break;
            }
            else
            {
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
    for(int i=0;i<tot;i++)
        for(int j=prime[i];j<=N;j+=prime[i])
        num[j]+=mu[j/prime[i]];//预处理出对于所有质数p,sigma(f(p))对应的F(i)的系数,用num[i]表示
    for(int i=1;i<=N;i++)sum[i]=sum[i-1]+num[i];
}
LL solve(int n,int m)
{
    LL res=0;
    if(n>m)swap(n,m);
    for(int i=1,last=0;i<=n;i=last+1)
    {
        last=min(n/(n/i),m/(m/i));
        res+=(LL)(sum[last]-sum[i-1])*(n/i)*(m/i);
    }
    return res;
}

int main()
{
    int T,n,m;
    Mobius();
    T=read();
    while(T--)
    {
        n=read();m=read();
        LL ans=solve(n,m);
        printf("%lld\n",ans);
    }
}

时间: 2024-12-17 04:34:41

SPOJ PGCD(莫比乌斯反演)的相关文章

bzoj 2820 / SPOJ PGCD 莫比乌斯反演

那啥bzoj2818也是一样的,突然想起来好像拿来当周赛的练习题过,用欧拉函数写掉的. 求$(i,j)=prime$对数 \begin{eqnarray*}\sum_{i=1}^{n}\sum_{j=1}^{m}[(i,j)=p]&=&\sum_{p=2}^{min(n,m)}\sum_{i=1}^{\lfloor\frac{n}{p}\rfloor}\sum_{j=1}^{\lfloor\frac{m}{p}\rfloor}[i⊥j]\newline&=&\sum_{p=

spoj 4491 莫比乌斯反演

题意: 给出a,b,求gcd(x,y)=prime的方案数,其中:1 <= x <= a && 1 <= y <= b 限制: 1 <= a,b <= 1e7 思路: 先把问题拆成一个一个来考虑,然后问题就变成gcd(x,y)=k的方案数. 设f(k)为gcd(x,y)=k的方案数, 设F(k)为gcd(x,y)为k的倍数的方案数,显然F(k)=floor(a/k)*floor(b/k). 由莫比乌斯反演得: f(k)=mu[1]*F[k]+mu[2]*

zoj 3435 spoj 7001 莫比乌斯反演

zoj 3435 题意: 给出3个数a,b,c, 定义一个立方体,这个立方体有a*b*c个点,每个点的坐标都是整数(x,y,z),求经过坐标(1,1,1)和另外任意一个点(x1,y1,z1)的不同的直线有多少条. 限制: 2 <= a,b,c <= 1e6; 有200组数据. 思路: 有3种情况: 1. x1,y1,z1都大于等于2: 问题就变成求1 <= x <= a-1 && 1 <= y <= b-1 && 1 <= z &l

SPOJ VLATTICE 莫比乌斯反演

题目链接:https://www.spoj.com/problems/VLATTICE/en/ VLATTICE - Visible Lattice Points Description Consider a NNN lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X

SPOJ 7001(莫比乌斯反演)

传送门:Visible Lattice Points 题意:0<=x,y,z<=n,求有多少对xyz满足gcd(x,y,z)=1. 设f(d) = GCD(a,b,c) = d的种类数 : F(n) 为GCD(a,b,c) = d 的倍数的种类数, n%a == 0 n%b==0 n%c==0. 即 :F(d) = (N/d)*(N/d)*(N/d); 则f(d) = sigma( mu[n/d]*F(n), d|n ) 由于d = 1 所以f(1) = sigma( mu[n]*F(n) )

SPOJ VLATTICE Visible Lattice Points 莫比乌斯反演 难度:3

http://www.spoj.com/problems/VLATTICE/ 明显,当gcd(x,y,z)=k,k!=1时,(x,y,z)被(x/k,y/k,z/k)遮挡,所以这道题要求的是gcd(x,y,z)==1的个数+{(x,y,0)|gcd(x,y)==1}的个数+3{(0,0,1),(0,1,0),(1,0,0)} 现在不去管最后的三个坐标轴上的点, 设f(i)=|{(x,y,0)|gcd(x,y)==i}|*3+|{(x,y,z)|gcd(x,y,z)==i}|,也就是不在坐标轴上且

【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)

[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n. Input The first line contains T the number of test cases. Each of the n

spoj 7001 Visible Lattice Points莫比乌斯反演

Visible Lattice Points Time Limit:7000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from co

[SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演

7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many lattice points are visible from corner at (0,0,0) ? A point X is visible from point Y iff no other lat