HDU 5651 xiaoxin juju needs help 数学

HDU 5651 xiaoxin juju needs help 数学


xiaoxin juju needs help

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5651

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

Hint

题意

给你一个串,你可以改变字符位置

问你能够形成多少种回文串。

题解:

首先把答案为0的情况判断掉

然后就很简单了,因为回文嘛,所以左右肯定相同

然后就可以排列组合怼一波了

就相当于选位置,把所有字母安上去。

C(x1,x2)*C(y1,y2)….这种

代码

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<cstring>
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e5+7;
int num[30];
typedef long long ll;
ll fac[maxn];
ll qpow(ll a,ll b)
{
    ll ans=1;a%=mod;
    for(ll i=b;i;i>>=1,a=a*a%mod)
        if(i&1)ans=ans*a%mod;
    return ans;
}
ll C(ll n,ll m)
{
    if(m>n||m<0)return 0;
    ll s1=fac[n],s2=fac[n-m]*fac[m]%mod;
    return s1*qpow(s2,mod-2)%mod;
}
void solve()
{
    memset(num,0,sizeof(num));
    string s;cin>>s;
    for(int i=0;i<s.size();i++)
        num[s[i]-'a']++;
    if(s.size()%2==0)
    {
        for(int i=0;i<26;i++)
            if(num[i]%2==1)
            {
                printf("0\n");
                return;
            }
    }
    else
    {
        int cnt = 0;
        for(int i=0;i<26;i++)
            if(num[i]%2==1)cnt++;
        if(cnt!=1)
        {
            printf("0\n");
            return;
        }
    }
    long long ans = 1;
    long long las = s.size();
    for(int i=0;i<26;i++)
    {
        ans = (ans * C(las/2,num[i]/2))%mod;
        las-=num[i];
    }
    printf("%lld\n",ans);
}
int main()
{
    fac[0]=1;
    for(int i=1;i<maxn;i++)
        fac[i]=fac[i-1]*i%mod;
    int t;scanf("%d",&t);
    while(t--)solve();
    return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109105.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • RangeValidator 控件

    RangeValidator 控件RangeValidator控件用于检测用户输入的值是否介于两个值之间。可以对不同类型的值进行比较,比如数字、日期以及字符。我们一般会用来验证输入的年龄或者考试的分数等。下面我们一块看看RangeValidator的属性:属性描述 BackColor 背景颜色 ControlToValidate

    2022年7月12日
    19
  • particles.js使用及配置

    particles.js使用及配置particles.js使用及配置参考:http://blog.csdn.net/csdn_yudong/article/details/53128570这个项目中有提供demo,可以直接下载这个

    2022年8月5日
    5
  • dex文件详解

    dex文件详解1、基本概念能被DVM虚拟机识别,加载并执行的文件格式2、生成dex文件1、通过IDE自动帮我们build生成2、手动通过dx命令去生成dex文件3、在手机上手动运行dex文件3、使用dx命令来生成dex文件首先得要先配置dx环境变量,这个环境变量怎么配呢?找到的sdk的安装目录,然后进入build-tools目录,这里是你电脑上的所有sdk的安装目录,随便选择一个进去,就可以知道dx.b

    2022年6月27日
    62
  • 2021.12goland激活码-激活码分享「建议收藏」

    (2021.12goland激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月30日
    89
  • error at hooking api ntprotect_read,match and write

    error at hooking api ntprotect_read,match and write 编译环境:delphi2010+windows7u,用途读取其他程序中readprocessmemory和writeprocessmemory的参数,但不知读取偏移即a+($b),b是怎么读的  一、用hook全局钩子线程钩子:已实现使用INLINEhookapi,CriticalSection临界区,dll分为动态loadlibry和静态加载问题1:对多线程目标

    2025年11月5日
    2
  • 对于多层神经网络,BP算法的直接作用_什么是多层神经网络

    对于多层神经网络,BP算法的直接作用_什么是多层神经网络多层神经网络BP算法原理及推导转载;https://www.cnblogs.com/liuwu265/p/4696388.html首先什么是人工神经网络?简单来说就是将单个感知器作为一个神经网络

    2022年8月3日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号