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 + 7;
LL quick_pow(LL a, LL b, LL MOD) {  //求解 a^b%MOD的值LL base = a % MOD;LL ans = 1; //相乘,所以这里是1while (b) {if (b & 1) {ans = (ans * base) % MOD; //如果这里是很大的数据,就要用quick_mul
        }base = (base * base) % MOD;    //notice。注意这里,每次的base是自己base倍b >>= 1;}return ans;
}LL C(LL n, LL m, LL MOD) {if (n < m) return 0; //防止sb地在循环,在lucas的时候if (n == m) return 1;LL ans1 = 1;LL ans2 = 1;LL mx = max(n - m, m); //这个也是必要的。能约就约最大的那个LL mi = n - mx;for (int i = 1; i <= mi; ++i) {ans1 = ans1 * (mx + i) %MOD;ans2 = ans2 * i % MOD;}return (ans1 * quick_pow(ans2, MOD - 2, MOD) % MOD); //这里放到最后进行,不然会很慢
}
const int maxn = 2000 + 20;
int a[maxn];
map<int, int>book;
LL A(int n, int has, int MOD) {LL ans1 = 1;LL ans2 = 1;for (int i = 1; i <= n; ++i) {ans1 = ans1 * i % MOD;}for (int i = 1; i <= has; ++i) {ans2 = ans2 * 2 % MOD;}return (ans1 * quick_pow(ans2, MOD - 2, MOD) % MOD);
}
void work() {book.clear();int n;scanf("%d", &n);for (int i = 1; i <= n; ++i) {scanf("%d", &a[i]);book[a[i]]++;}int all = 0;for (map<int, int> :: iterator it = book.begin(); it != book.end(); it++) {if (it->second == 2) {all++;}}
//    cout << all << endl;LL ans = A(n, all, MOD);if (all == 0) {cout << ans << endl;return;}
//    cout << ans << endl;for (int i = 1; i <= all; ++i) {if (i & 1) {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 localfreopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endifint t;scanf("%d", &t);while (t--) work();return 0;
}

View Code

转载于:https://www.cnblogs.com/liuweimingcprogram/p/6135008.html

Tara's Beautiful Permutations 组合数学相关推荐

  1. CF1516E. Baby Ehab Plays with Permutations(组合数学)

    CF1516E. Baby Ehab Plays with Permutations Solution 因为组合水平不行所以只弄出来一个O(k4)O(k^4)O(k4)的做法(虽然随便改改可能就O(k ...

  2. C. Cyclic Permutations(组合数学+单峰序列)

    Problem - 1391C - Codeforces 题意: 一个长度为n的排列是由1到n的n个不同的整数按任意顺序组成的数组.例如,[2,3,1,5,4]是一个排列组合,但[1,2,2]不是排列 ...

  3. linux 文件系统 启动,linux kernel文件系统启动部分

    现在的kernel里,有个叫做ramfs的文件系统,会把initrd(或者ramdisk,为惯性叫法)里的东西挂载到early-rootfs里(即rootfs,是ramfs的一个特殊实例),执行一些在 ...

  4. 第十四届华中科技大学程序设计竞赛 B Beautiful Trees Cutting【组合数学/费马小定理求逆元/快速幂】...

    链接:https://www.nowcoder.com/acm/contest/106/B 来源:牛客网题目描述 It's universally acknowledged that there're ...

  5. CodeForces - 336D Vasily the Bear and Beautiful Strings(dp+组合数学)

    题目链接:点击查看 题目大意:给出一个 01 字符串,规定求值的过程如下: 每次选择末尾的两个数字: 如果为 0 0 ,那么替换成一个 1 否则替换成一个 0 循环往复,直至只剩一个数字位置,剩下的数 ...

  6. 组合数学之排列组合(Permutations and Combinations)(四种情况)

    加减乘除四个原理不再赘述.(即使小学生都会的原理也能出些大学生不会的题目) 1集合的排列(Pertutations of Sets)(无重有序)(无重复有序) 设r为正整数,把n个元素的集合S的一个r ...

  7. CodeForces 285 E.Positions in Permutations(dp+组合数学)

    Description 定义一个排列的权为满足|pi−i|=1|p_i-i|=1的ii的个数,问长度为nn的排列且权为kk的有多少个 Input 两个整数n,k(1≤n≤1000,0≤k≤n)n,k( ...

  8. python itertools combination_Python itertools.combinations 和 itertools.permutations 等价代码实现...

    最近编程时经常要用到排序组合的代码,想当年还抱着一些情况买了一本<组合数学>,不过现在这货也不知道被自己放哪里了,估计不会是垫桌子腿了吧. 由于去年去东北大学考博面试的时候遇到过可能涉及排 ...

  9. 组合数学学习笔记(未完待续

    这学期学了不少组合数学,期末给他补完. 算法竞赛考得很多的部分啊 这个还是很重要的 在目前的算法竞赛中有三大计数考点 1)组合计数 2)线性计数 3)群论计数 其中群论计数比较困难,我又不知道什么是线 ...

  10. Beautiful Walls

    [1553] Beautiful Walls 时间限制: 1000 ms 内存限制: 65535 K 问题描述 To simplify the building process, XadillaX b ...

最新文章

  1. 像教光学一样在高中教深度学习?怼过LeCun的Google大牛认为这事有出路
  2. 未来15年,人工智能将带给城市8种改变
  3. 新车模、新舵机、新体验
  4. javascript高级程序设计之BOM
  5. pfSense 2.4.0-RC版发布了!
  6. VC中使用全局变量的2种办法及防错措施
  7. 【leetcode】56. Merge Intervals 相邻线段归并
  8. 它来了,阿里架构师的“Java多线程+并发编程”知识点详解手册,限时分享
  9. Angulary应用依赖里的platform-browser
  10. Loj#6485. LJJ 学二项式定理
  11. tensorflow源码编译教程_源码编译安装tensorflow 1.8
  12. 【C语言】创建一个函数,利用该函数将字符串中的小写字母转换为大写字母
  13. GitHub|基于强化学习自动化剪枝
  14. 如何应对被地下的Oracle口令加密算法(2)
  15. 关于 数据库 my_slq的 安装及其卸载
  16. AMP Roadshow技术分享路演中国专场报名开始了!
  17. ttl传输种过期_来自 202.112.36.253 的回复: TTL 传输中过期。解决思路
  18. 【Axure教程】中继器手风琴
  19. 火车头定制规则php什么意思,火车头WordPress发布规则写法教程
  20. bcc钱包地址生成linux,从Bcc到xdp原理分析

热门文章

  1. 仓库盘点的四大方法和盘点流程
  2. Docker安装ElasticSearch 版本7.6.2
  3. 常用数字信号处理方法在matlab上的实现(目录和先导)
  4. win7计算机名称格式,win7笔记本电脑如何显示文件扩展名
  5. shell脚本shc加密解密
  6. 【项目实训】微信公众号获取用户openid
  7. 光谱分辨率单位_遥感图像的空间分辨率,光谱分辨率,辐射分辨率,时间分辨率...
  8. Linux文字游戏制作软件,小精灵美化app
  9. Python —— 压缩文件夹
  10. 怎么在html中把3个单元格合并成2个,Excel表格怎么将一个单元格拆分成2个?将多个单元合并成一个的方法...