HDU 5651 xiaoxin juju needs help 逆元
2016-05-24 10:12
323 查看
xiaoxin juju needs help
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d &
%I64u
Submit
Status
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\leq 20) which
represents the number of test cases.
For each test case, there is a single line containing a string S(1 \leq length(S) \leq
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)
给你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!*(num[i]!)^(p-2)%mod
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d &
%I64u
Submit
Status
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\leq 20) which
represents the number of test cases.
For each test case, there is a single line containing a string S(1 \leq length(S) \leq
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)
给你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!*(num[i]!)^(p-2)%mod
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #define mod 1000000007 using namespace std; typedef long long LL; int C[550][550]; int main() { memset(C,0,sizeof(C)); for(int i=0;i<=505;i++) { C[i][1]=i; C[i][0]=1; } for(int i=2;i<=500;i++) for(int j=2;j<=i;j++) { C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod; } //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { LL ans=1; int i,len,cnt=0; char s[1050]; scanf("%s",s); len=strlen(s); int a[400],b[400],j=0; memset(a,0,sizeof(a)); for(i=0;i<len;i++) { a[s[i]]++; } for(i='a';i<='z';i++) { if(a[i]) { b[j++]=a[i]/2; } if(a[i]%2!=0) cnt++;//奇数个字符的个数 } if(cnt>1) { printf("0\n"); continue; } int h=len/2; for(i=0;i<j;i++) { ans=(ans*C[h][b[i]])%mod; h-=b[i]; } printf("%I64d\n",ans); } return 0; }
相关文章推荐
- HDU 1521 排列组合 指数型母函数
- Maven安装与环境配置(Windows)
- HDU 1023 Traning Problem (2) 大数卡特兰数
- HDU 2082 母函数模板题
- HDU 5317 RGCDQ
- E sum
- HDU 2588 GCD
- Codeforces7C 扩展欧几里得
- POJ 1006 - Biorhythms (中国剩余定理)
- java的若干问题(3)——多线程
- POJ 2478 Farey Sequence(欧拉函数前n项和)
- HDU 1452 Happy 2004
- c语言实现二叉树的先序遍历,中序遍历,后序遍历
- LightOJ 1234 Harmonic Number
- SGU 106 The equation
- 初探TypedArray和AttributeSet
- github pages部署静态网页
- iOS学习之——UIImagePickerController从拍照、图库、相册获取图片
- angularJS如何让控制器与控制器进行通信,个人理解。
- 20160524 数据分析与SAS1 数据模型