题意:给定一棵带权树,求每个点与其子树结点的权值互质的个数. 析:首先先要进行 dfs 遍历,len[i] 表示能够整除 i 的个数,在遍历的前和遍历后的差值就是子树的len值,有了这个值,就可以使用莫比斯反演了.注意如果子树的权值是1,还要加上它本身. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #includ…
题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表示权值为\(i\)的倍数的节点的个数. 那么根据莫比乌斯反演,与\(u\)互质的节点的个数为\(\sum_{d|u}\mu(d)count_d\) 所以,我们记录一下遍历子树之前的\(count\)值和遍历子树之后的\(count\)值,作差就是这棵子树的\(count\)值 #include <i…
Puzzled Elena Time Limit: 2500ms Memory Limit: 131072KB This problem will be judged on HDU. Original ID: 546864-bit integer IO format: %I64d      Java class name: Main Since both Stefan and Damon fell in love with Elena, and it was really difficult f…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2841 题意:给n*m的矩阵(从(1,1)开始编号)格子,每个格子有一棵树,人站在(0,0)的位置,求可以看到多少棵树.同一直线上的树只能看到最靠近人的那颗. 思路:可以将题目转化为求gcd(x, y) = 1,(1 <= x <= n, 1 <= y <= m)的对数.直接套用莫比乌斯反演即可. code: #include <cstdio> #include <cs…
题意:给出序列[a1..aN],整数M和k,求对1-M中的每个整数d,构建新的序列[b1...bN],使其满足: 1. \(1 \le bi \le M\) 2. \(gcd(b 1, b 2, -, b N) = d\) 3. 恰好有k个位置 \(bi!=ai\) 求对每个d,有多少种满足条件的序列 分析:对于前两个条件,就是单纯的莫比乌斯反演. 令\(F(d) = [d|gcd(b1...bN)]\) \(f(d) = [gcd(b1...bN)]=d]\) 则$f(n) = \sum_{x…
题目链接 题意 给出n个数,问在这n个数里面,有多少组bi(1<=bi<=ai)可以使得任意两个bi不互质. 思路 想法就是枚举2~min(ai),然后去对于每个ai都去除以这些质数,然后再相乘就代表对于这个数有多少种.但是这样的想法会超时的还会算重复. 那么考虑分段计数,处理一个前缀和,代表sum[1~i]这段区间出现了多少个ai,然后对于每个因子都去枚举它的倍数,因为这段区间里面的数对于这个因子的贡献是相同的,于是可以有pow(倍数, sum[r] - sum[l-1])的贡献,这样的复杂…
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N integers a 1, a 2, -, a N, and M, K. She says each integers 1 ≤ a i ≤ M. And now Alice wants to ask for each d = 1 to M, how many different sequences b…
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6134 [题目大意] 求$\sum_{i=1}^{n}{\sum_{j=1}^{i}\lceil{\frac{i}{j}}\rceil}[ (i,j)==1 ]$ [题解] 设 $g(i)=\sum_{i=1}^{n}{\sum_{j=1}^{i}\lceil{\frac{i}{j}}\rceil}$,$h(i)=\sum_{i=1}^{n}{\sum_{j=1}^{i}\lceil{\frac{…
hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: 1 1 0 0 0 题意:在一棵树上,每个节点有值,求以x为根节点的树中,有多少与根节点互质 思路: 用num[i]记录节点中包含因子i的个数,然后搜索到当前根节点时,我们先记录下在此之前的num,然后遍历返回后, 计算num的差值,利用莫比乌斯原理,先ans记录树中所有的节点数,然后该加的加,该减…
题意:给定一个 n 个数的集合,然后让你求两个值, 1.是将这个集合的数进行全排列后的每个区间的gcd之和. 2.是求这个集合的所有的子集的gcd乘以子集大小的和. 析:对于先求出len,len[i]表示能够整除 i 的的个数. 第一个值,根据排列组合,求出gcd是 i 的倍数的个数, 解释一下这个式子,先从len[i]中选出 j 个数,然后进行排列,这就是所选的区间,然后再把这 j 个数看成一个大元素,再和其他的进行排列,也就是(n-j+1)!,总体也就是排列组合. 对于第二个值, 这个式子应…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4141    Accepted Submission(s): 1441 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)…
Puzzled Elena Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1247    Accepted Submission(s): 370 Problem Description Since both Stefan and Damon fell in love with Elena, and it was really dif…
Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 376 Problem Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4746 题意: 1≤x,y≤n , 求gcd(x,y)分解后质因数个数小于等k的(x,y)的对数. 分析: 莫比乌斯反演. 还是一个套路,我们设 f(d):满足gcd(x,y)=d且x,y均在给定范围内的(x,y)的对数. F(d):满足d|gcd(x,y)且x,y均在给定范围内的(x,y)的对数. 显然F(x)=[n/x]∗[m/x],反演后我们得到 f(x)=∑x|dμ(d/x)[n/d]∗[m…
Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 300    Accepted Submission(s): 124 Problem Description WLD likes playing with codes.One day he is writing a function.Howerver,his computer b…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4291    Accepted Submission(s): 1502 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)…
这道题看巨巨的题解看了好久,好久.. 本文转自hdu4746(莫比乌斯反演) 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<=n, 1<=b<=m) 分析:设A(d):gcd(a, b)=d的有多少种      设B(j): gcd(a, b)是j的倍数的有多少种,易知B(j) = (n/j)*(m/j)      则由容斥原理得:(注:不同行的μ是不相同的,μ为莫比乌斯函数)      A(1) = μ(1)*B(1)…
题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是不知道怎么应用. 这里有关于莫比乌斯反演的知识,而且最后的例题中就有这道题并给出了公式的推导. 在最后的例题2中有个重要的结论: #include <cstdio> #include <algorithm> typedef long long LL; ; ], vis[maxn + ]…
GCD 题意:输入5个数a,b,c,d,k;(a = c = 1, 0 < b,d,k <= 100000);问有多少对a <= p <= b, c <= q <= d使得gcd(p,q) = k; 注:对于(p,q)和(q,p)只算一次: 思路:由于遍历朴素求两个数的gcd的时间复杂度为O(n^2*log(n)),朴素算法遍历搜索在判断累加,所以效率很低: 资料   NanoApe's Blog   ACdreamers 莫比乌斯反演:利用整与分之间的可逆来由整体利用…
分析:简单的莫比乌斯反演 f[i]为k=i时的答案数 然后就很简单了 #include<iostream> #include<algorithm> #include<set> #include<vector> #include<queue> #include<cstdlib> #include<cstdio> #include<cstring> #include<cmath> using names…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17212    Accepted Submission(s): 6637 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x,…
题意:给定上一个数组,求 析: 其中,f(d)表示的是gcd==d的个数,然后用莫比乌斯反演即可求得,len[i]表示能整队 i 的个数,可以线性筛选得到, 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include…
题目链接:https://cn.vjudge.net/problem/HDU-1695#author=541607120101 感觉讲的很好的一个博客:https://www.cnblogs.com/peng-ym/p/8647856.html 今天刚开始学莫比乌斯反演,先据我所了解的说一下. 首先是莫比乌斯函数. 1,mu(x).当x为1时,mu(1)等于1. 2,当x为素数时,mu(x)=-1. 3,当x能唯一分解成多个不同的素数相乘的时候(不能有重复的素数)mu(x)=(-1)的k次方,k…
Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1306    Accepted Submission(s): 540 Problem Description WLD likes playing with codes.One day he is writing a function.Howerver,his computer b…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9942    Accepted Submission(s): 3732 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5310    Accepted Submission(s): 1907 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12004    Accepted Submission(s): 4531 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y…
题意略. 思路:首先想到暴力去扫,这样的复杂度是n * min(ai),对于gcd = p,对答案的贡献应该是 (a1 / p) * (a2 / p) * .... * (an / p),得出这个贡献未必要暴力地去扫, 我们可以分桶后,再求后缀和,再作差来得到个数后,进行快速幂.比如说:我们想知道gcd = p时对答案的贡献,那么add = (c1 ^ d1) * (c2 ^ d2) *.....,其中 c1是ai / p之后得出的数,d1表示(a1 / p) * (a2 / p) * ....…
Mophues \[ Time Limit: 10000 ms\quad Memory Limit: 262144 kB \] 题意 求出满足 \(gcd\left(a,b\right) = k\),其中\(1\leq a\leq n,1\leq b \leq m\)且 \(k\) 的因子数 \(\leq P\) 思路 \(g\left(x\right)\) 表示 \(gcd\left(a, b\right) | x\) 的对数 \(f\left(x\right)\) 表示 \(gcd\left…
题目描述 有N2−3N+2=∑d∣Nf(d)N^2-3N+2=\sum_{d|N} f(d)N2−3N+2=∑d∣N​f(d) 求∑i=1Nf(i)\sum_{i=1}^{N} f(i)∑i=1N​f(i)  mod 109+7~mod~10^9+7 mod 109+7 1<=T<=5001<=N<=1091<=T<=500\\1<=N<=10^91<=T<=5001<=N<=109 只有最多555组数据N>106N>10…