题目链接:

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. 初识Modbus TCP/IP-------------C#编写Modbus TCP客户端程序(一)

    转自:http://blog.csdn.net/thebestleo/article/details/52269999 首先我要说明一下,本人新手一枚,本文仅为同样热爱学习的同学提供参考,有不 对的地 ...

  2. 白昼夢 / Daydream(模拟)

    C - 白昼夢 / Daydream Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You ...

  3. windowsphone8.1学习笔记之Toast通知

    熟悉或者了解wp系统的人都知道wp的Toast,Toast(吐司)通知是在屏幕最顶上弹出来的提示框,它专为与锁屏提醒.磁贴通知和应用中UI结合使用而设计,但它不是MessageDialog.说简单点它 ...

  4. 7.FactoryBean 和BeanFactory去区别

    FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...

  5. php在不同平台下路径分隔符不同的解决办法

    在看phpamf时看到一个常量“DIRECTORY_SEPARATOR”,最后发现是一个全局的常量,用来定义路径分隔符的 主要解决在windows和linux下路径分隔符不同的造成代码不通用的问题,在 ...

  6. lua2json

    lua对象json序列化,很简单,没做中文的unicode编码了 local function bool2json(v) return v and "true" or " ...

  7. Android系统移植与调试之------->MTK 标准编译命令

    命令格式:./maketek [option] [project] [action] [modules]Option:   -t ,-tee :输出log信息到当前终端   -o , -opt=-- ...

  8. 分析DNS解析时间

    提高网页的打开速度,一般地我们会选择使用CDN,利用“就近原则”让用户在最短的时间内获取到服务器资源,实际应用当中采用CDN的方式提高网站访问速度的效果也是最明显的.这也就是为什么国外的空间打开速度远 ...

  9. ecmobile实现支付宝支付和百度云推送遇到的问题及解决方案(android)

    1.首先检测支付账户是否开通快捷支付服务,如果开通后,那么公钥是否上传(支付宝问题一定要找支付宝客服解决,找其他人没有用,支付宝客服可以帮你分析底层原因) 2.修改app配置文件:alipay_cal ...

  10. 每天一个Linux命令(23)chmod命令

        chmod命令用来变更文件或目录的权限. 在UNIX系统家族里,文件或目录权限的控制分别以读取.写入.执行3种一般权限来区分,另有3种特殊权限可供运用.用户可以使用chmod指令去变更文件与目 ...