hdu 5651 重复全排列+逆元
知识点:
n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列。
若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为
,即其重复度为ni!
同理a1重复了n1次,a2重复了n2次,····,ak重复了nk次,n1+n2+····+nk=n。
对于这样的n个元素进行全排列,可得不同排列的个数实际上是 
由于题目要求是对100000007取余 同余定理中对于同一个除数,两个数的乘积与它们余数的乘积同余。但这里有除法所以得用上逆元
逆元
定义:
满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元。eg: 1=5*3-14 所以5关于模14的乘法逆元为3.应用:
当我们要求 (a/b) mod P 的值时,如果 a 很大,无法直接求得a/b的值时,我们就可以使用乘法逆元。我们可以通过求b关于P的乘法逆元k,将a乘上k再模P,即(a%P*k)。其结果与(a/b) mod P等价。
关于逆元的求解方法日后在做总结 这里用的是exgcd
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const LL MOD=1e9+;
int cnt[];
char ch[]; LL jiecheng(int n)
{
if(n==)
return ;
LL ans=;
for(int i=;i<=n;i++)
ans=ans*i%MOD;
return ans;
} LL x,y;
LL gcd(LL a,LL b)
{
LL t,d;
if(b==)
{
x=,y=;
return a;
}
d=gcd(b,a%b);
t=x, x=y, y=t-(a/b)*y;
return d;
} int main()
{
int t;
cin>>t;
while(t--)
{
memset(cnt,,sizeof(cnt));
scanf("%s",ch);
int len=strlen(ch);
for(int i=;i<len;i++)
{
cnt[ch[i]-' ']++;
}
int count=;
for(int i=;i<;i++)
{
if(cnt[i]&)
count++;
cnt[i]/=;
}
if(count>)
{
cout<<<<endl;
continue;
}
LL ans=jiecheng(len/)%MOD;
for(int i=;i<;i++)
{
if(cnt[i]>)
{
gcd(jiecheng(cnt[i]),MOD);
if(x<)
x+=MOD;// 求逆元
ans=ans*x%MOD;
}
}
cout<<ans<<endl;
}
}
hdu 5651 重复全排列+逆元的更多相关文章
- POJ3252 Round Numbers(不重复全排列)
题目问区间有多少个数字的二进制0的个数大于等于1的个数. 用数学方法求出0到n区间的合法个数,然后用类似数位DP的统计思想. 我大概是这么求的,确定前缀的0和1,然后后面就是若干个0和若干个1的不重复 ...
- LightOJ1060 nth Permutation(不重复全排列+逆康托展开)
一年多前遇到差不多的题目http://acm.fafu.edu.cn/problem.php?id=1427. 一开始我还用搜索..后来那时意外找到一个不重复全排列的计算公式:M!/(N1!*N2!* ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- HDU 5651 组合+逆元
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...
- HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...
- HDU 5651 xiaoxin juju needs help 逆元
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5651 逆元
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu 5651 xiaoxin juju needs help 逆元 两种求解方式
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
随机推荐
- js反混淆
var esprima = require('esprima') var escodegen = require('escodegen') content = "function _0x35 ...
- Ubuntu 安装 JDK1.8
以下是Ubuntu 14.04安装JDK1.8.0_25与配置环境变量过程笔记. 1.源码包准备: 首先到官网下载jdk,http://www.oracle.com/technetwork/java/ ...
- HearthBuddy Magnetic 磁力
https://hearthstone.gamepedia.com/Magnetic Magnetic is an ability exclusive to certain Mech minions ...
- 微服务中的CAP定律
说到微服务,先给大家提一下CAP分布式应用知识吧,无论你微服务使用的是阿里云开源的Dubbo还是基于Springboot的一整套实现微服务的Springcloud都必须遵循CAP定理不然你所实现的分布 ...
- java.io.IOException: Cleartext HTTP traffic to xxx.xxx.xxx.xxx not permitted
java.io.IOException: Cleartext HTTP traffic to xxx.xxx.xxx.xxx not permitted 转 https://blog.csdn.net ...
- Oracle常用CURD
-------------------------------------------------------------------------------------通用函数和条件判断函数 使用N ...
- hibernate一对一映射
package loaderman.c_one2one; // 身份证 public class IdCard { // 身份证号(主键) private String cardNum;// 对象唯一 ...
- [spring]@Resource和@Autowired区别对比
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Sprin ...
- oc 执行shell 脚本
-(id) InvokingShellScriptAtPath :(NSString*) shellScriptPath { NSTask *shellTask = [[NSTask alloc]in ...
- Claymore's Dua Miner挖矿教程
一.软件准备:首先需要一款挖矿软件.在这里推荐Claymore's Dua Miner的官方原版. 注意: 1. 如果不了解,请不要在网上随意下载其它版本的挖矿软件或者Claymore's Dua M ...