题目链接:

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. jedisLock—redis分布式锁实现(转)

    一.使用分布式锁要满足的几个条件: 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体 ...

  2. nodejs windows下安装运行

    node 官网下载地址http://nodejs.org/  下载自己对应的版本  ,我下的是windows版本 node-v4.1.1-x64.msi 然后 下一步 下一步 就完成安装了,非常简单, ...

  3. 关于Future

    1 为什么需要Callable和Future Runnable没有返回值,也不抛异常,这样主线程不能知道子线程的执行结果. 为了解决这个问题就有了Callable和Future.Callable提供的 ...

  4. iOS 运行时详解

    注:本篇文章转自:http://www.jianshu.com/p/adf0d566c887 一.运行时简介 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行 ...

  5. Image Recognition

    https://www.tensorflow.org/tutorials/image_recognition

  6. 开发者眼中的Spring与JavaEE

    JavaEE与Spring 在Java社区中,Spring与Java EE之争是个永恒的话题.在这场争论中,来自两个阵营的布道师.架构师 与铁杆粉丝都在不遗余力地捍卫着本方的尊严,并试图说服对方加入到 ...

  7. vue前戏ES6

    es6语法 es6语法:let和const: { var a=123; let b=234; } console.log(a); console.log(b); 浏览器里会只看到123; 而且还会抱一 ...

  8. Django之stark组件2

    action批量处理功能 用户可以自定义批量处理功能,但是默认的有批量删除功能. ***思路*** 1,定义一个列表用来装action的类名并extend用户自定义列表 2.循环该列表获取到函数名(用 ...

  9. hdoj 1455 Sticks 【dfs】

    题意:找最短的木棍可以组成的长度, hdoj  1518 的加强版 代码: #include <stdio.h> #include <string.h> #include &l ...

  10. 关于highcharts-ng

    1.内容都正确但是不显示,使用parseInt()方法转换