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\) ,每一步只 ...
随机推荐
- ORACLE PL/SQL编程详解
ORACLE PL/SQL编程详解 编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设 ...
- iwork 09 可以用的序列号
E4RJ-YKHP-9ER4-WK9Y-H4UM-5S2G9MU-9U9J-KN4M-4PAE-DYNA-5S2EGMU-3PKP-NN9G-3JW4-3RAD-5S2
- mysql-创建库之问题
一.在登入mysql命令行,创建数据库时报错 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'xiaolu ...
- 【转载】IIS7.5(经典模式)访问静态资源(.css和.js文件)提示:未能执行 URL
IIS7.5(经典模式)静态资源(.css和.js文件)提示:未能执行 URL “/”应用程序中的服务器错误. 未能执行 URL. 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪 ...
- Visual Studio技巧之打造拥有自己标识的代码模板
可能经过很多博客的介绍,大家都知道代码段的使用,使用代码段可以很方便地生成一些常用的代码格式,确实对我们开发很方便.在团队开发中或者在某些情况下我们经常可能还会希望使用Visual Studio生成的 ...
- java短信接口
一.背景 从是Java一直想做一个跟生活联系特别紧密的东西,比如短信.邮箱.电话什么的一直是我感兴趣的,可是楞是当初没有头绪弄,恰巧今天公司在做一个 webrtc的视频会议的软件,刚好有短信这个需求, ...
- Linux 下三种方式设置环境变量
1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错误. 2.那么什么是环境变 ...
- ModelAttribute注解
1.使用@ModelAttribute标记方法,会在每个目标方法执行前被springMVC调用 2.使用@ModelAttribute修饰目标方法pojo入参,其value属性值有以下作用: 1)sp ...
- (Array)27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- goldengate 参数之GETTRUNCATES | IGNORETRUNCATES --转载
GETTRUNCATES | IGNORETRUNCATESValid ForExtract and ReplicatDescriptionUse the GETTRUNCATESand IGNORE ...