HDU 5651 xiaoxin juju needs help 数学
xiaoxin juju needs help
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5651
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≤20) which represents the number of test cases.
For each test case, there is a single line containing a string 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,007.
Sample Input
3
aa
aabb
a
Sample Output
1
2
1
Hint
题意
给你一个串,你可以改变字符位置
问你能够形成多少种回文串。
题解:
首先把答案为0的情况判断掉
然后就很简单了,因为回文嘛,所以左右肯定相同
然后就可以排列组合怼一波了
就相当于选位置,把所有字母安上去。
C(x1,x2)*C(y1,y2)....这种
代码
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e5+7;
int num[30];
typedef long long ll;
ll fac[maxn];
ll qpow(ll a,ll b)
{
ll ans=1;a%=mod;
for(ll i=b;i;i>>=1,a=a*a%mod)
if(i&1)ans=ans*a%mod;
return ans;
}
ll C(ll n,ll m)
{
if(m>n||m<0)return 0;
ll s1=fac[n],s2=fac[n-m]*fac[m]%mod;
return s1*qpow(s2,mod-2)%mod;
}
void solve()
{
memset(num,0,sizeof(num));
string s;cin>>s;
for(int i=0;i<s.size();i++)
num[s[i]-'a']++;
if(s.size()%2==0)
{
for(int i=0;i<26;i++)
if(num[i]%2==1)
{
printf("0\n");
return;
}
}
else
{
int cnt = 0;
for(int i=0;i<26;i++)
if(num[i]%2==1)cnt++;
if(cnt!=1)
{
printf("0\n");
return;
}
}
long long ans = 1;
long long las = s.size();
for(int i=0;i<26;i++)
{
ans = (ans * C(las/2,num[i]/2))%mod;
las-=num[i];
}
printf("%lld\n",ans);
}
int main()
{
fac[0]=1;
for(int i=1;i<maxn;i++)
fac[i]=fac[i-1]*i%mod;
int t;scanf("%d",&t);
while(t--)solve();
return 0;
}
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 helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...
- 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& ...
- hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)
题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 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,··· ...
随机推荐
- WordPress手机端插件——WPtouch
戒微博之后,把更多的精力开始转投回网站上来:今天用nexus7访问@Bee君 的博客时,发现博客的界面与电脑上访问的界面不相同,顺藤摸瓜之后发现原来bee君使用的是WPtouch-pro插件来实现移动 ...
- 牛B的日本精神
在汤森路透评选出的<2015全球创新企业百强>榜单里,日本以40家高居榜首,力压美国的35家.而中国内地无一入围. 在中国媒体上,我们见到的日本是“失去的20年”,经济衰退.创新能力丧 ...
- ab的使用方法【转】
使用方法 ab -n 800 -c 800 http://192.168.0.10/ (-n发出800个请求,-c模拟800并发,相当800人同时访问,后面是测试url) ab -t 60 -c 1 ...
- iOS 取消按钮高亮显示方法
objective-C 第1种方法: 设置按钮的normal 与 highlighted 一样的图片, 不过如果你也需要selected状态下的图片, 就不能这么做, 这样做在取消选中状态的时候就会显 ...
- [ python ] 作业:选课系统
功能代码实现源地址:https://www.cnblogs.com/lianzhilei/p/5832691.html 如有侵权,立即删除 本文主要是分析 选课系统 实现思路及上面代码的实现过程 ...
- asp基础
0.1在浏览器中通过查看源代码的方式是无法看到 ASP 源代码的,你只能看到由 ASP 文件输出的结果,而那些只是纯粹的 HTML 而已.这是因为,在结果被送回浏览器前,脚本已经在服务器上执行了. 0 ...
- Hadoop HDFS 单节点部署方案
初学者,再次记录一下. 确保Java 和 Hadoop已安装完毕(每个人的不一定一样,但肯定都有数据,仅供参考) [root@jans hadoop-2.9.0]# pwd /usr/local/ha ...
- LoadRunner中常用的字符串操作函数
LoadRunner中常用的字符串操作函数有: strcpy(destination_string, source_string); strc ...
- eclipse重的自动提示与行号和快捷图标的显示
显示行号:Window->Preferences->Gerenal->Editors->Text Editors然后在show line number上打对勾自动提示:Wind ...
- 使用afl-dyninst fuzz无源码的二进制程序
转:http://ele7enxxh.com/Use-AFL-dyninst-To-Fuzz-Blackbox-Binaries.html 使用afl-dyninst fuzz无源码的二进制程序 通常 ...