Tara's Beautiful Permutations 组合数学
https://www.hackerrank.com/contests/hourrank-15/challenges/taras-beautiful-permutations
题意是说,给定一个数组,里面的数字最多出现两次,求所有的合法排列,合法排列定义为没有相同的数字排在一起。
首先先统计一下个数为2的数字的个数。
用all表示。
然后先不理题目要求,总排列数是A(n, n) / (2! * 2! * 2! .... * 2!),就是(2!)^all
下面蹦一波容斥。
暴力枚举i表示有i对东西是放在一起的,就是违反了规矩的,
然后这i对和身下的n - 2 * i个东西组合一起的情况有A(n - 2 * i + i) / (2!)^(all - i)种情况,容斥即可。
奇减偶加
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int MOD = 1e9 + ;
LL quick_pow(LL a, LL b, LL MOD) { //求解 a^b%MOD的值
LL base = a % MOD;
LL ans = ; //相乘,所以这里是1
while (b) {
if (b & ) {
ans = (ans * base) % MOD; //如果这里是很大的数据,就要用quick_mul
}
base = (base * base) % MOD; //notice。注意这里,每次的base是自己base倍
b >>= ;
}
return ans;
} LL C(LL n, LL m, LL MOD) {
if (n < m) return ; //防止sb地在循环,在lucas的时候
if (n == m) return ;
LL ans1 = ;
LL ans2 = ;
LL mx = max(n - m, m); //这个也是必要的。能约就约最大的那个
LL mi = n - mx;
for (int i = ; i <= mi; ++i) {
ans1 = ans1 * (mx + i) %MOD;
ans2 = ans2 * i % MOD;
}
return (ans1 * quick_pow(ans2, MOD - , MOD) % MOD); //这里放到最后进行,不然会很慢
}
const int maxn = + ;
int a[maxn];
map<int, int>book;
LL A(int n, int has, int MOD) {
LL ans1 = ;
LL ans2 = ;
for (int i = ; i <= n; ++i) {
ans1 = ans1 * i % MOD;
}
for (int i = ; i <= has; ++i) {
ans2 = ans2 * % MOD;
}
return (ans1 * quick_pow(ans2, MOD - , MOD) % MOD);
}
void work() {
book.clear();
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
book[a[i]]++;
}
int all = ;
for (map<int, int> :: iterator it = book.begin(); it != book.end(); it++) {
if (it->second == ) {
all++;
}
}
// cout << all << endl;
LL ans = A(n, all, MOD);
if (all == ) {
cout << ans << endl;
return;
}
// cout << ans << endl;
for (int i = ; i <= all; ++i) {
if (i & ) {
ans = (ans + MOD - C(all, i, MOD) * A(n - i, all - i, MOD) % MOD) % MOD;
} else {
ans = (ans + C(all, i, MOD) * A(n - i, all - i, MOD) % MOD) % MOD;
}
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
Tara's Beautiful Permutations 组合数学的更多相关文章
- codeforces 336D Vasily the Bear and Beautiful Strings(组合数学)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Vasily the Bear and Beautiful Strings Vas ...
- codeforces 336D. Vasily the Bear and Beautiful Strings 组合数学 dp
题意: 给出n,m,g,求好串的个数 0 <= n,m <= 10^5,n + m >= 1,0 <= g <= 1 好串的定义: 1.只由0,1组成,并且恰好有n个0, ...
- HDU 3811 Permutation 状压dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...
- HDU3811 Permutation —— 状压DP
题目链接:https://vjudge.net/problem/HDU-3811 Permutation Time Limit: 6000/3000 MS (Java/Others) Memor ...
- 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】
链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网 题目描述 It's universally acknowledged that there'r ...
- Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)
A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...
- Codeforces 1264D - Beautiful Bracket Sequence(组合数学)
Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...
- HDU 5321 Beautiful Set (莫比乌斯反演 + 逆元 + 组合数学)
题意:给定一个 n 个数的集合,然后让你求两个值, 1.是将这个集合的数进行全排列后的每个区间的gcd之和. 2.是求这个集合的所有的子集的gcd乘以子集大小的和. 析:对于先求出len,len[i] ...
- Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学
题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...
随机推荐
- 在HTML标签<a/>中调用javascript代码
<a/>标签的“href”属性可以是一个有效的URL,表示跳转的目的地,除此之外,href还可以是一段javascript代码.当为“href”设置javascript代码时,格式如下:& ...
- sublimetext 使用正则表达式匹配中文
[\x{4e00}-\x{9fa5}] ============================================= 参考资料 1.在javascript下正确的\x4e00-\x9fa ...
- Windows环境下使用Redis缓存工具的图文详细方法
一.简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合). ...
- JS--事件模块
一.JS event 的浏览器兼容 说到JS事件,不能不先讲一下事件流 1 事件流的定义:事件流是指从页面中接收事件的顺序 如下图所示,假设有四个圆层叠在一起,如果我们单击图中最里面的那个圆,那么我们 ...
- .NET EXCEL NPOI 图片
今天闲来无事写了下 “ .NET 用 NPOI 组件 将 图片文件 写到 EXCEL 中 ” 先看效果: 头是我加的,这个应该不是难事! 再看代码: private void button1_C ...
- 用JavaScript动态加载CSS和JS文件
本文转载自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/11/14/2248451.html 今天项目中需要用到动态加载 CSS 文件 ...
- (C#) Tasks 中的异常处理(Exception Handling.)
多线程编程中要注意对线程异常的处理.首先写个例子. 一个线程用于显示信息(Show Messages).主线程用于做其他工作(Do Works). using (Task taskShowMessag ...
- ant 自定义taskdef的工作目录
上次同事在用ant执行多层目录的测试用例的时候遇到了一些问题,也就是自定义的taskdef的工作目录总是在开始执行ant的那个目录,而有一些地方用到了当前目录,因此很多测试用命的代码出现了“找不到自定 ...
- day6_1
一.加密模块 1.hashlib >>> data=hashlib.md5() >>> data.update(b'hello') >>> pri ...
- log4j配置不同的类多个日志文件
<Configuration status="INFO"> <Appenders> <Console name="STDOUT" ...