提交时间:2022-07-20 11:50:56
运行 ID: 52708
#include <iostream> #include <iomanip> #include <algorithm> using namespace std; int ar[21],k[21],n; int solve() { int cnt = 0; for(int i = 1;i <= n;i++) k[i] = ar[i]; for(int i = 1;i <= n;i++) { while(k[i] != i) { swap(k[i],k[k[i]]); cnt++; } } return cnt; } int main() { int a,b,i,j; cin>>n>>a>>b; for(i = 1;i <= n;i++) cin>>ar[i]; if(solve() == 0) { cout<<fixed<<setprecision(9)<<0.0; return 0; } else if(b > solve() * a) { double ans = 0; ans = solve() * a; cout<<fixed<<setprecision(9)<<ans<<endl; } else { long double ans = 0; long long jc = 1; for(i = 1;i <= n;i++) jc*=i; for(i = 1;i <= n;i++) ar[i] = i; do { ans+=solve(); }while(next_permutation(ar + 1,ar + n + 1)); ans*=a; ans+=((n - 1) * b); ans = ans * 1.0 / jc; if(jc * b < ans) ans = jc * b; cout<<fixed<<setprecision(9)<<ans; } return 0; }