HDU 5651 逆元

xiaoxin juju needs help

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

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

Source

BestCoder Round #77 (div.2)

题意: 给一段只有小写字母的字符串 判断能够组合成多少种回文串

题解:首先,如果不止一个字符出现的次数为奇数,则结果为0。 否则,我们把每个字符出现次数除2,也就是考虑一半的情况。 那么结果就是这个可重复集合的排列数了。

A!/(n1!*n2!....)

这里直接除 会出错 需要用到逆元的知识

http://blog.csdn.net/acdreamers/article/details/8220787

另外如何求逆元?

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<map>
 5 #define ll __int64
 6 using namespace std;
 7 int n;
 8 char a[1005];
 9 #define mod 1000000007
10 map<char,int> mp;
11 ll quickmod(ll a,ll b)
12 {
13     ll sum=1;
14     while(b)
15     {
16         if(b&1)
17             sum=(sum*a)%mod;
18         b>>=1;
19         a=(a*a)%mod;
20     }
21     return sum;
22 }
23
24 int main()
25 {
26     scanf("%d",&n);
27     for(int i=1;i<=n;i++)
28     {
29         mp.clear();
30         int jishu=0;
31         int sum=0;
32         scanf("%s",a);
33         int len=strlen(a);
34         for(int j=0;j<len;j++)
35             mp[a[j]]++;
36         for(int j=‘a‘;j<=‘z‘;j++)
37         {
38             if(mp[j]%2==1)
39              {
40              jishu++;
41              }
42         }
43         if(len%2==0)
44         {
45             if(jishu!=0)
46             {
47             cout<<"0"<<endl;
48             continue;}
49         }
50         else
51         {
52             if(jishu>1)
53             {cout<<"0"<<endl;
54             continue;}
55         }
56       for(int j=‘a‘;j<=‘z‘;j++)
57       {
58            sum=sum+mp[j]/2;
59       }
60       ll ans=1;
61       ll gg=1;
62       for(int j=‘a‘;j<=‘z‘;j++)
63       {
64       for(int k=1;k<=mp[j]/2;k++)
65          gg=gg*k%mod;
66
67       }
68       for(int j=1;j<=sum;j++)
69       {
70           ans=ans*j%mod;
71       }
72       printf("%I64d\n",(ans*(quickmod(gg,mod-2)%mod))%mod);
73     }
74     return 0;
75 }

时间: 2024-10-08 18:30:44

HDU 5651 逆元的相关文章

hdu 5651 重复全排列+逆元

知识点: n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列. 若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 ,即其重复度为ni! 同理a1重复了n1次,a2重复了n2次,····,ak重复了nk次,n1+n2+····+nk=n. 对于这样的n个元素进行全排列,可得不同排列的个数实际上是  由于题目要求是对100000007取余 同余定理中对于同一个除数,两个数的乘积与它们余数的乘积同余.但这里有除法所以得用上逆元 逆元 定义

HDU 5651 xiaoxin juju needs help 逆元

给你n个字母,求可以组成的回文串的个数 1.n为奇数,有一个字母的个数为奇数 2.n为偶数,字母个数全为偶数 然后将字母的个数num[i]/2,得出在对称轴左边的个项字母的个数 假设左边有len个字母,如果每个字母都不同则有len!中可能 然后除去所有重复的可能num[i]!即可 因为除法取模 (len!/num[i]!)%mod a^(p-1) = 1(mod p)p为素数    于是 a*a^(p-2) = 1(mod p)所以a^(p-2)替代1/a. 所以上面的公式  ->  len!*

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =

HDU 5651 xiaoxin juju needs help(BestCoder Round #77 (div.1)1001)

传送门 xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 243 Problem Description As we all known, xiaoxin is a brilliant coder. He knew **palin

hdu 5685(逆元)

Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 520    Accepted Submission(s): 203 Problem Description 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串的

HDU 5651 xiaoxin juju needs help

组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<algorithm> using namespace std; const long long MOD=1000000007; const int maxn=1000+10; long long c[maxn][maxn]; int tot[30]; c

HDU 4828 逆元+catalan数

Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 953    Accepted Submission(s): 418 Problem Description 度度熊最近很喜欢玩游戏.这一天他在纸上画了一个2行N列的长方形格子.他想把1到2N这些数依次放进去,但是为了使格子看起来优美,他想找到使每行每列都递增的方案.不过画了很

hdu 1211 逆元

RSA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2353    Accepted Submission(s): 1677 Problem Description RSA is one of the most powerful methods to encrypt data. The RSA algorithm is describ

HDU 5651

题意: 给你一个字符串,可以打乱顺序,问可以组合成几种回文串. 其实就是给你一些字母,让你组合回文串. 首先判断能不能成一个回文串,其次计算一共有几种方法. len为奇数:只有一个字母为奇数,其余必须为偶数. len为偶数:所有的字母都要是偶数个. 计算回文串的方法是:一共有len/2个位置可选. 运用组合数:每次从剩余的位置中选择需要的字母个数,乘起来就可以了. 代码: #include <stdio.h> #include <string.h> #include <mat