HDU 5651 xiaoxin juju needs help 逆元
题目链接:
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651
bc:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=682&pid=1002
xiaoxin juju needs help
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?
This problem has multi test cases. First line contains a single integer T(T\leq 20)T(T≤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)S(1≤length(S)≤1,000).
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,0071,000,000,007.
3
aa
aabb
a
1
2
1
题解:
1、可行性:
统计每个字母出去的次数,如果有两种即以上字母出现的次数为奇数,则一定不可能排出回文串。
2、统计:
由于回文串左右两边必须相同,所以我们考虑一边就可以了(如果为奇数则正中间一个不管就和偶数情况是一样的了)。
则可以转化为排列组合问题,等价于求解: (cnt[i]代表某个字母出现的次数(cnt[i]>0) )
(
)%(1e9+7)
由于涉及到除法,要求逆元:
例子:
求a/b(mod n) 如果b与n互质,则求满足bx=1(%n)的一个解x,原式可转化为a/b*bx(%n),即a*x(%n);这样就把除法消除了。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn = + ;
const int mod = 1e9 + ;
typedef long long LL; char str[maxn];
int n; int cnt[];
int tmp[], tot; LL b[maxn];
//预处理出阶乘
void pre() {
b[] = b[] = ;
for (int i = ; i < maxn; i++) b[i] = (b[i - ] * i) % mod;
}
//扩展的欧几里得算法求逆元
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (!b) {
d = a;
x = ; y = ;
}
else {
gcd(b, a%b, d, y, x);//y=x',x=y';
//x=y'; y=x'-(a/b)*y';
y -= (a / b)*x;
}
} void init() {
tot = ;
memset(cnt, , sizeof(cnt));
} int main() {
pre();
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%s", str);
n = strlen(str);
for (int i = ; i < strlen(str); i++) {
cnt[str[i] - 'a']++;
}
int flag = ;
for (int i = ; i < ; i++) {
if (cnt[i] % ) flag++;
//统计一半的字母出现的次数
if (cnt[i] > ) {
tmp[tot++] = cnt[i] / ;
}
}
if (flag > ) { printf("0\n"); continue; }
LL sum = ;
for (int i = ; i < tot; i++) {
int t = tmp[i];
sum = (sum*b[t]) % mod;
}
LL d, x, y;
gcd(sum, mod, d, x, y);
//x有可能是负数,需要处理成正的。
x = (x%mod + mod) % mod;
LL ans = (b[n / ] * x) % mod;
printf("%lld\n", ans);
}
return ;
}
HDU 5651 xiaoxin juju needs help 逆元的更多相关文章
- hdu 5651 xiaoxin juju needs help 逆元 两种求解方式
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651 题意:生成回文串.输出所有回文串的可能数. 题解:mod除法会损失高位,用逆元来代替除法,模板如下 ac代 ...
- HDU 5651 xiaoxin juju needs help 数学
xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...
- HDU 5651 xiaoxin juju needs help (组合数)
xiaoxin juju needs helpTime Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64uSu ...
- HDU 5651 xiaoxin juju needs help
组合数杨辉三角打表,这样避免了除法求逆元. #include<cstdio> #include<cstring> #include<cmath> #include& ...
- HDU 5651 xiaoxin juju needs help 水题一发
分析:求一下组合数 首先,如果不止一个字符出现的次数为奇数,则结果为0. 否则,我们把每个字符出现次数除2,也就是考虑一半的情况. 那么结果就是这个可重复集合的排列数了. fact(n)/fact(a ...
- hdu5651 xiaoxin juju needs help(逆元)
xiaoxin juju needs help Accepts: 150 Submissions: 966 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- HDU 5651 逆元
xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
随机推荐
- Python中级 —— 03进程与线程
多任务的实现有3种方式: 多进程模式: 多线程模式: 多进程+多线程模式. ** 进程: ** 不同任务,例如打开一个写字本,就是开启一个新进程. 多进程 Unix/Linux操作系统提供了一个for ...
- ps基本认识
近来中意ui方面学习,从视频中总结了些许notses,希望能够帮到共同喜欢(❤ ω ❤)的友友 ·基础了解 位图:由像素组成的图片,把位图无限放大以后看到很多小方格,一个方格代表一个像素 矢量图:放大 ...
- 移动端H5页面解决软件键盘把页面顶起
在input失去焦点的时候加上强制页面归位 window.scroll(0,0); 上代码 <input data-component="SearchInput" type= ...
- leetcode记录-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- 【LG4103】[HEOI2014]大工程
[LG4103][HEOI2014]大工程 题面 洛谷 题解 先建虚树,下面所有讨论均是在虚树上的. 对于第一问:直接统计所有树边对答案的贡献即可. 对于第\(2,3\)问:记\(f[x]\)表示在\ ...
- MySQL入门篇(二)之常见命令管理
一.SQL结构化查询语言 SQL,英文全称Structured Query Language,中文意思是结构化查询语言.它是一种对关系数据库中的数据进行定义和操作的语言方法,是大多数关系数据库管理系统 ...
- 五、利用EnterpriseFrameWork快速开发基于WebServices的接口
回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 前面几章已完成EnterpriseFrameWo ...
- OpenCL入门:(一:Intel核心显卡OpenCL环境搭建)
组装的电脑没带独立显卡,用的是CPU自带的核显,型号是Intel HD Graphics 530,关于显卡是否可以使用OpenCL,可以下载GPU-Z软件查看. 本文在Windows 10 64位系统 ...
- shell loop
#!/bin/sh date i=0 while [ $i -le 30 ] do echi $i /usr/sbin/r2/np_test_acl -f rule.txt i=$(e ...
- exe4j 使用记录(二):jar打包exe
一.环境 exe4j: 6.0.2 jre(32位): 1.8 二.打包过程 1.新建一个文件夹testExe(我的目录位置:D:\testExe)用来存放所需要打成exe的jar包.jdk或者jre ...