题目链接:

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

bc:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=682&pid=1002

xiaoxin juju needs help

 Accepts: 150
 Submissions: 966
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
Problem 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)T(T≤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)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,0071,000,000,007.

Sample Input
3
aa
aabb
a
Sample Output
1
2
1

题解:

1、可行性:

统计每个字母出去的次数,如果有两种即以上字母出现的次数为奇数,则一定不可能排出回文串。

2、统计:

由于回文串左右两边必须相同,所以我们考虑一边就可以了(如果为奇数则正中间一个不管就和偶数情况是一样的了)。

则可以转化为排列组合问题,等价于求解: (cnt[i]代表某个字母出现的次数(cnt[i]>0) )

( )%(1e9+7)

由于涉及到除法,要求逆元:

例子:

  求a/b(mod n) 如果b与n互质,则求满足bx=1(%n)的一个解x,原式可转化为a/b*bx(%n),即a*x(%n);这样就把除法消除了。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = + ;
const int mod = 1e9 + ;
typedef long long LL; char str[maxn];
int n; int cnt[];
int tmp[], tot; LL b[maxn];
//预处理出阶乘
void pre() {
b[] = b[] = ;
for (int i = ; i < maxn; i++) b[i] = (b[i - ] * i) % mod;
}
//扩展的欧几里得算法求逆元
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) {
d = a;
x = ; y = ;
}
else {
gcd(b, a%b, d, y, x);//y=x',x=y';
//x=y'; y=x'-(a/b)*y';
y -= (a / b)*x;
}
} void init() {
tot = ;
memset(cnt, , sizeof(cnt));
} int main() {
pre();
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%s", str);
n = strlen(str);
for (int i = ; i < strlen(str); i++) {
cnt[str[i] - 'a']++;
}
int flag = ;
for (int i = ; i < ; i++) {
if (cnt[i] % ) flag++;
//统计一半的字母出现的次数
if (cnt[i] > ) {
tmp[tot++] = cnt[i] / ;
}
}
if (flag > ) { printf("0\n"); continue; }
LL sum = ;
for (int i = ; i < tot; i++) {
int t = tmp[i];
sum = (sum*b[t]) % mod;
}
LL d, x, y;
gcd(sum, mod, d, x, y);
//x有可能是负数,需要处理成正的。
x = (x%mod + mod) % mod;
LL ans = (b[n / ] * x) % mod;
printf("%lld\n", ans);
}
return ;
}

HDU 5651 xiaoxin juju needs help 逆元的更多相关文章

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

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

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

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

  3. HDU 5651 xiaoxin juju needs help 数学

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

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

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

  5. HDU 5651 xiaoxin juju needs help

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

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

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

  7. hdu5651 xiaoxin juju needs help(逆元)

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

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

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

  9. HDU 5651 逆元

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

随机推荐

  1. linux 命令 sort

    Linux下的sort排序命令详解(一) 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比较,比较原则是从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出. [zook ...

  2. HTML5 添加视频和音频(响应式视频)

    最初的 HTML5规范呼吁所有浏览器内置支持使用 Ogg格式① 直接播放视频或音频(无需插件).但是由于 HTML5工作组的内部争议,曾经作为基线标准的支持 Ogg(包括 Theoravideo 和 ...

  3. Delphi无边框Form拖动

    用Delphi做登陆窗口,如果使用无边框Form,想要拖动窗口,可以在某个控件的OnMouseDown事件中写下以下代码 ReleaseCapture; Perform(WM_SYSCOMMAND, ...

  4. 树莓派安装DNSMASQ服务

    功能: DNSMASQ是一款高性能的.小型的DNS服务器软件. * 缓存域名.IP地址,本地缓存解析的地址. * DHCP服务 1.安装 sudo apt-get update sudo apt-ge ...

  5. Office 365部分安装及同时安装Visio的方法

    From MWeb Win版本的Office 365安装包默认安装所有组件,没有选择的页面,在安装Office 365后再安装下载的Visio 2016专业版时,会显示计算机上已经安装了即插即用Off ...

  6. python2.7入门---多线程

        多线程类似于同时执行多个不同程序,多线程运行有如下优点: 使用线程可以把占据长时间的程序中的任务放到后台去处理. 用户界面可以更加吸引人,这样比如用户点击了一个按钮去触发某些事件的处理,可以弹 ...

  7. C语言 迭代部分的代码编写

    C语言代码学习 迭代部分 迭代要用到函数部分的知识,一开始我写了计算n!的计算,代码和运行结果如下: 结果只能单一的计算出整数内的值,如果输入负值则结果为返回值1,显然是不对的,根据查书学习以后,知道 ...

  8. WPF 窗口句柄获取和设置

    原文:WPF 窗口句柄获取和设置 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83347163 ...

  9. 深度学习开源库tiny-dnn的使用(MNIST)

    tiny-dnn是一个基于DNN的深度学习开源库,它的License是BSD 3-Clause.之前名字是tiny-cnn是基于CNN的,tiny-dnn与tiny-cnn相关又增加了些新层.此开源库 ...

  10. MySQL入门篇(六)之mysqldump备份和恢复

    一.备份单个数据库 1.备份命令:mysqldump MySQL数据库自带的一个很好用的备份命令.是逻辑备份,导出 的是SQL语句.也就是把数据从MySQL库中以逻辑的SQL语句的形式直接输出或生成备 ...