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

Submit Status

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 (多重集的全排列+逆元)的更多相关文章

  1. hdu5651 xiaoxin juju needs help(逆元)

    xiaoxin juju needs help  Accepts: 150  Submissions: 966  Time Limit: 2000/1000 MS (Java/Others)  Mem ...

  2. hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)

    题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  3. HDU 5651 xiaoxin juju needs help (组合数)

    xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...

  4. hdu 5651 xiaoxin juju needs help 逆元 两种求解方式

    xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  5. HDU 5651 xiaoxin juju needs help 逆元

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  6. HDU 5651 xiaoxin juju needs help 数学

    xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...

  7. HDU 5651 xiaoxin juju needs help 水题一发

    分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...

  8. HDU 5651 xiaoxin juju needs help

    组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  9. HDU - 5651 xiaoxin juju needs help 逆元模板

    http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...

随机推荐

  1. 爬虫入门——01

    1. 引言     从今天开始系统的学习网络爬虫.写这篇博客的目的在于,一来记录下自己的学习过程:二来希望可以给像我一样不懂爬虫但又对爬虫十分感兴趣的人带来一些帮助.     昨天去图书馆找有关爬虫书 ...

  2. C# XML转JSON,不引用第三方JSON.NET类库

    应用场景:需要调用第三方接口(返回XML)数据,然后供自己多个系统使用(涉及跨域,使用JSONP) 代理:调用接口(把XML转换为JSONP解决跨域问题) B/S应用系统:调用代理返回的数据进行UI显 ...

  3. 计算机程序的思维逻辑 (49) - 剖析LinkedHashMap

    之前我们介绍了Map接口的两个实现类HashMap和TreeMap,本节来介绍另一个实现类LinkedHashMap.它是HashMap的子类,但可以保持元素按插入或访问有序,这与TreeMap按键排 ...

  4. git for windows 的默认工作路径(HOME)的设置以及Git与GitHub之间的SSH加密协议

    1.安装及配置默认路径 Windows中从GitHub上面:https://git-for-windows.github.io/ 下载安装好git后(双击,改一下需要的安装路径,一路确定就好了),安装 ...

  5. JavaScript中尺寸、坐标

    测试环境是IE8,Chrome38,Firefox40,下面是全局通用脚本打印代码 /** * 打印 */ function write(str) { document.write(str + '&l ...

  6. Linux内核启动过程概述

    版权声明:本文原创,转载需声明作者ID和原文链接地址. Hi!大家好,我是CrazyCatJack.今天给大家带来的是Linux内核启动过程概述.希望能够帮助大家更好的理解Linux内核的启动,并且创 ...

  7. UITableViewCell单元格的删除、插入、移动

    UITableViewDelegate的方法      设置编辑模式中得cell的编辑样式(删除或插入)      - (UITableViewCellEditingStyle)tableView:( ...

  8. .NET 同步与异步之封装成Task(五)

    本随笔续接:.NET 实现并行的几种方式(四) 前篇随笔已经介绍了几种可以实现并发的方式,其中异步方法.是最简便的方式.而 异步方式是基于 Task 和 async修饰符和await运算符实现的. 换 ...

  9. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  10. C# if中连续几个条件判断

    C# if中连续几个条件判断 1.if (条件表达式1 && 条件表达式2) 当条件表达式1为true时 using System; using System.Collections. ...