uva11426(莫比乌斯反演)
传送门:GCD Extreme (II)
题意:给定n(n<=4000000),求G
G=0
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
G+=gcd(i,j).
分析:这题本来应该欧拉函数预处理来解决,不过尝试一下莫比乌斯反演,没想到也AC了,复杂度O(nlog(n)),应该是题目100case中大数据不多,不然会超时吧。
设F(n)表示gcd(x,y)==n的倍数所有gcd之和,f(n)表示gcd(x,y)==n的所有gcd之和,那么反演有:
f(1)=mu(1)*F(1)+mu(2)*F(2)+...+mu(n)*F(n).
f(2)=mu(1)*F(2)+mu(2)*F(4)+...+mu(n/2)*F(n).
......
F(d)=(n/i)*(n/i-1)/2*d(其中i%d==0).
用筛素数的方法就可求出所有的f(i)了。
#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 4000000
#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=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
bool vis[N+];
int mu[N+],prime[N+],sum[N+],cnt[N+];
void Mobius()
{
memset(vis,false,sizeof(vis));
mu[]=;
int tot=;
for(int i=;i<=N;i++)
{
if(!vis[i])
{
prime[tot++]=i;
mu[i]=-;
}
for(int j=;j<tot;j++)
{
if(i*prime[j]>N)break;
vis[i*prime[j]]=true;
if(i%prime[j]==)
{
mu[i*prime[j]]=;
break;
}
else
{
mu[i*prime[j]]=-mu[i];
}
}
}
} int main()
{
int n;
Mobius();
while(scanf("%d",&n),n)
{
LL ans=;
for(int i=;i<=n;i++)
for(int j=i;j<=n;j+=i)
ans+=(LL)mu[j/i]*(n/j)*(n/j-)/*i;
printf("%lld\n",ans);
}
}
uva11426(莫比乌斯反演)的更多相关文章
- 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)
[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- hdu1695 GCD(莫比乌斯反演)
题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...
- BZOJ 2154: Crash的数字表格 [莫比乌斯反演]
2154: Crash的数字表格 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2924 Solved: 1091[Submit][Status][ ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- 莫比乌斯函数筛法 & 莫比乌斯反演
模板: int p[MAXN],pcnt=0,mu[MAXN]; bool notp[MAXN]; void shai(int n){ mu[1]=1; for(int i=2;i<=n;++i ...
- 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2371 Solved: 1143[Submit][Sta ...
- POI2007_zap 莫比乌斯反演
题意:http://hzwer.com/4205.html 同hdu1695 #include <iostream> #include <cstring> #include & ...
随机推荐
- perl中 wx返回的json需要encode_utf8($d);
$count is 9 now not support message 51 Wide character in print at /root/scanwx/lib/synccheck.pm line ...
- Mac 实用工具
命令行常用工具: Iterm2 也是一个终端命令行工具,支持多工作区,使用清爽 http://www.iterm2.com/documentation.html 给你的命令行 代码上色 Solariz ...
- Qt4_VS10 程序打包发布
源地址:http://www.2cto.com/kf/201306/217205.html 目录结构如下: ---------------------------------------------- ...
- C#面试-总结
1.override,overload的区别(鲁班联盟面试题) 笔试场景: 当时写反了区别 正确答案: override(重写,覆盖) 1.方法名.参数.返回值相同. 2.子类方法不能缩小父类方法的访 ...
- 基于visual Studio2013解决C语言竞赛题之1087数字变换
题目 解决代码及点评 /************************************************************************/ /* ...
- POJ3436 ACM Computer Factory 【最大流】
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5412 Accepted: 1 ...
- C# - 委托的使用
代码: using System; namespace Delegate { public class Program { //声明委托 public delegate void DelegatePa ...
- 内省(二)之BeanUtils工具类
上一篇内省(Introspector)讲到的是采用JavaAPI中的类来操作bean及其属性,而Apache也开源了第三方框架来简化和丰富了对bean属性的操作,这个框架就是BeanUtils. 使用 ...
- TWinControl.DefaultHandler处理WM_CTLCOLORMSGBOX..WM_CTLCOLORSTATIC消息的两个参数很有意思,两个都是传递句柄
procedure TWinControl.DefaultHandler(var Message); begin then begin with TMessage(Message) do begin ...
- Qt Style Sheets Examples(QT真是有很全的文档)
http://doc.qt.io/qt-5/stylesheet-examples.html http://doc.qt.io/qt-4.8/stylesheet.html