hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
题意:给你一个字符串,求打乱字符后,有多少种回文串。 (题于文末)
知识点:
n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列。
若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为
,即其重复度为ni!
同理a1重复了n1次,a2重复了n2次,····,ak重复了nk次,n1+n2+····+nk=n。
对于这样的n个元素进行全排列,可得不同排列的个数实际上是 
题解:
字符串长度为len,若每个元素的重复次数ni为奇数的个数count>1,则无法形成回文串。
当能构成回文串时,我们只需考虑这个回文串左半部分的情况,所以这个问题也就变成了求一半字符串的有重复的全排列。
因为涉及到除法取模的问题,所以用到逆元。
逆元可以用扩展欧几里得,或费马小定理。/*6.逆元*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
int cnt[260];
char ch[1005]; LL jiecheng(int n)
{
if(n==0)
return 1;
LL ans=1;
for(int i=1;i<=n;i++)
ans=ans*i%MOD;
return ans;
} LL x,y;
LL gcd(LL a,LL b)
{
LL t,d;
if(b==0)
{
x=1,y=0;
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,0,sizeof(cnt));
scanf("%s",ch);
int len=strlen(ch);
for(int i=0;i<len;i++)
{
cnt[ch[i]-' ']++;
}
int count=0;
for(int i=0;i<260;i++)
{
if(cnt[i]&1)
count++;
cnt[i]/=2;
}
if(count>1)
{
cout<<0<<endl;
continue;
}
LL ans=jiecheng(len/2)%MOD;
for(int i=0;i<260;i++)
{
if(cnt[i]>0)
{
gcd(jiecheng(cnt[i]),MOD);
if(x<0)
x+=MOD;
ans=ans*x%MOD;
}
}
cout<<ans<<endl;
}
}
xiaoxin juju needs help
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
As we all known, xiaoxin is a brilliant coder. He knew **palindromic** strings when he was only a six grade student at elementry school.
This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin's leader needs to buy?
Input
This problem has multi test cases. First line contains a single integer $T(T\leq 20)$ which represents the number of test cases.
For each test case, there is a single line containing a string $S(1 \leq length(S) \leq 1,000)$.
Output
For each test case, print an integer which is the number of watermelon candies xiaoxin's leader needs to buy after mod $1,000,000,007$.
Sample Input
3
aa
aabb
a
Sample Output
1
2
1
hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)的更多相关文章
- hdu5651 xiaoxin juju needs help(逆元)
xiaoxin juju needs help Accepts: 150 Submissions: 966 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)
题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- HDU 5651 xiaoxin juju needs help (组合数)
xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...
- hdu 5651 xiaoxin juju needs help 逆元 两种求解方式
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- 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 数学
xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...
- HDU 5651 xiaoxin juju needs help 水题一发
分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...
- HDU 5651 xiaoxin juju needs help
组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...
随机推荐
- 2. Struts2 基础
1. Struts2简介 Struts2是一个WEB端MVC框架.作为比较早的MVC 框架之一,Struts2在使用中还是比较多的.虽然个人感受没有SpringMVC还那么的好用 Struts2 官网 ...
- 读书笔记--SQL必知必会22--高级SQL特性
22.1 约束 约束(constraint),管理如何插入或处理数据库数据的规则. DBMS通过在数据库表上施加约束来实施引用完整性. 大多数约束是在表定义中定义的. 22.1.1 主键 主键,用来保 ...
- 基于 HTML5 的 Web SCADA 报表
背景 最近在一个 SCADA 项目中遇到了在 Web 页面中展示设备报表的需求.一个完整的报表,一般包含了筛选操作区.表格.Chart.展板等多种元素,而其中的数据表格是最常用的控件.在以往的工业项目 ...
- 基于 Cmd MarkDown 的 markdown 语法学习
首先我要打一个属于干货的广告:CmdMarkDown 是非常好用的markdown编辑器软件,支持全平台,由作业部落出品,分为客户端与WEB端两种使用场景. 本篇博客学习的markdown语法都是基于 ...
- TinyMCE的使用(包括汉化及本地图片上传功能)
TinyMCE我就不多介绍了,这是下载地址:https://www.tinymce.com/download/ 下载下来是英文版,要汉化也很简单.首先去网上随便下载个汉化包,然后把汉化包解压后的lan ...
- 【笔记】LAMP 环境无脑安装配置 Centos 6.3
p.p1 { margin: 0.0px 0.0px 5.0px 0.0px; font: 12.0px Times; color: #ff2500 } p.p2 { margin: 0.0px 0. ...
- 《C#微信开发系列(Top)-微信开发完整学习路线》
年前就答应要将微信开发的学习路线整理给到大家,但是因为年后回来这段时间学校还有公司那边有很多事情需要兼顾,所以没能及时更新文章.今天特地花时间整理了下,话不多说,上图,希望对大家的学习有所帮助哈. 如 ...
- 浅谈Hybrid技术的设计与实现
前言 浅谈Hybrid技术的设计与实现 浅谈Hybrid技术的设计与实现第二弹 浅谈Hybrid技术的设计与实现第三弹——落地篇 随着移动浪潮的兴起,各种APP层出不穷,极速的业务扩展提升了团队对开发 ...
- arcgis 许可异常的解决
异常现象: arcgis 许可服务管理器中无法重新读取许可,许可服务启动后立即停止. 解决方法: 1.卸载license:安装新的license!重新破解,替换license文件夹BI ...
- [备查]使用 SPQuery 查询 "Person or Group" 字段
原文地址:http://www.stum.de/2008/02/06/querying-the-person-or-group-field-using-spquery/ Querying the “P ...