xiaoxin juju needs help HDU - 5651 (有重复的全排列,逆元)

(回文串全排列个数) 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 h... 阅读详情

 xiaoxin juju needs help

HDU - 5651
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 T(T≤20) which represents the number of test cases.
For each test case, there is a single line containing a string S(1≤length(S)≤1,000)
.
Output
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,007
.
Sample Input
3
aa
aabb
a
Sample Output
1
2
1


题意:给你一个字符串,求打乱字符后,有多少种回文串

 

知识点:

    n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列。

    若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 clip_image002[4],即其重复度为ni!

    同理a1重复了n1次,a2重复了n2次,····,ak重复了nk次,n1+n2+····+nk=n。

    对于这样的n个元素进行全排列,可得不同排列的个数实际上是 clip_image001

   

题解:

字符串长度为len,若每个元素的重复次数ni为奇数的个数count>1,则无法形成回文串。

当能构成回文串时,我们只需考虑这个回文串左半部分的情况,所以这个问题也就变成了求一半字符串的有重复的全排列。

因为涉及到除法取模的问题,所以用到逆元。

逆元可以用扩展欧几里得,或费马小定理。/*6.逆元*/

为什么要求逆元:因为

费马小定理:取模运算的等阶式不存在除号的运算只能用乘号,所以要求逆元而不是直接除再取模


code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define MOD 1000000007
typedef long long ll;
int num[30];
ll q_pow(ll a,ll b){
	ll ans = 1;
	ll x = a % MOD;
	while(b){
		if(b&1)
		   ans = ans * x % MOD;
		b >>= 1;
		x = x * x % MOD;
	}
	return ans;//快速幂取模运算 
}

