poj--5651

xiaoxin juju needs help

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1164    Accepted Submission(s): 335


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(T20)  which represents the number of test cases.
For each test case, there is a single line containing a string  S(1length(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
 

Source

BestCoder Round #77 (div.2)


题目大意:当字符串中含有的字符,其个数为奇数的字符种类>1时,输出0;剩下的就可以理解成给你一串含有重复字母的字符串,问有多少种组合方式.

用到公式n!/(pa!*pb!*pc!......pz!),其中在这题中n=l/2,p?=?字符的总数除以2。因为数很大,所以可以边乘边取模,但是分子不能直接除以分母,需要用到逆元的知识。


代码如下:

#include<cstdio>
#include<string.h>
#define mod 1000000007
char s[1010];
int num[27];
long long power[600];
void inin(){
	int i;
	power[0]=1;
	for(i=1;i<=500;i++){
		power[i]=power[i-1]*i%mod;
	}
} 
void gcd(long long a, long long  b,  long  long &x, long long  &y) {
    if (!b) {
        
        x = 1; y = 0;
    }
    else {
        gcd(b, a%b, y, x);//y=x',x=y';
        //x=y'; y=x'-(a/b)*y';
        y -= (a / b)*x;
    }
}
int main(){
	int t,i,j,l,v;
	long long ans;
	long long sum;
	inin();
	scanf("%d",&t);
	while(t--){
		scanf("%s",s);
		l=strlen(s);
		memset(num,0,sizeof(num));
		for(i=0;i<l;i++){
			num[s[i]-'a']++;
		}
		v=0;sum=1;
		for(i=0;i<26;i++){
			if(num[i]){
				if(num[i]%2)
				v++;
				sum=sum*power[num[i]/2]%mod;
			}
			if(v>=2){
				break;
			}
		}
		if(v>=2)printf("0\n");
		else {
			long long  x,y;
			//printf("%lld\n",ans/sum%mod);逆元定理 
			gcd(sum,mod,x,y);
			x = (x%mod + mod) % mod;
            long long  ans = (power[l / 2] * x) % mod;
            printf("%lld\n", ans);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值