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/ ...
随机推荐
- {"errorCode":50} 的解决办法
# 无反爬 import urllib.parse import urllib.request import json content = input('请输入需要翻译的词语:') # url = ' ...
- 中间件安全加固之Jboss
JBoss 的安全设置 1) jmx-console A.jmx-console登录的用户名和密码设置 默认情况访问 http://localhost:8080/jmx-console 就可以浏览jb ...
- iOS开发-iOS7禁用手势返回
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // 禁用 iOS7 返回手势 if ([self.nav ...
- iOS开发--时间戳问题
什么是时间戳? 时间戳(timestamp),通常是一个字符序列,唯一地标识某一刻的时间.数字时间戳技术是数字签名技术一种变种的应用. 思考:简单来讲就是根据文件hash加密后生成的摘要和时间生成的时 ...
- OSG3.4编译FFMPEG插件
0.加入你要读a.mp4,那个正确的写法是osg::Image* image = osgDB::readImageFile("a.mp4.ffmpeg"); 1.在github上下 ...
- GDI+ gif文件的显示和格式转换
GDI+ gif文件的显示和格式转换 gdi+imagedeletenulltiff GDI+ gif文件的显示和格式转换 怎么获取gif文件的每一帧,并且显示出来呢? 1.怎么用gid+显示gi ...
- Struts2(三)配置详解
一.概述 Struts2提供了多种可选的配置文件形式. 其中,struts-default.xml和default.properties是框架级别的配置文件,这两个文件在Struts的核心JAR包中, ...
- 【LeetCode OJ】Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that app ...
- Sqlserver Sequence操作
USE [database_test] GO --创建SEQUENCE CREATE SEQUENCE defaultSequence AS INT --设置开始行 START --自增量 INCRE ...
- jQuery队列(三)
看了一下队列剩下的几个方法,在没有应用场景的情况下,对它所做的一些处理不能明白.后续希望可以通过动画部分代码的阅读能搞清楚这些处理的意义.jQuery.fn.extend({ // 推迟队列中函数的执 ...