题目链接:

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. less的编译

    less其实也文本类型,跟txt的性质差不多 less有自己语法(变量,函数,作用域.Mixin混入),使css样式更加方便,有逻辑性,提高可维护性,减少重复性代码的冗余. 把less编译成css文件 ...

  2. Redis 4.0.X版本reshard出现错误的解决办法

    原文链接:https://my.oschina.net/juluking/blog/1606222 原作者的版本是Redis 4.0.6,我的版本是4.0.8,所以猜测是否所有4.0.x版本都有此问题 ...

  3. Delphi获取Android下GPS的NMEA 0183数据

    下面的程序,可以实现Android下获取GNSS的NMEA0183数据: unit utAndroidNmea; interface uses Androidapi.JNIBridge, Androi ...

  4. T+API HTTPServer服务端

    该服务端是一个HTTP服务器,这样其他语言调用也方便. 出于某些原因,只支持Post方法,不打算支持其他方法,例如Get. API所接受的参数将以Json传送,回传的数据也是一个Json数据,一切只是 ...

  5. 2016-2017-2015329 《Java程序设计》第4周学习总结

    学号 2016-2017-2015329 <Java程序设计>第4周学习总结 教材学习内容总结 面向对象有三大特性:封装.继承.多态 封装 封装是指,一种将抽象性函式接口的实例细节部份包装 ...

  6. 前端 layui

    如果想实现类似 alert这种效果又不想阻塞html运行的话,就是用layer吧! http://www.layui.com/

  7. logstash处理@timestamp时区

    input { stdin { } } filter { #ruby { # code => "event.set('timestamp', event.get('@timestamp ...

  8. 安装centos minimal 版本后安装setup包(linux)

    网络配置好后,输入命令 yum install setuptool,安装过程有两个确认,输入Y即可

  9. 我们一起学习WCF 第二篇WCF承载多个接口

    前言:现在王大叔养了大批猪,赚了很多钱.但是最近发现养鸡也可以赚很多钱,他就像扩展业务开始养鸡.又过两年他发现市场对狗的需求量很大,他开始养狗.那么他改怎么做呢,不可能去修改猪住的地方把鸭子和狗放里面 ...

  10. javaweb(三十九)——数据库连接池

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...