这道题很容易卡时间,最后求概率要用递推法求,暴力求会超时
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const ll N = 1e3;
ll fac[N];
int a[15];
double ans[N];
bool cmp(char a, char b)
{
return a>b;
}
void factorial()
{
fac[0] = 1;
for(int i=1; i<=10; i++)
{
fac[i] = fac[i-1] * i;
}
}
int main()
{
factorial();
int T;
cin>>T;
while(T--)
{
string s;
cin>>s;
int len = s.length();
for(int i=0; i<len; i++)
{
a[i] = s[i] - '0';
}
int n = 0;
while(next_permutation(a, a+len))
{
n++;
}
if(n==0)
{
double a = 0;
printf("%.9f\n", a );
continue;
}
else
{
double temp = 1.0 / fac[len];
double x, ans;
x = ans = temp;
for(int i=0; i<n-1; i++)
{
ans = x + x * temp;
x = ans;
}
printf("%.9f\n", ans);
}
}
return 0;
}