题意:给定一个 n 个数的集合,然后让你求两个值,

1。是将这个集合的数进行全排列后的每个区间的gcd之和。

2。是求这个集合的所有的子集的gcd乘以子集大小的和。

析:对于先求出len,len[i]表示能够整除 i 的的个数。

第一个值,根据排列组合,求出gcd是 i 的倍数的个数

解释一下这个式子,先从len[i]中选出 j 个数,然后进行排列,这就是所选的区间,然后再把这 j 个数看成一个大元素,再和其他的进行排列,也就是(n-j+1)!,总体也就是排列组合。

对于第二个值,

这个式子应该很好理解,就是一个组合问题。

要提前先预处理出来上面那两个式子,处理第一个式子的时候,还要注意处理阶乘的逆元,这个可用费马小定理和快速幂来解决。

处理完上面那个式子后,再用莫比乌斯反演来处理,可以得到f1,f2

最后答案就是

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int maxm = 2e4 + 10;
const LL mod = 258280327;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} bool vis[maxn];
int prime[maxn], mu[maxn]; LL fact[maxn], fa[maxn], inv[maxn]; LL fast_pow(LL a, int n){
LL res = 1;
while(n){
if(n&1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
} void Moblus(){
mu[1] = 1; int tot = 0; fa[1] = 2;
fact[0] = fact[1] = fa[0] = 1;
for(int i = 2; i < maxn; ++i){
if(!vis[i]) prime[tot++] = i, mu[i] = -1;
for(int j = 0; j < tot; ++j){
int t = i * prime[j];
if(t >= maxn) break;
vis[t] = 1;
if(i % prime[j] == 0) break;
mu[t] = -mu[i];
}
fact[i] = (fact[i-1] * i) % mod;
fa[i] = (fa[i-1]<<1) % mod;
}
inv[maxn-1] = fast_pow(fact[maxn-1], mod-2);
for(int i = maxn-2; i >= 0; --i)
inv[i] = inv[i+1] * (i+1) % mod;
} int a[maxn], len[maxn];
LL g1[maxn], g2[maxn]; int main(){
Moblus();
while(scanf("%d", &n) == 1){
ms(len, 0); ms(a, 0);
int mmax = 0;
for(int i = 0; i < n; ++i){
int x; scanf("%d", &x);
++a[x]; mmax = max(mmax, x);
}
for(int i = 1; i <= mmax; ++i)
for(int j = i; j <= mmax; j += i)
len[i] += a[j];
for(int i = 1; i <= mmax; ++i){
g1[i] = g2[i] = 0;
if(!len[i]) continue;
for(int j = 1; j <= len[i]; ++j)
g1[i] = g1[i] + fact[len[i]] * inv[len[i]-j] % mod * fact[n-j+1] % mod;
g2[i] = len[i] * fa[len[i]-1] % mod;
}
LL ans1 = 0, ans2 = 0;
for(int i = 1; i <= mmax; ++i){
LL tmp1 = 0, tmp2 = 0;
for(int j = i, k = 1; j <= mmax; j += i, ++k){
tmp1 += mu[k] * g1[j];
tmp2 += mu[k] * g2[j];
}
ans1 = (ans1 + tmp1 * i) % mod;
ans2 = (ans2 + tmp2 * i) % mod;
}
ans1 = (ans1 + mod) % mod;
ans2 = (ans2 + mod) % mod;
if(ans1 == ans2) printf("Equal %lld\n", ans1);
else if(ans1 > ans2) printf("Mr. Zstu %lld\n", ans1);
else printf("Mr. Hdu %lld\n", ans2);
}
return 0;
}

  

HDU 5321 Beautiful Set (莫比乌斯反演 + 逆元 + 组合数学)的更多相关文章

  1. HDU 5321 Beautiful Set 漂亮集合

    题意:给定一个集合,含有n个数.浙理工先生和杭电先生各自有计算这个集合漂亮值的方法. 浙理工先生的计算方法是:对于这个n个数的某个排列,此排列的漂亮值为这个排列全部的区间最大公约数之和.然后这个集合的 ...

  2. HDU 4746 Mophues【莫比乌斯反演】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4746 题意: 1≤x,y≤n , 求gcd(x,y)分解后质因数个数小于等k的(x,y)的对数. 分 ...

  3. bzoj 4816: [Sdoi2017]数字表格【莫比乌斯反演+逆元】

    把题意简化,就是要求 \[ \prod_{d=1}^{min(n,m)}f[d]^{\sum_{i=1}^{n}\sum_{j=1}^{m}e[gcd(i,j)==d]} \] 把幂用莫比乌斯反演转化 ...

  4. HDU 1695 GCD (莫比乌斯反演)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. HDU 1695 GCD (莫比乌斯反演模板)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  6. HDU 5468 Puzzled Elena 莫比乌斯反演

    题意: 给出一棵树,每个点上有权值.然后求每棵子树中与根节点互质( \(gcd(a, b) = 1\) )的节点个数. 分析: 对于一颗子树来说,设根节点的权值为\(u\), \(count_i\)表 ...

  7. hdu 1695 容斥原理或莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. hdu 1695: GCD 【莫比乌斯反演】

    题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...

  9. HDU 5608 function(莫比乌斯反演 + 杜教筛)题解

    题意: 已知\(N^2-3N+2=\sum_{d|N}f(d)\),求\(\sum_{i=1}^nf(i) \mod 1e9+7\),\(n\leq1e9\) 思路: 杜教筛基础题? 很显然这里已经设 ...

随机推荐

  1. linux下svn版本控制的常用命令大全

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录) 例如:svn checkout svn://192.168.1.1/pro/domain 简写:s ...

  2. 合并两个git仓库

    现有两个git仓库ekt_zy.ekt_zijian,需要把ekt_zijian项目中的代码合并到ekt_zy项目中. 1 将ekt_zijian作为远程仓库.添加到ekt_zy中,设置别名为ziji ...

  3. node.js中事件触发器events的使用

    node.js是基于事件驱动的,通过events,我们可以方便的创建事件,并通过触发事件来调用我们自定义的监听函数. 所有能触发事件的对象都应该是 EventEmitter 类的实例,一般我们自定义一 ...

  4. stark组件开发之URL分发和默认Handler

    为register 函数添加一个,prev参数,默认None ,用于可以让用户自己指定前缀. def register(self, model_class, handler_class=None, p ...

  5. JFinal Web开发学习(九)后台添加前台显示博客

    效果: 发博客: 显示博客: 后台:使用hui-admin,文章编辑器是百度开源的ueditor 前台:使用layui前端框架 1.写控制器BlogController controller包中 pa ...

  6. Http方式下载文件

    代码: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System ...

  7. 直接提交一个form表单后台返回一个新页面通过target属性可以放到iframe当中

    问题描述: 我想提交一个form表单后台直接返回一个新页面,但是当前页面还不想被替换掉: 解决方案: 在页面中添加一个iframe将form表单的target属性设置为iframe的id这样的话返回的 ...

  8. 基于spring boot的统一异常处理

    一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...

  9. dell E6400笔记本 centos7 安装无线网卡

    1.下载  驱动:http://download.csdn.net/detail/nj4506/9752842 2.解压: make  make install 3.将wk.ko拷贝到 /lib/mo ...

  10. 大神的P图过程!快来偷窥!

    来自美国的艺术家James(@jameasons) 平时我们总是能看到一些大神合成出这样的图片, 但是他们P图的过程是怎样的,很多人都是不知道的. 接下来再看看这位大神的其他作品, 如果你看了上面视频 ...