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/ ...
随机推荐
- 如何在 Arch Linux 的终端里设定 WiFi 网络
如果你使用的是其他 Linux 发行版 而不是 Arch CLI,那么可能会不习惯在终端里设置 WiFi.尽管整个过程有点简单,不过我还是要讲一下.在这篇文章里,我将带领新手们通过一步步的设置向导,把 ...
- IOS基础之 (十二) 类的扩展
对OC类的扩展总结如下,共有4个: 1.子类 subClass 作用:可以使用类的继承来增添父类的变量和方法. 写法:在.h文件中 @interface Student : Person 2.分类 C ...
- css中的id和css的区别
在样式表定义一个样式的时候,可以定义id也可以定义class. 1.在CSS文件里书写时,ID加前缀"#":CLASS用"." 2.id一个页面只可以使用一次: ...
- C# 抓取网页Html源码 (网络爬虫)
http://www.cnblogs.com/wxxian001/archive/2011/09/07/2169519.html 刚刚完成一个简单的网络爬虫,因为在做的时候在网上像无头苍蝇一样找资料. ...
- python 与 mysql
1.开发环境: 1)CLion-2016.1.3 C/C++ 与 Python 混合编程 IDE,先安装好以下 2) 3) 编译器再关联 2)tdm-gcc-4.8.1-3 C/C++ 编译器 3)W ...
- ping 和 traceroute 命令
ping 和 traceroute 命令 ping 程序 就是发送一个ICMP查询报文给某服务器,以测试该服务器是否可达. 当返回ICMP回显应答时,要打印出序列号.TTL,和往返时间: [roo ...
- cmd+lcx+nc+sc提权工具总结
cmd:执行命令的载体cmdshell lcx:端口映射工具 1.在自己的host上的cmd下运行:lcx.exe -listen 51 3389 //意思是监听51端口并转发到3389端口 2.在服 ...
- Data conversion error converting
词错如果出现在sql语句中,那么多半是类型转换的问题
- 反射中 GetCustomAttributes
public abstract object[] GetCustomAttributes(bool inherit); 这是GetCustomAttributes方法的一个重载,参数为bool类型返回 ...
- javascript 快速排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...