HDU 5651 xiaoxin juju needs help (组合数)
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
Source
BestCoder Round #77 (div.2)
给你n个字母,求可以组成的回文串的个数
只有满足以下条件才可以组成回文串。
1.n为奇数,有一个字母的个数为奇数
2.n为偶数,字母个数全为偶数
然后将字母的个数num[i]/2,得出在对称轴左边的个项字母的个数
假设左边有len个字母,如果每个字母都不同则有len!中可能
然后除去所有重复的可能num[i]!即可
因为除法取模 (len!/num[i]!)%mod
#include <iostream>
#include <cstring>
using namespace std;
#define mod 1000000007
typedef long long ll;
ll c[][];
void get()
{
memset(c,,sizeof(c));
c[][]=; //会涉及到
for(int i=;i<=;i++)
{
c[i][]=;
c[i][]=i;
}
for(int i=;i<=;i++)
for(int j=;j<=i;j++)
c[i][j]=(c[i-][j]+c[i-][j-])%mod;
}
int main()
{
get();
int t;
cin>>t;
while(t--)
{
char str[];
int a[];
memset(a,,sizeof(a));
cin>>str; //不要重复定义c
int len=strlen(str);
for(int i=;i<len;i++)
a[str[i]-'a']++;
int flag=-;
for(int i=;i<;i++)
{
if(a[i]%)
flag++;
a[i]/=;
}
//cout<<flag<<endl;
if(flag>=) //注意 -1 为真!!!
{
cout<<<<endl; //输出一个正整数的时候前面不能加0 否则会错
}
else
{
len/=;
//cout<<len<<a[0]<<a[1]<<endl;
ll ans=;
for(int i=;i<;i++)
{
ans=ans*c[len][a[i]]%mod;
len-=a[i];
}
cout<<ans<<endl;
}
}
return ;
}
HDU 5651 xiaoxin juju needs help (组合数)的更多相关文章
- HDU 5651 xiaoxin juju needs help 逆元
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- HDU 5651 xiaoxin juju needs help 数学
xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...
- hdu 5651 xiaoxin juju needs help 逆元 两种求解方式
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...
- HDU 5651 xiaoxin juju needs help 水题一发
分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...
- HDU 5651 xiaoxin juju needs help
组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- hdu5651 xiaoxin juju needs help(逆元)
xiaoxin juju needs help Accepts: 150 Submissions: 966 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- HDU 5651 逆元
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
随机推荐
- POJ-2299 Ultra_QuickSort 线段树+逆序对数
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50737 Accepted: 18595 Des ...
- Java输入、输入、IO流 类层次关系梳理
本文主要关注在Java编程中涉及到的IO相关的类库.方法.以及对各个层次(抽线.接口继承)的流之间的关系进行梳理 相关学习资料 http://baike.baidu.com/view/1007958. ...
- SPOJ Play on Words
传送门 WORDS1 - Play on Words #graph-theory #euler-circuit Some of the secret doors contain a very inte ...
- linux内存回收 内核参数
ss -atu| awk '/^tcp/{++S[$2]} END {for(a in S) print a,S[a]}' ps up pid (RSS:实际内存大小,长驻内存) ps o pid,c ...
- 初学JDBC,获取插入记录的主键、执行批量操作
一.获取插入记录主键值 在创建语句的地方使用Statement.RETURN_GENERATED_KEYS标识一下,然后通过getGeneratedKeys方法获得 preparedStatement ...
- Hadoop的datanode无法启动
Hadoop的datanode无法启动 hdfs-site中配置的dfs.data.dir为/usr/local/hadoop/hdfs/data 用bin/hadoop start-all.sh启动 ...
- 10.11 pod 安装
http://blog.csdn.net/youtk21ai/article/details/48896043
- Struts2几种传值
1.url向action传值 url为 http://localhost/txyl/teacher_info?method:teacher_info&teacher_seq=dedafdsf3 ...
- JS实现打字机式字符输出效果
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- C++标准库异常类
C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报 分类: c/c++(36) C++标准库异常类继承层次中的根类为exception,其定义在excep ...