HDU - 5651 xiaoxin juju needs help 逆元模板
http://acm.hdu.edu.cn/showproblem.php?pid=5651
题意:生成回文串。输出所有回文串的可能数。
题解:mod除法会损失高位,用逆元来代替除法,模板如下
ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string> using namespace std;
const int maxn = ;
int num[maxn];
//char s[maxn];
typedef long long ll;
const int mod = 1e9 + ;
ll f[maxn];
void init() {
f[] = ; f[] = ;
for (int i = ; i < maxn; i++)
f[i] = f[i - ] * i%mod;
}
ll cal(ll x) {
ll res = ;
int k = mod - ;
while (k) {
if (k & ) {
res *= x;
res %= mod;
}
x*=x;
x %= mod;
k >>= ;
}
return res;//cal(x^ mod)
}
int main() {
int t;
init();
cin >> t;
while (t--) {
memset(num, , sizeof(num));
string s;
cin >> s;
int n = s.length(); for (int i = ; i < n; i++) {
num[s[i]]++; }
int cnt = ;
for (int i = 'a'; i <= 'z'; i++) {
if (num[i] & ) cnt++;
}
if (cnt > ) {
cout << << endl; continue;
}
int sum = ;
for (int i = 'a'; i <= 'z'; i++) {
num[i] /= ;
sum += num[i];
}
ll res = f[sum];
for (int i ='a'; i <='z'; i++)if(num[i]) {
res = res*cal(f[num[i]]) % mod;
}
cout << res<<endl; }
}
HDU - 5651 xiaoxin juju needs help 逆元模板的更多相关文章
- HDU 5651 xiaoxin juju needs help 逆元
题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5651 bc:http://bestcoder.hdu.edu.cn/contests/con ...
- 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 数学
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/ ...
随机推荐
- 8 -- 深入使用Spring -- 4...4 Spring 的 AOP 支持
8.4.4 Spring 的AOP 支持 Spring中的AOP代理由Spring的IoC容器负责生成.管理,器依赖关系也由IoC容器负责管理.因此,AOP代理可以直接使用容器中的其他Bean实例作为 ...
- centos 7 安装 gcc-4.9.3.tar.gz
由于编译新内核需要,更新了GCC编译器,自行获取文件,手动升级. 首先是获取文件:wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gc ...
- Specified VM install not found: type Standard VM, name Java
Specified VM install not found: type Standard VM, name Java 下了一个新项目,使用SpringSource中执行ant脚本时,莫名提示以下错误 ...
- Python 管理 MySQL
Python MySQLdb 模块 Python pymysql 模块 Python SQLAlchemy 模块 Python ConfigParser 模块 Python 创建 MySQL 配置文件 ...
- Django SimpleCMDB 使用序列化
如下,前面我们是使用 urllib 方法来转换并传递数据的: [root@localhost ~]$ tail /data/script/getHostInfo.py if __name__ == ' ...
- Splash jsfunc() 方法
jsfunc()方法可以直接调用 JavaScript 定义的方法,但是所调用的方法需要用双中括号包围,这相当于实现了 JavaScript 方法到 Lua 脚本的转换 function main(s ...
- Apache Kafka 0.11版本新功能简介
Apache Kafka近日推出0.11版本.这是一个里程碑式的大版本,特别是Kafka从这个版本开始支持“exactly-once”语义(下称EOS, exactly-once semantics) ...
- ibatis 引入多个model
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "- ...
- codeforces水题100道 第十七题 Codeforces Beta Round #25 (Div. 2 Only) A. IQ test (brute force)
题目链接:http://www.codeforces.com/problemset/problem/25/A题意:在n个书中找到唯一一个奇偶性和其他n-1个数不同的数.C++代码: #include ...
- React Native 开发工具篇
正文 概述:开发RN的工具有很多,选择性也比较多,比如Facebook专门为React开发的IDE:Nuclide,还有做前端比较熟悉的WebStorm.Sublime Text 3.VS Code等 ...