hdu 5651 组合数学+费马小定理求逆元
来源:互联网 发布:类似whatsapp的软件 编辑:程序博客网 时间:2024/06/06 17:52
http://acm.hdu.edu.cn/showproblem.php?pid=5651
给定字符串,判断任意交换顺序,能产生几个回文串。
先判定0,如果有超过一个字母出现的次数那么就是不可能的。
求个数就是用排列组合了。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define M 1009typedef long long ll;char s[M];int num[26];const int mod = 1000000007;ll qpow(ll x,ll n,ll mod){ ll ans = 1; while(n) { if(n & 1) ans = ans * x % mod; x = x * x % mod; n >>= 1; } return ans;}ll C(ll a,ll b){ ll s1 = 1, s2 = 1; for(int i = b - a + 1;i <= b;i++) s1 = s1*i%mod; for(int i = 1;i <= a;i++) s2 = s2*i%mod; ll inv = qpow(s2,mod-2,mod); return s1*inv%mod; //return (s1%(s2*mod))/s2;}int main(){ ios::sync_with_stdio(false); int T; cin >> T; while(T--) { cin >> s; memset(num,0,sizeof(num)); int len = strlen(s); for(int i = 0;i < len;i++) num[s[i] - 'a']++; int sum = 0; for(int i = 0;i < 26;i++) { if(num[i]&1) sum++; } if(sum > 1) { cout << 0 << endl; continue; } ll ans = 1; sum = len/2; //只看左半边 for(int i = 0;i < 26;i++) { if(!num[i]) continue; if(num[i] & 1) num[i]--; ans = ans*C(num[i]/2,sum)%mod; sum -= num[i]/2; //已经放过的位置要减掉 } cout << ans << endl; } return 0;}
0 0
- hdu 5651 组合数学+费马小定理求逆元
- Codeforces554C组合数学+费马小定理求逆元
- HDU 4704 Sum(费马小定理,组合数学,快速幂)
- hdu 3944 数学组合+帕斯卡定理
- HDU4869 组合数学+费马小定理求逆元+快速幂
- HDU 5651 组合数学
- hdu 3037 lucas定理-组合数学 hdu 4399
- hdu 5651 (组合数学 + 阶乘求逆元)
- hdu 4390 Number Sequence(组合数学+容斥定理)
- hdu 4366 Card Collector(组合数学:容斥定理)
- poj 1286 poj 2409 hdu 1812 polya定理 组合数学
- hdu 3037 Saving Beans(组合数学+lucas定理)
- 【组合数学--拉姆齐定理】hdu 6152 Friend-Graph
- HDU 3037 Saving Beans(组合数学+Lucas定理)
- 组合数学各种小定理
- hdu 3037 费马小定理+逆元求组合数+Lucas定理
- HDU4675【GCD of scequence】【组合数学、费马小定理、取模】
- 多校第一场 费马小定理+模拟+组合数学
- 请求码(requestCode)与结果码(resultCode)解析
- 回溯法
- 易语言学习第五课
- 最简单是使用viewpager的方法
- IDEA 中的简单单元测试配置
- hdu 5651 组合数学+费马小定理求逆元
- 微信支付--WeixinJSBridge--验证通过
- Android 布局属性 Android:layout_weight 总结
- java 抽象类和接口的理解
- IOS单元测试中的断言
- RecyclerView详细了解
- qnx修改ip地址方法
- Android Studio 几个非常有用的工具
- Java的静态方法继承相关