题目链接:

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. python迭代器生成器

    1.生成器和迭代器.含有yield的特殊函数为生成器.可以被for循环的称之为可以迭代的.而可以通过_next()_调用,并且可以不断返回值的称之为迭代器 2.yield简单的生成器 #迭代器简单的使 ...

  2. 笔记本电脑、VM虚拟机、开发板三者网线连接互ping

    笔者在做NFS挂接练习时,发现网上的资料大部分是笔记本电脑(以下简称PC)和虚拟机PING.PC和开发板PING,这样的方式不是我想要的.笔者需要使用无线网卡上网,使用有线网卡进行三者互PING.在开 ...

  3. 【深度优先搜索】NOIP2017_D2T1 洛谷3958奶酪

    这道题的写法大体有两种:大法师DFS和并查集,两种算法都不难,本篇博客主要讲解DFS,而且测试数据特水,连个剪枝都不用都可以过. 题目描述[luogu传送门] 现有一块大奶酪,它的高度为 h,它的长度 ...

  4. c语言单向链表逆转实现方法

    自己理解的思路如下所示: 从第二个节点开始,先记录下一个节点,把第二个节点移到头节点之前,头节点变为移动的这个节点之前记录的节点变为接下来要移动的节点用for循环重复最后把原来头节点变成尾节点(*ne ...

  5. R语言学习笔记—组合数

    组合数:从m个不同元素中取出n(n≤m)个元素的所有组合的个数,叫做从m个不同元素中取出n个元素的组合数. 代码: str_comb <- function(vector){ n <- l ...

  6. kaggle之员工离职分析

    本文探讨的是kaggle中的一个案例-员工离职分析,从数据集中分析员工的离职原因,并发现其中的问题.数据主要包括影响员工离职的各种因素(工资.绩效.工作满意度.参加项目数.工作时长.是否升职.等)以及 ...

  7. java入门---运算符&算术运算符&自增自减运算符&关系运算符&位运算符

        计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量.我们可以把运算符分成以下几组: 算术运算符 关系运算符 位运算符 逻辑运算符 赋值运算符 ...

  8. 成都Uber优步司机奖励政策(4月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  9. day2 HTML - body

    <body>内常用标签 1.基本标签 所有标签分为: #  块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标,  ...

  10. spring源码-aop动态代理-5.3

    一.动态代理,这是一个很强大的东西哦.研发过程中我们会常用很多业务类,但是存在一个问题.如何在不修改源码逻辑的情况下,加入自己的相关逻辑.比如异常处理,日志记录等! 二.Java动态代理的两种方式JD ...