题目链接:

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. java心跳发送

    java心跳发送: 大家都知道.如果你在互联网公司,并且开发的是产品那你一定接触不到.心跳机制.心跳包 那什么是心跳机制呢? 心跳机制就是定时发送一个自定义的结构体(心跳包).确保连接的有效的机制. ...

  2. 大数据hadoop之zookeeper

    一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...

  3. WEBserver、应用程序server、HTTPserver差别

    WEBserver.应用程序server.HTTPserver差别 WEBserver.应用程序server.HTTPserver有何差别?IIS.Apache.Tomcat.Weblogic.Web ...

  4. mysql ODBC连接配置

    1.软件包安装 [root@server1 mysql]# rpm -ivh MySQL-server-5.1.72-1.glibc23.i386.rpm [root@server1 mysql]# ...

  5. unity中动态生成网格

    以下是绘制正方形面片的一个例子,方便之后查阅: 效果如图所示: 红轴为x方向,蓝轴为z方向. 代码如下: using System.Collections; using System.Collecti ...

  6. hihoCoder #1388 : Periodic Signal

    NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...

  7. mysql 不同库不同表字段数据复制

    需求:把一个表某个字段内容复制到另一张表的某个字段. 实现sql语句1: UPDATE file_manager_folder f1 LEFT OUTER JOIN file_manager_fold ...

  8. EventBus的使用详解,功能为在Fragment,Activity,Service,线程之间传递消息

    最近跟同事用到了EventBus的使用,之前不太了解EventBus,查阅资料发现EventBus还挺好用的,用法比较简单,下面就把我看到的关于EventBus的博客分享给大家,里面介绍了很多的使用详 ...

  9. H - Funny Car Racing

    H - Funny Car Racing Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Desc ...

  10. CentOS7.1安装 Vsftpd FTP 服务器

    # yum install vsftpd 安装 Vsftpd FTP 编辑配置文件 ‘/etc/vsftpd/vsftpd.conf’ 用于保护 vsftpd. # vi /etc/vsftpd/vs ...