【XSY2719】prime 莫比乌斯反演

题目描述

  设\(f(i)\)为\(i\)的不同的质因子个数,求\(\sum_{i=1}^n2^{f(i)}\)

  \(n\leq{10}^{12}\)

题解

  考虑\(2^{f(i)}\)的意义:有\(f(i)\)总因子,每种可以分给两个人中的一个。那么就有\(2^{f(i)}=\sum_{d|i}[\gcd(d,\frac{i}{d})=1]\)

  然后就是简单莫比乌斯反演了。
\[
\begin{align}
s&=\sum_{i=1}^n\sum_{d|i}[\gcd(d,\frac{i}{d})=1]\&=\sum_{i=1}^n\sum_{d|i}\sum_{j|d\text{&&}j|\frac{i}{d}}\mu(j)\&=\sum_{i=1}^n\sum_{j^2|i}g(\frac{i}{j^2})\mu(j)\&=\sum_{j=1}^\sqrt{n}\mu(j)\sum_{i=1}^{\lfloor\frac{n}{j^2}\rfloor}g(i)\&=\sum_{j=1}^\sqrt{n}\mu(j)\sum_{i=1}^{\lfloor\frac{n}{j^2}\rfloor}\lfloor\frac{n}{j^2i}\rfloor
\end{align}
\]
  时间复杂度:\(O(\sqrt n\log n)\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll p=998244353;
ll gao(ll x)
{
    ll s=0;
    ll i,j;
    for(i=1;i<=x;i=j+1)
    {
        j=x/(x/i);
        s+=(x/i)*(j-i+1);
    }
    return s;
}
int b[1000010];
int pri[1000010];
int cnt;
int miu[1000010];
int main()
{
    ll i,j;
    miu[1]=1;
    for(i=2;i<=1000000;i++)
    {
        if(!b[i])
        {
            pri[++cnt]=i;
            miu[i]=-1;
        }
        for(j=1;j<=cnt&&i*pri[j]<=1000000;j++)
        {
            b[i*pri[j]]=1;
            if(i%pri[j]==0)
            {
                miu[i*pri[j]]=0;
                break;
            }
            miu[i*pri[j]]=-miu[i];
        }
    }
    ll ans=0;
    ll n;
    scanf("%lld",&n);
    for(i=1;i*i<=n;i++)
        ans=(ans+miu[i]*gao(n/(i*i)))%p;
    ans=(ans+p)%p;
    printf("%lld\n",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/ywwyww/p/8513531.html

时间: 2024-08-08 07:36:41

【XSY2719】prime 莫比乌斯反演的相关文章

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=

hdu1695(莫比乌斯反演)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意: 对于 a, b, c, d, k . 有 x 属于 [a, b],  y 属于 [c, d], 求 gcd(x, y) = k 的 x, y 的对数 . 其中 a = b = 1 . 注意: (x, y), (y, x) 算一种情况 . 思路: 莫比乌斯反演 可以参考一下: http://blog.csdn.net/lixuepeng_001/article/details/5057

bzoj2301 [HAOI2011]Problem b【莫比乌斯反演 分块】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2301 很好的一道题.首先把每个询问转化为4个子询问,最后的结果就是这四个子询问的记过加加减减,类似二维前缀和.那么问题转化为在1 <= x <= lmtx, 1 <= y <= lmty时gcd(x, y) == k的对数,这个问题在转化一下,转化成1 <= x <= lmtx / k,1 <= y <= lmty / k时x与y互质的对数.莫比乌斯反

BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演

分析:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 然后对于求这样单个的gcd(x,y)=k的,我们通常采用莫比乌斯反演 但是,时间复杂度是O(n*(n/k))的,当复杂度很坏的时候,当k=1时,退化到O(n^2),超时 然后进行分块优化,时间复杂度是O(n*sqrt(n)) #include<cstdio> #include<cstring> #include<queue

ACdream 1114(莫比乌斯反演)

传送门:Number theory 题意:给n个数,n 和 每个数的范围都是 1---222222,求n个数中互质的对数. 分析:处理出每个数倍数的个数cnt[i],然后进行莫比乌斯反演,只不过这里的F(i)=cnt[i]*(cnt[i]-1)/2. #pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <st

ACdream 1148(莫比乌斯反演+分块)

传送门:GCD SUM 题意:给出N,M执行如下程序:long long  ans = 0,ansx = 0,ansy = 0;for(int i = 1; i <= N; i ++)   for(int j = 1; j <= M; j ++)       if(gcd(i,j) == 1) ans ++,ansx += i,ansy += j;cout << ans << " " << ansx << " &qu

bzoj 2301 莫比乌斯反演

求$(i,j)=k$的一系列模板题之一. 但是这里i,j是有下界的,注意用容斥去掉重复组,其他都一样了. /** @Date : 2017-09-09 19:21:18 * @FileName: bzoj 2301 莫比乌斯反演 多组 范围内 GCD=k.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #inclu

bzoj 1101 [POI2007]Zap - 莫比乌斯反演

Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a ,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到你的帮助. Input 第一行包含一个正整数n,表示一共有n组询问.(1<=n<= 50000)接下来n行,每行表示一个询问,每行三个 正整数,分别为a,b,d.(1<=d<=a,b<=50000) Output 对于每组询问,输出到输出文件zap.out一个正

hdu_1695: GCD 【莫比乌斯反演】

题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就好了.注意要删去重复的. 关于 莫比乌斯反演 的结论 #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=1e6; int prime[maxn+5]; bool check[maxn+