gpt4 book ai didi

algorithm - 长字符串的排列

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:46:27 26 4
gpt4 key购买 nike

以下是我尝试解决的问题:

A string contains characters from '0' to '9'. Now find out the total possible permutations for this string. Since the number can be very large, output the number modulus 10^9+7.

Input : First line represent the number of test cases(T). Each test case contains a string of digits. Output : For each test case output the required result.

Constraints : T<=100. Length of String <= 1000

所以,我假设我只需要找出 len!其中 len 是字符串的长度。我找出阶乘的代码如下:

long long int fact(long long int n)
{
if(n<=1)
return 1;
else
return (len * fact(len-1))%1000000007;
}

但是我一直因为这个代码而收到 WA。忽略输出格式问题,上面用于找出阶乘的代码是否正确?或者我应该使用不同的方法?

注意:我还更改了算法,以便通过将答案除以每个字符出现的阶乘的乘积来处理某些字符的重复。

最佳答案

(a) 不只是 n!,只有 10 个可能的数字(在您的字符串中可能更少)。如果我给你字符串 "22",将只有 1 个排列。

(b) 最终的数字仍然会很大,如果没有 bignums,你将无法计算它。但是由于您只需要对一个相当小的数字取模,您可以使用 mod 的恒等式来保持中间数字较小。特别有用的是 a * b % k = ((a % k) * (b % k)) % k

希望这足以继续下去而不会破坏它。

关于algorithm - 长字符串的排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20919131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com