xiaoxin juju needs help

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5651

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≤20) which represents the number of test cases.

For each test case, there is a single line containing a string S(1≤length(S)≤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

Hint

题意

给你一个串,你可以改变字符位置

问你能够形成多少种回文串。

题解:

首先把答案为0的情况判断掉

然后就很简单了,因为回文嘛,所以左右肯定相同

然后就可以排列组合怼一波了

就相当于选位置,把所有字母安上去。

C(x1,x2)*C(y1,y2)....这种

代码

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e5+7;
int num[30];
typedef long long ll;
ll fac[maxn];
ll qpow(ll a,ll b)
{
ll ans=1;a%=mod;
for(ll i=b;i;i>>=1,a=a*a%mod)
if(i&1)ans=ans*a%mod;
return ans;
}
ll C(ll n,ll m)
{
if(m>n||m<0)return 0;
ll s1=fac[n],s2=fac[n-m]*fac[m]%mod;
return s1*qpow(s2,mod-2)%mod;
}
void solve()
{
memset(num,0,sizeof(num));
string s;cin>>s;
for(int i=0;i<s.size();i++)
num[s[i]-'a']++;
if(s.size()%2==0)
{
for(int i=0;i<26;i++)
if(num[i]%2==1)
{
printf("0\n");
return;
}
}
else
{
int cnt = 0;
for(int i=0;i<26;i++)
if(num[i]%2==1)cnt++;
if(cnt!=1)
{
printf("0\n");
return;
}
}
long long ans = 1;
long long las = s.size();
for(int i=0;i<26;i++)
{
ans = (ans * C(las/2,num[i]/2))%mod;
las-=num[i];
}
printf("%lld\n",ans);
}
int main()
{
fac[0]=1;
for(int i=1;i<maxn;i++)
fac[i]=fac[i-1]*i%mod;
int t;scanf("%d",&t);
while(t--)solve();
return 0;
}

HDU 5651 xiaoxin juju needs help 数学的更多相关文章

  1. HDU 5651 xiaoxin juju needs help 逆元

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

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

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

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

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

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

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

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

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

  6. HDU 5651 xiaoxin juju needs help

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

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

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

  8. hdu5651 xiaoxin juju needs help(逆元)

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

  9. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

随机推荐

  1. 工具===激活xmind 8

      [下载jar包]: https://stormxing.oss-cn-beijing.aliyuncs.com/files/XMindCrack.jar   方法: 打开xmind 8 安装目录的 ...

  2. 使用正则表达式匹配IP地址

    IP地址分为4段,以点号分隔.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配:   第一步:地址分析,正则初判 1.0-9 \d 进行匹配 2.10-99 [1-9]\d 进行匹 ...

  3. C语言实现int转换string

    #include <stdio.h> #include <stdlib.h> #include <string.h> int string2int(const ch ...

  4. final修饰的变量是引用不能改变还是引用的对象不能改变

    我们都知道final修饰变量时 会变为常量,但是使 用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变? 下面让我们来看这段代码: /** * 验证final修饰的变量是引用不能变, ...

  5. leetcode 之Valid Sudoku(七)

    判断行.列.九宫格内数字是否重复. 按照行.列.九宫格进行检查即可. bool validSudoku(const vector<vector<char>>& boar ...

  6. 数据库连接池(c3p0与druid)

    1.数据库连接池概念 其实就是一个容器(集合),存放数据库连接的容器.当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归 ...

  7. 远程连接 mysql 数据库连接不上的解决方案

    今天用Navicat访问虚拟机上的mysql,无法访问报cannot connect(10038). 首先看是否可以telnet,本机cmd,telnet 10.10.10.10 3306,结果是连接 ...

  8. 读书笔记|Windows 调试原理学习|持续更新

    关于调试方面的学习笔记,主要来源于<软件调试>的读书笔记和梦织未来论坛的视频教程 1.调试器使用一个死循环监听调试信息. DebugActiveProcess(PID);while(TRU ...

  9. 谷歌翻译python接口

    项目地址:  https://github.com/ssut/py-googletrans 安装: sudo pip install googletrans 使用: #!/usr/bin/python ...

  10. 浅谈css中浮动和清除浮动带来的影响

    有很多时候,我们都会用到浮动,而我们有时候对浮动只是一知半解,却不是太清楚它到底是怎么回事,不知道各位有没有和我一样的感觉,只知道用它,却不知道它到底是怎么回事,所以,在学习的过程中,就要把一个概念不 ...