ll getInverse(ll n){
	ll i;
	ll x = 1;
	for(i = 1; i <= n; i++){
		x = x * i % MOD;//先求出这个重复度,即出现次数的阶乘 
	}
	return q_pow(x,MOD-2);//利用费马小定理求逆元 
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		string s;
		cin >> s;
		memset(num,0,sizeof(num));
		ll i;
		for(i = 0; i < s.length(); i++){
			num[s[i]-'a']++;//记录下每个字母出现的次数 
		}
		int odd = 0;
		int flag = 0;
		for(i = 0; i < 27; i++){
			if(num[i]&1)odd++;
			if(odd > 1){
				flag = 1;//统计出现奇数次的字母个数,超过1个就不能构成回文,直接进行下一次输入 
				printf("0\n");
				break;
			}
		}
		if(flag)continue;
		ll ans = 1;
		for(i = 1; i <= s.length()/2; i++){
			ans = ans * i % MOD;//先求所有的阶乘,是长度的一半,因为只看一半就可以了 
		}
		for(i = 0; i < 27; i++){
			if(num[i] > 0){
				ll x = getInverse((ll)num[i]/2);//求逆元,注意这里传进去的参数是个数的一半,因为只看一半就可以了 
				ans = ans * x % MOD;//依次除去重复度 
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}


xiaoxin juju needs help 已支持(Java & JS & Python & C & C++),不重复的全排列数量,大数运算,除法取模,乘法逆元,费马小定理,快速幂 阅读详情

相关推荐

工业界实战:小红书推荐系统中的AB测试策略解析——分层、Holdout与反转实验

本文深入解析了小红书推荐系统在工业界实践中采用的AB测试核心策略。重点阐述了分层实验如何解决多实验并行与干扰问题,Holdout机制如何作为评估黄金标准提供稳定基准,以及反转实验如何作为全量上线后的终极安全网进行风险控制。这些策略共同构成了科学、严谨的线上评估体系,是保障推荐系统迭代效果与决策质量的关键。

transformer2023的博客 483

HDOJ 5651-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 h

小I的博客 534

创客学院嵌入式驱动开发——学习资料汇总

1.创客学院知识巩固-01C语言回顾_C/C++_Set_Mode的博客-CSDN博客https://blog.csdn.net/Set_Mode/article/details/94431050 2.创客学院知识巩固-02数据结构_Set_Mode的博客-CSDN博客https://blog.csdn.net/Set_Mode/article/details/94431087 3.创客学院知...

snaking616 5125

杭电5651 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...

weixin_41370251的博客 341

VFIO框架源码分析(二十一)- IOMMUFD 模式下的 DMA 映射机制

在虚拟化 I/O 的世界里,无论上层管理面(如对象模型、设备绑定)如何重构,数据面最核心的任务始终未变——DMA 映射(DMA Mapping)。即如何建立 Guest Physical Address (GPA) 到 Host Physical Address (HPA) 的映射关系,并将其填入 IOMMU 的硬件页表中,使得直通设备能够正确访问虚拟机内存。在 Legacy VFIO (Type1) 时代,这一职责由承担,它通过ioctl 操作 Container。

qq_36071928的博客 1028

URP Shader编程之自定义后处理单pass渲染(URP 14.x)

❌ 不要在里拿✅ 只在里拿RendererFeature 生命周期RTHandle 设计Blitter 的真实语义Shader 输入来源。

qq1342753906的博客 614

hdu5651--A - xiaoxin juju needs help 组合数问题

A - xiaoxin juju needs help Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description As we all known, xiaoxin is a brilliant coder. He k

扬帆起航 530

手把手教你用分立元件搭建自举电路:解决高端MOS驱动难题

本文详细介绍了如何用分立元件搭建自举电路,解决高端MOS驱动中的栅源极电压不足问题。通过面包板实验,逐步讲解二极管、电容等关键元件的选型与电路搭建,提供波形调试和常见问题排查方法,帮助工程师提升开关电源和电机驱动等应用的效率与可靠性。

weixin_30555125的博客 160

HDU 5651xiaoxin juju needs help 带模除法逆元+有重复元素的全排列值

点击打开链接 xiaoxin巨从小就喜欢字符串,六年级的时候他就知道了什么是回文串。这时,xiaoxin巨说到:如果一个字符串 SSS 是回文串,那么该字符串从前往后看和从后往前看是一样一样的。 六年级的暑假,xiaoxin很快就做完了暑假作业,然后到腾讯做起了实习生。这日,leader给了xiaoxin一个字符串,请xiaoxin帮忙写一个函数来生成所有可能的回文串,可以任意改变字符串的顺序

became_a_wolf的专栏 458

统信UOS下获取root权限的两种姿势:图形化开启开发者模式 vs 命令行直接提权

本文详细解析了统信UOS下获取root权限的两种方法:通过图形界面永久开启开发者模式和使用命令行临时提权。开发者模式适合系统级开发,提供完整root权限但降低安全性;临时提权则更灵活,适合日常管理任务。文章还对比了两种方案的优缺点,帮助用户根据需求选择最佳权限管理策略。

weixin_30266885的博客 540

【Unity大规模RTS/攻城战框架】Large Scale Combat System - RTS Sieges深度解析

本文介绍了Unity专业级大规模RTS战斗框架Large Scale Combat System(LSCS)。该系统通过Job System+Burst技术解决了万人级单位的性能问题,提供完整的RTS操作体系、动态阵型系统、战术AI和攻城战功能。其亮点包括可破坏城门、行走城墙、战术AI配置系统,支持URP渲染管线,适用于Total War风格的大规模兵团对战项目。LSCS不仅提供核心战斗系统,还包含UI、小地图等配套模块,是Unity中少有的专业级RTS解决方案,特别适合需要深度战术玩法和大规模战斗的开发者

2403_88403568的博客 1357

HDU5651 xiaoxin juju needs help 组合

xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 293    Accepted Submission(s): 87 Problem Description As we all

Jonariguez 吾本优秀 589

Android AICore的核心组件工作原理解析

AICore通过LoRA实现个性化适配Gemini Nano提供多模态智能Safety Features保障可信运行,构建了端侧AI的完整生态。这种设计不仅让手机等移动设备能处理复杂AI任务(如实时多模态交互),还通过隐私保护机制打破了传统云端AI的数据依赖,为未来“设备即AI基础设施”的愿景奠定了基础。输入触发:用户输入“刚提交了代码,等审核”→ 进入TEE加密处理。Gemini Nano基础生成:通过原始权重W_q生成初始回复候选。LoRA个性化微调:通过A'和B'计算ΔW,更新W_q。

1572

BestCoder Round #77 1002.xiaoxin juju needs help(HDU5651)

[HDU5651.xiaoxin juju needs help(http://acm.hdu.edu.cn/showproblem.php?pid=5651) xiaoxin巨从小就喜欢字符串,六年级的时候他就知道了什么是回文串。这时,xiaoxin巨说到:如果一个字符串 SS 是回文串,那么该字符串从前往后看和从后往前看是一样一样的。 六年级的暑假,xiaoxin很快就做完了暑假

Void main with out return 631

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

xiaoxin juju needs help   题意:给你一个字符串,求打乱字符后,有多少种回文串。                      (题于文末)   知识点:     n个元素,其中a1,a2,····,an互不相同,进行全排列,可得n!个不同的排列。     若其中某一元素ai重复了ni次,全排列出来必有重复元素,其中真正不同的排列数应为 ,即

Strangedbly 909

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

hdu 5651 (求回文组合数)

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 lead

黑码 547

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

HDU 5651 xiaoxin juju needs help(组合数学)

xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1657    Accepted Submission(s): 499 Problem Description As

Calm微笑 824

hdu 5651(逆元+排列组合)

xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 984    Accepted Submission(s): 276 Problem Description As we al

saber_acher的博客 1174

有重复组合公式及其证明方法

有重复组合公式如下: 若在n个元素中有重复的选择r个元素的公式: Crn+r−1Cn+r−1rC_{n+r-1}^{r} 这个公式的证明有很多种方法,这里只选取最容易理解的方式进行证明: 证明如下: 把n个元素当成n个顺序摆放的盒子,r是r个完全相同的球,这样从n个元素中有重复取r个元素的方法就转化成,把r个同质球放入n个盒子的方法 为什么可以这样呢,想想,把一个球放到第i个盒...

codeswarrior的博客 2万+

Character Encoding HDU - 6397(容斥原理解决方程解个数经典问题详解)

Character Encoding HDU - 6397 In computer science, a character is a letter, a digit, a punctuation mark or some other similar symbol. Since computers can only process numbers, number codes are used t...

codeswarrior的博客 1999

POJ 3904 Sky Code(容斥原理)

Sky Code Stancu likes space travels but he is a poor software developer and will never be able to buy his own spacecraft. That is why he is preparing to steal the spacecraft of Petru. There is only o...

codeswarrior的博客 1289

Pave the Parallelepiped(有重复组合公式+状态压缩)

Pave the Parallelepiped You are given a rectangular parallelepiped with sides of positive integer lengths A, B and C . Find the number of different groups of three integers (a , b, c) such that 1≤...

codeswarrior的博客 1075

cf 997c Sky Full of Stars(组合数+容斥原理)

Sky Full of Stars On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game. It is well known that one month on this planet consists of n2 days, so calen...

codeswarrior的博客 937

HDU 2841 Visible Trees(容斥原理+解释)

Visible Trees There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see. If two trees and Sherlock are ...

codeswarrior的博客 812
上一篇: 迷宫城堡 HDU - 1269 (强连通分量模板题)
下一篇: Cube HDU - 1220(数学)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址