HDU 5651 xiaoxin juju needs help
程序员文章站
2022-08-13 20:30:29
xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K...
xiaoxin juju needs help
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1202Accepted Submission(s): 344
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
For each test case, there is a single line containing a string
Output For each test case, print an integer which is the number of watermelon candies xiaoxin's leader needs to buy after mod
Sample Input
3 aa aabb a
Sample Output
1 2 1
题意: 有一串字符 可以随意交换每个字符的顺序 输出能组成的回文串的个数
注意的是 求排列组合的时候不能直接取模相除 会WA
首先统计每个字符出现的次数,然后在进行排列组合
#include #include #include #include #include using namespace std; const int mod=1e9+7; typedef long long ll; //返回d=gcd(a,b);和对应于等式ax+by=d中的x,y ll extend_gcd(ll a,ll b,ll &x,ll &y) { if(a==0&&b==0) return -1;//无最大公约数 if(b==0){x=1;y=0;return a;} ll d=extend_gcd(b,a%b,y,x); y-=a/b*x; return d; } //*********求逆元素******************* //ax = 1(mod n) ll mod_reverse(ll a,ll n) { ll x,y; ll d=extend_gcd(a,n,x,y); if(d==1) return (x%n+n)%n; else return -1; } ll c(ll m,ll n) { ll i,j,t1,t2,ans; t1=t2=1; for(i=n;i>=n-m+1;i--) t1=t1*i%mod; for(i=1;i<=m;i++) t2=t2*i%mod; return t1*mod_reverse(t2,mod)%mod; } int main() { int n; char ch[1010]; int a[26]; scanf("%d",&n); while(n--) { memset(a,0,sizeof(a)); scanf("%s",ch); int len=strlen(ch); for(int i=0;i1) //出现两个奇数的字符则不可能组成回文串了 不需要判断字符串长度是否为奇偶 { printf("0\n"); } else { ll ans=1; int sum=len/2; for(int i=0;i<26;i++) //对每个字符进行位置的排列组合 { if(a[i]&1) a[i]--; ans=(ans*c(a[i]/2,sum))%mod; sum-=a[i]/2; } printf("%lld\n",ans); } } return 0; }