HDU 5651 逆元

时间:2022-07-20 23:11:56

xiaoxin juju needs help

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 809    Accepted Submission(s): 231

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≤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
 
Source
 
题意: 给一段只有小写字母的字符串 判断能够组合成多少种回文串
 
题解:首先,如果不止一个字符出现的次数为奇数,则结果为0。 否则,我们把每个字符出现次数除2,也就是考虑一半的情况。 那么结果就是这个可重复集合的排列数了。
A!/(n1!*n2!....)
这里直接除 会出错 需要用到逆元的知识
http://blog.csdn.net/acdreamers/article/details/8220787
另外如何求逆元?
 
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#define ll __int64
using namespace std;
int n;
char a[];
#define mod 1000000007
map<char,int> mp;
ll quickmod(ll a,ll b)
{
ll sum=;
while(b)
{
if(b&)
sum=(sum*a)%mod;
b>>=;
a=(a*a)%mod;
}
return sum;
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
mp.clear();
int jishu=;
int sum=;
scanf("%s",a);
int len=strlen(a);
for(int j=;j<len;j++)
mp[a[j]]++;
for(int j='a';j<='z';j++)
{
if(mp[j]%==)
{
jishu++;
}
}
if(len%==)
{
if(jishu!=)
{
cout<<""<<endl;
continue;}
}
else
{
if(jishu>)
{cout<<""<<endl;
continue;}
}
for(int j='a';j<='z';j++)
{
sum=sum+mp[j]/;
}
ll ans=;
ll gg=;
for(int j='a';j<='z';j++)
{
for(int k=;k<=mp[j]/;k++)
gg=gg*k%mod; }
for(int j=;j<=sum;j++)
{
ans=ans*j%mod;
}
printf("%I64d\n",(ans*(quickmod(gg,mod-)%mod))%mod);
}
return ;
}
 
 

HDU 5651 逆元的更多相关文章

  1. HDU 5651 计算回文串个数问题&lpar;有重复的全排列、乘法逆元、费马小定理&rpar;

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  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 逆元

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  4. HDU 5651 组合&plus;逆元

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...

  5. hdu 5651 xiaoxin juju needs help 逆元 两种求解方式

    xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  6. hdu 5651 重复全排列&plus;逆元

    知识点: n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列. 若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 ,即其重复度为ni! ...

  7. HDU 5651 xiaoxin juju needs help 数学

    xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...

  8. HDU 5651 xiaoxin juju needs help

    组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...

  9. hdu 1211 逆元

    RSA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. Random number

    Original #include <stdlib.h> #include <time.h> srand(time(NULL)); rand(); The versions o ...

  2. bsearch

    在java中为了避免 low+high溢出,可以用无符号右移:正数高位补0,负数高位补1 int mid = (low + high) >>> 1; 如果是在c++中,那么需要先转换 ...

  3. knockoutjs表格增加更新删除

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  4. ArcGis&colon;vs c&num;编程遇到问题The specified filename is invalid

    出现这个问题总归一点那就是你没有添加一个控件axLicenseControl这个控件导致报这个错误

  5. Visual Studio 2012 编译C&plus;&plus;显示cl命令

    为了用newlisp来实现VC编译,以便用我的Emacs开发VC程序,而不需要再打开VS 2012, 需要自己实现命令行的编译.我不需要nmake,因为我想直接了解VC编译器,以便今后更好的驾驭它. ...

  6. 关于ECSHOP模板架设的服务器php版本过高报错的解决方法(二)

    ECShop安装之后,在后台发现一个错误,这个错误提示的意思:mktime()方法不带参数被调用时,会被抛出一个报错提示. ECShop安装之后,在后台发现一个错误提示: Strict Standar ...

  7. 关于MongoDB数据库中文件唯一性的问题

    ※重要※——介绍一下我的环境:MongoDB的“win32-x86_64-2008plus-ssl-3.0.5”,MongoVUE版本是1.6.9,VS2010,dll是1.10版本. MongoDB ...

  8. Flask最强攻略 - 跟DragonFire学Flask - 第二篇 Flask 中的 Render Redirect HttpResponse

    1.Flask中的HTTPResponse 在Flask 中的HttpResponse 在我们看来其实就是直接返回字符串 2.Flask中的Redirect 每当访问"/redi" ...

  9. Unity shader学习之逐像素漫反射光照模型

    shader如下: Shader "Custom/Diffuse Fragment-Level" { Properties { _Diffuse (,,,) } SubShader ...

  10. ACdream 1025 bfs

    Transform Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit St ...