【莫比乌斯反演】51nod1594 Gcd and Phi
题解
显然可以O(nlogn)计算
代码
//by 减维
#include<set>
#include<map>
#include<queue>
#include<ctime>
#include<cmath>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define il inline
#define rg register
#define db double
#define mpr make_pair
#define maxn 2000005
#define inf (1<<30)
#define eps 1e-8
#define pi 3.1415926535897932384626L
using namespace std; inline int read()
{
int ret=;bool fla=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-'){fla=;ch=getchar();}
while(ch>=''&&ch<=''){ret=ret*+ch-'';ch=getchar();}
return fla?-ret:ret;
} int t,n,num,mu[maxn],pri[maxn],phi[maxn];
ll cnt[maxn],sum[maxn];
bool pd[maxn]; void pre()
{
phi[]=;mu[]=;
for(int i=;i<=maxn-;i++)
{
if(!pd[i]) pri[++num]=i,phi[i]=i-,mu[i]=-;
for(int j=;j<=num&&i*pri[j]<=maxn-;++j)
{
pd[i*pri[j]]=;
if(i%pri[j]==)
{
phi[i*pri[j]]=phi[i]*pri[j];
break ;
}
phi[i*pri[j]]=phi[i]*phi[pri[j]];
mu[i*pri[j]]=-mu[i];
}
}
} il ll gcd(ll x,ll y){return y==?x:gcd(y,x%y);} ll solve(int x)
{
memset(cnt,,sizeof cnt);
memset(sum,,sizeof sum);
for(int i=;i<=x;++i) cnt[phi[i]]++;
for(int i=;i<=x;++i)
for(int j=;i*j<=x;++j) sum[i]+=cnt[i*j];
ll ret=;
for(int d=;d<=x;++d)
if(mu[d])
for(int dd=;dd*d<=x;++dd)
ret+=phi[dd]*mu[d]*sum[d*dd]*sum[d*dd];
return ret;
} int main()
{
t=read();
pre();
while(t--)
{
n=read();
printf("%lld\n",solve(n));
}
return ;
}
【莫比乌斯反演】51nod1594 Gcd and Phi的更多相关文章
- 数学:莫比乌斯反演-GCD计数
Luogu3455:莫比乌斯反演进行GCD计数 莫比乌斯反演就是用来解决这一类问题的,通常f函数是要求的那个,F函数是显然的 这样利用F的结果就可以推出来f的结果 在计算结果的时候整除分快儿一下就可以 ...
- ZOJ 3435 Ideal Puzzle Bobble 莫比乌斯反演
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4119 依然是三维空间内求(1,1,1)~(a,b,c)能看到的整点数,平移一下 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- 【Project Euler】530 GCD of Divisors 莫比乌斯反演
[题目]GCD of Divisors [题意]给定f(n)=Σd|n gcd(d,n/d)的前缀和F(n),n=10^15. [算法]莫比乌斯反演 [题解]参考:任之洲数论函数.pdf 这个范围显然 ...
- 洛谷P2568 GCD (欧拉函数/莫比乌斯反演)
P2568 GCD 题目描述 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入输出格式 输入格式: 一个整数N 输出格式: 答案 输入输出样例 输入 ...
- ACM学习历程—HYSBZ 2818 Gcd(欧拉函数 || 莫比乌斯反演)
Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Sample Input 4 Sam ...
- 洛谷 - SP3871 GCDEX - GCD Extreme - 莫比乌斯反演
易得 $\sum\limits_{g=1}^{n} g \sum\limits_{k=1}^{n} \mu(k) \lfloor\frac{n}{gk}\rfloor \lfloor\frac{n}{ ...
- 【BZOJ2818】Gcd(莫比乌斯反演,欧拉函数)
题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对 1<=N<=10^7 思路:莫比乌斯反演,同BZOJ2820…… ; ..max]of ...
- BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 2534 Solved: 1129 [Submit][Status][Discu ...
随机推荐
- php post提交xml文件
<?php header("Content-type: text/xml;"); // xml code demo $xmlData = '<?xml version= ...
- 用C#实现WEB代理服务器
用C#实现Web代理服务器 代理服务程序是一种广泛使用的网络应用程序.代理程序的种类非常多,根据协议不同可以分成HTTP代理服务程序.FTP代理服务程序等,而运行代理服务程序的服务器也就相应称为HTT ...
- MySQL数据库查看数据表占用空间大小和记录数
MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...
- python3 练习题100例 (二十六)回文数判断
题目内容: 给一个5位数,判断它是不是回文数,是则输出yes,不是则输出no. 例如12321是回文数,它的个位与万位相同,十位与千位相同. 输入格式: 共一行,为一个5位数. 输出格式: 共一行,y ...
- ESP32 学习笔记 - 环境搭建
打开终端 输入命令 sudo apt-get install gcc git wget make libncurses-dev flex bison gperf python python-seria ...
- maven 添加自己下载的jar包到本地仓库
1.在pom文件中添加依赖,其中groupId等变量都自拟. 例如: 2.在命令行执行以下命令,提示build success即表示安装成功. mvn install:install-file -Dg ...
- react-router 4.0中跳转失灵
在https://github.com/ReactTraining/history文档中,跳转是 用这种方法,但是,用了之后就存在这么一个问题,网址换了但是页面并没有刷新. 查了资料后,history ...
- TCD产品技术参考资料
1.Willis环 https://en.wikipedia.org/wiki/Circle_of_Willis 2.TCD仿真软件 http://www.transcranial.com/index ...
- Xcode9新变化
http://www.cnblogs.com/lurenq/archive/2017/09/26/7594909.html
- poj1050 dp动态规划
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...