题目链接:

xiaoxin juju needs help

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 491    Accepted Submission(s): 142

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
题意:问给的字符串能形成多少个不同的回文串;
思路:因为回文,所以都是取一半计算,n=len/2,a[i]=a[i]/2;(a[i]为第i个字母出现的次数)ans=n!/(a[i]!*a[j]!*a[k]!*..)(i,j,k...都是大于0的a[]);麻烦之处就是阶乘会爆掉,除法%mod我又不会,所以想了这个gcd暴力约分的方法;
AC代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=1e9+;
char str[];
int a[],b[];
int gcd(int x,int y)
{
if(y==)return x;
return gcd(y,x%y);
}
queue<int>qu;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
while(!qu.empty())qu.pop();
memset(a,,sizeof(a));
scanf("%s",str);
int len=strlen(str);
for(int i=;i<len;i++)
{
a[str[i]-'a']++;
}
int flag=;
long long ans=;
for(int i=;i<;i++)
{
if(a[i]%)
{
flag++;
}
a[i]=a[i]/;
}
for(int i=;i<=len/;i++)
{
b[i]=(long long)i;
}
//memset(vis,0,sizeof(vis));
for(int i=;i<;i++)
{
if(a[i]>)
{
for(int j=;j<=a[i];j++)
{
qu.push(j);
}
}
}
while(!qu.empty())
{
int x=qu.front();
qu.pop();
for(int i=;i<=len/;i++)
{
if(b[i]%x==)
{
b[i]/=x;
break;
}
else
{
int y=gcd(b[i],x);
if(y>)
{
b[i]/=y;
x/=y;
if(x>)qu.push(x);
break;
}
}
} }
for(int j=;j<=len/;j++)
{
ans*=(long long)b[j];
ans%=mod;
}
if(flag>)cout<<""<<"\n";
else cout<<ans<<"\n"; } return ;
}

hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)的更多相关文章

  1. HDU 5651 xiaoxin juju needs help 数学

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

  2. HDU 5651 xiaoxin juju needs help 逆元

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

  3. HDU 5651 xiaoxin juju needs help (组合数)

    xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...

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

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

  5. HDU - 5651 xiaoxin juju needs help 逆元模板

    http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...

  6. HDU 5651 xiaoxin juju needs help 水题一发

    分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...

  7. HDU 5651 xiaoxin juju needs help

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

  8. hdu5651 xiaoxin juju needs help(逆元)

    xiaoxin juju needs help  Accepts: 150  Submissions: 966  Time Limit: 2000/1000 MS (Java/Others)  Mem ...

  9. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

随机推荐

  1. 安装Struts2 类库

    现在,如果一切正常,那么你可以继续设置您的Struts 2框架.以下是简单的步骤,下载并安装在机器上Struts2. 请选择是否要安装Hibernate在Windows或Unix,然后继续进行下一个步 ...

  2. 动态创建Lambda表达式实现高级查询

    需求简介 最近这几天做的东西总算是回归咱的老本行了,给投资管理项目做一个台账的东西,就是类似我们的报表.其 中有一个功能是一个高级查询的需求,在查询条件方面大概有7.8个查询条件.需求就是如果一个条件 ...

  3. apache常用模块介绍

      mod_actions 基于媒体类型或请求方法,为执行CGI脚本而提供 mod_alias 提供从文件系统的不同部分到文档树的映射和URL重定向 mod_asis 发送自己包含HTTP头内容的文件 ...

  4. EL 表达式 函数 操作 字符串

    <%@tablib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ${fn ...

  5. [Delphi]解决Delphi Distiller运行报错"HKEY_CURRENT_USER\\" is of wrong kind or size

    最近终于决心将使用多年的Delphi 7升级到Delphi 2007,虽然目前Delphi最高版本已经是XE8,但对于只做VCL开发的话还是喜欢2007这个经典的版本. 安装Delphi 2007一切 ...

  6. 基于SQLAIchemy的Flask目录

    预先知识 flask的基本使用 快速搭建开发的目录,以后我们在用Flask开发项目的时候可以直接用这个目录,不需要再自己创建. flask-sqlalchemy flask-sqlalchemy相当于 ...

  7. CENTOS7 修改网卡名称为eth[012...],格式

    具体操作是修改/etc/default/grub文件 在GRUB_CMDLINE_LINUX一行中添加net.ifnames=0 biosdevname=0 保存文件后然后运行 grub2-mkcon ...

  8. tomcat调试页面的时候,不刷新

    1.调试一个页面的时候,js文件不刷新,也就是相当于没有改(cache) 2.解决:在Server中将当前tomcat的模式改为debug即可

  9. js基础--substr()和substring()的区别

    最近做项目的时候,字符串截取一直用的是substr()方法,有时候需要截取的内容是中间部分的话就很麻烦,需要分两次,第一次截取前半部分,第二次在第一次的基础上截取后半部分.写了几次之后总觉得没对,应该 ...

  10. vim下的ctags和taglist等的使用和配置

    1.ctags (1)到 http://prdownloads.sourceforge.net/ctags/ctags-5.6.tar.gz         下载ctags源码ctags-5.6.ta ...