Problem:
Problem C:
GIGA Universe Cup
Following FIFA World Cup, a larger competition called ``GIGA Universe Cup'' is taking place somewhere in our universe. Both FIFA World Cup and GIGA Universe Cup are two rounds competitions that consist of the first round, also known as ``group league,'' and the second called ``final tournament.'' In the first round, participating teams are divided into groups of four teams each. Each team in a group plays a match against each of the other teams in the same group. For example, let's say we have a group of the following four teams, ``Engband, Swedon, Argontina, and Nigerua.'' They play the following six matches: Engband - Swedon, Engband - Argontina, Engband - Nigerua, Swedon - Argontina, Swedon - Nigerua, and Argontina - Nigerua.
The result of a single match is shown by the number of goals scored by each team, like ``Engband 1 - 0 Argontina,'' which says Engband scored one goal whereas Argontina zero. Based on the result of a match, points are given to the two teams as follows and used to rank teams. If a team wins a match (i.e., scores more goals than the other), three points are given to it and zero to the other. If a match draws (i.e., the two teams score the same number of goals), one point is given to each.
The
goal difference
of a team in given matches is the total number of goals it scored minus the total number of goals its opponents scored in these matches. For example, if we have three matches ``Swedon 1 - 2 Engband,'' ``Swedon 3 - 4 Nigerua,'' and ``Swedon 5 - 6 Argontina,'' then the goal difference of Swedon in these three matches is (1 + 3 + 5) - (2 + 4+ 6) = -3.
Given the results of all the six matches in a group, teams are ranked by the following criteria, listed in the order of priority (that is, we first apply (a) to determine the ranking, with ties broken by (b), with ties broken by (c), and so on).
(a) greater number of points in all the group matches;
(b) greater goal difference in all the group matches;
(c) greater number of goals scored in all the group matches.
If two or more teams are equal on the basis of the above three criteria, their place shall be determined by the following criteria, applied in this order:
(d) greater number of points obtained in the group matches between the teams concerned;
(e) greater goal difference resulting from the group matches between the teams concerned;
(f) greater number of goals scored in the group matches between the teams concerned;
If two or more teams are stiIl equal, apply (d), (e), and (f) as necessary to each such group. Repeat this until those three rules to equal teams do not make any further resolution. Finally, teams that still remain equal are ordered by:
(g) drawing lots by the Organizing Committee for the GIGA Universe Cup.
The two teams coming first and second in each group qualify for the second round.
Your job is to write a program which, given the results of matches played so far in a group and one team specified in the group, calculates the 
...(truncated)

Submitted solutions:

code_snippet: #include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

vector<double> p(9);

vector<vector<string>> get(vector<pair<pair<string,string>,pair<int,int>>> S){

	map<string,vector<int>> mp;
	vector<int> base(3,0);
	rep(i,S.size()){
		mp[S[i].first.first]=base;
		mp[S[i].first.second]=base;
	}
	
	rep(i,S.size()){
		string TeamA = S[i].first.first;
		string TeamB = S[i].first.second;
		int A = S[i].second.first;
		int B = S[i].second.second;
		
		if(A==B){
			mp[TeamA][0]++;
			mp[TeamB][0]++;
		}
		else if(A>B){
			mp[TeamA][0]+=3;
		}
		else{
			mp[TeamB][0]+=3;
		}
		
		mp[TeamA][1]+=A-B;
		mp[TeamB][1]+=B-A;
		
		mp[TeamA][2]+=A;
		mp[TeamB][2]+=B;
	}
	
	vector<pair<vector<int>,string>> V;
	for(auto a:mp){
		V.emplace_back(a.second,a.first);
	}
	
	sort(V.rbegin(),V.rend());
	
	vector<vector<string>> ret;
	rep(i,V.size()){
		if(i==0 || V[i-1].first!=V[i].first){
			ret.push_back(vector<string>(1,V[i].second));
		}
		else ret.back().push_back(V[i].second);
	}
	
	if(ret.size()==1)return ret;
	vector<vector<string>> temp;
	rep(i,ret.size()){
		if(ret[i].size()==1){
			temp.push_back(ret[i]);
			continue;
		}
		sort(ret[i].begin(),ret[i].end());
		vector<pair<pair<string,string>,pair<int,int>>> nS;
		rep(j,S.size()){
			string x = S[j].first.first,y = S[j].first.second;
			if(binary_search(ret[i].begin(),ret[i].end(),x)&&binary_search(ret[i].begin(),ret[i].end(),y)){
				nS.push_back(S[j]);
			}
		}
		vector<vector<string>> X = get(nS);
		rep(j,X.size()){
			temp.push_back(X[j]);
		}
	}
	
	return temp;
	
}

double get(vector<string> S){
	vector<string> names(4);
	string target;
	rep(i,4){
		names[i] = S[i+1].substr(1,3);
		if(S[i+1][0]=='*'){
			target = names[i];
		}
	}

	vector<pair<pair<string,string>,pair<int,int>>> matches;
	vector<int> emp;
	rep(i,4){
		rep(j,4){
			if(i>=j)continue;
			char c0 = S[i+1][6+j*5],c1 = S[i+1][6+j*5+2];
			if(c0=='_'){
				emp.push_back(matches.size());
				matches.emplace_back(make_pair(names[i],names[j]),make_pair(-1,-1));
			}
			else matches.emplace_back(make_pair(names[i],names[j]),make_pair(c0-'0',c1-'0'));
		}
	}

	double ret = 0.0;
	rep(i,9){
		rep(j,9){
			rep(k,9){
				rep(l,9){
					
					matches[emp[0]].second.first = i;
					matches[emp[0]].second.second = j;
					matches[emp[1]].second.first = k;
					matches[emp[1]].second.second = l;
					double P = p[i]*p[j]*p[k]*p[l];
					vector<string> temp;
					vector<vector<string>> result = get(matches);
					if(result[0].size()==1){
						if(result[0][0]==target){
							ret += P;
							continue;
						}
						else{
							temp = result[1];
						}
					}
					else temp = result[0];
					
					bool f = false;
					rep(x,temp.size()){
						if(temp[x]==target){
							f=true;
						}
					}
					if(f){
						if(result[0].size()==1)ret += P / (double)temp.size();
						else{
							double s = temp.size();
							ret += P * (1.0-(s-1.0) * (s-2.0) / s / (s-1.0));
						}
					}
					
				}
			}
		}
	}
	
	return ret;
	
}

int main(){
	
	rep(i,9){
		p[i] = pow(0.25,i);
		p[i] *= pow(0.75,8-i);
		rep(j,8){
			p[i] *= j+1;
		}
		rep(j,i){
			p[i] /= j+1;
		}
		rep(j,8-i){
			p[i] /= j+1;
		}
	}
	
	int t;
	cin>>t;
	
	rep(_,t){
		vector<string> S(5);
		rep(i,5)cin>>S[i];
		
		cout<<fixed<<setprecision(10)<<get(S)<<endl;
	}
	
    return 0;
}
accuracy: 1
time_ms: 1040
memory_kb: 3800
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

int M[4][4];
double score[9];
double p;

int check(int n,vector<int> a,int id){
	int m=a.size();
	vector<int> P(m),GD(m),GS(m);
	for(int i=0;i<m;i++){
		for(int j=i+1;j<m;j++){
			int k=a[i],l=a[j];
			if(M[k][l]>M[l][k])P[i]+=3;
			else if(M[k][l]==M[l][k])P[i]++,P[j]++;
			else P[j]+=3;
			GD[i]+=M[k][l]-M[l][k],GD[j]+=M[l][k]-M[k][l];
			GS[i]+=M[k][l],GS[j]+=M[l][k];
		}
	}
	vector<pair<pair<int,int>,pair<int,int>>> v;
	for(int i=0;i<m;i++){
		v.push_back(make_pair(make_pair(P[i],GD[i]),make_pair(GS[i],a[i])));
	}
	sort(v.rbegin(), v.rend());
	int cnt=0; bool flag=false;
	vector<int> na;
	for(int i=0;i<m;i++){
		if(v[i].first.first==v[0].first.first and v[i].first.second==v[0].first.second and v[i].second.first==v[0].second.first){
			cnt++; na.push_back(v[i].second.second);
			if(v[i].second.second==id)flag=true;
		}
	}
	if(cnt<=n and flag)return 1;
	if(cnt>=n and !flag)return 0;
	if(cnt==1 and n==2 and !flag){
		na.pop_back();
		for(int i=1;i<m;i++){
			if(v[i].first.first==v[1].first.first and v[i].first.second==v[1].first.second and v[i].second.first==v[1].second.first){
				na.push_back(v[i].second.second);
				if(v[i].second.second==id)flag=true;
			}
		}
		if(!flag)return 0;
		else if(na.size()==1)return 1;
		else return check(1,na,id);
	}
	else{
		if(m==(int)na.size()){
			p*=(double)n;
			return (int)na.size();
		}
		else return check(n,na,id);
	}
}

void solve(){
	string T; cin >> T;
	int id=-1;
	for(int i=0,j=0;i<4&&j<T.size();){
		if(T[j]=='_')j++;
		else if(T[j]=='*')id=i,j++;
		else i++,j+=3;
	}
	vector<string> s(4);
	for(int i=0;i<4;i++) cin >> s[i];
	vector<int> r;
	for(int i=0;i<4;i++){
		for(int j=i+1;j<4;j++){
			if(s[i][j*5+6]=='_'){
				r.push_back(i);r.push_back(j);
			}
			else{
				int s1=s[i][j*5+6]-'0', s2=s[i][j*5+8]-'0';
				M[i][j]=s1, M[j][i]=s2;
			}
		}
	}
	double res=0.0;
	for(int i=0;i<=8;i++){
		for(int j=0;j<=8;j++){
			for(int k=0;k<=8;k++){
				for(int l=0;l<=8;l++){
					p=score[i]*score[j]*score[k]*score[l];
					M[r[0]][r[1]]=i,M[r[1]][r[0]]=j,M[r[2]][r[3]]=k,M[r[3]][r[2]]=l;
					vector<int> a={0,1,2,3};
					int t=check(2,a,id);
					if(t){
						p/=(double)t;
						res+=p;
					}
				}
			}
		}
	}
	printf("%.7f\n",res);
}

int main(){
	// 前計算
	const int base=1*2*3*4*5*6*7*8;
	for(int i=0;i<=8;i++){
		double res=base;
		for(int j=1;j<=i;j++){
			res/=(double)j;
			res*=(0.25);
		}
		for(int j=1;j<=8-i;j++){
			res/=(double)j;
			res*=(0.75);
		}
		score[i]=res;
	}
	int q; cin >> q;
	while(q--)solve();
}
accuracy: 1
time_ms: 250
memory_kb: 3136
final_rank: 2

code_snippet: #include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>


using namespace std;
typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using Real = double;
vector<int> hidx = { 1,1,1,2,2,3 };
vector<int> widx = { 11,16,21,16,21,21 };
vector<Real> p(10);
int tidx[50][50];

struct Data {
	int score, diff, goal, id;
	Data() = default;
	Data(int score, int diff, int goal, int id) :score(score), diff(diff), goal(goal), id(id) {}
	bool operator<(const Data& d)const {
		return make_tuple(score, diff, goal) < make_tuple(d.score, d.diff, d.goal);
	}
	bool operator>(const Data& d)const {
		return make_tuple(score, diff, goal) > make_tuple(d.score, d.diff, d.goal);
	}
	bool operator==(const Data& d)const {
		return make_tuple(score, diff, goal) == make_tuple(d.score, d.diff, d.goal);
	}
};

void calc_data(Data& a, Data& b, int af, int bf) {
	a.diff += af - bf;
	b.diff += bf - af;
	a.goal += af;
	b.goal += bf;
	if (af == bf) {
		a.score += 1;
		b.score += 1;
	}
	else if (af > bf) {
		a.score += 3;
	}
	else {
		b.score += 3;
	}
}
Real calc(array<string, 5>& vs, int my) {
	vector<bool> bad(4); int cnt = 0;
	bool tw = false;
	{
		vector<Data> d(4); for (int i = 0; i < 4; i++) d[i].id = i;
		for (int i = 0; i < 6; i++) {
			int f = vs[hidx[i]][widx[i]] - '0';
			int s = vs[hidx[i]][widx[i] + 2] - '0';
			int fi = tidx[hidx[i]][widx[i]];
			int si = tidx[hidx[i]][widx[i] + 2];
			calc_data(d[fi], d[si], f, s);
		}
		sort(d.rbegin(), d.rend());

		if (d[0] == d.back()) return 0.5;
		else if (d[0] == d[2]) {
			if (d[3].id == my) return 0.0;
			cnt = 2;
			bad[d[3].id] = true;
		}
		else if (d[0] == d[1]) {
			if (d[0].id == my or d[1].id == my) return 1.0;
			else return 0.0;
		}
		else {
			if (d[0].id == my) return 1.0;
			if (d[1] > d[2]) {
				if (d[1].id == my) return 1.0;
				else return 0.0;
			}
			else {
				if (d[2] > d[3]) {
					if (d[3].id == my) return 0.0;
					cnt = 1;
					bad[d[0].id] = true;
					bad[d[3].id] = true;
					tw = true;
				}
				else {
					cnt = 1;
					bad[d[0].id] = true;
				}
			}
		}
	}
	if (!tw) {
		vector<Data> d(4);
		for (int i = 0; i < 4; i++) {
			d[i].id = i;
			if (bad[i]) d[i].score -= 100000;
		}
		for (int i = 0; i < 6; i++) {
			int f = vs[hidx[i]][widx[i]] - '0';
			int s = vs[hidx[i]][widx[i] + 2] - '0';
			int fi = tidx[hidx[i]][widx[i]];
			int si = tidx[hidx[i]][widx[i] + 2];
			if (bad[fi] or bad[si]) continue;
			calc_data(d[fi], d[si], f, s);
		}
		sort(d.rbegin(), d.rend());
		d.pop_back();
		if (d[0] == d.back()) return (Real)cnt / 3.0;
		else if (d[0] == d[1]) {
			if (d[2].id == my) return 0.0;
			else if (cnt == 2) return 1.0;
			else {
				tw = true;
				bad[d[2].id] = true;
			}
		}
		else {
			if (d[0].id == my) return 1.0;
			else if (cnt == 1) return 0.0;
			else {
				if (d[1] > d[2]) {
					if (d[1].id == my) return 1.0;
					else return 0.0;
				}
				else {
					cnt = 1;
					tw = true;
					bad[d[0].id] = true;
				}
			}
		}
	}
	vector<Data> d(4);
	for (int i = 0; i < 4; i++) {
		d[i].id = i;
		if (bad[i]) d[i].score -= 100000;
	}
	for (int i = 0; i < 6; i++) {
		int f = vs[hidx[i]][widx[i]] - '0';
		int s = vs[hidx[i]][widx[i] + 2] - '0';
		int fi = tidx[hidx[i]][widx[i]];
		int si = tidx[hidx[i]][widx[i] + 2];
		if (bad[fi] or bad[si]) continue;
		calc_data(d[fi], d[si], f, s);
	}
	sort(d.rbegin(), d.rend());
	d.pop_back(); d.pop_back();
	if (d[0] == d[1]) return 1.0 / 2.0;
	else if (d[0].id == my) return 1.0;
	else return 0.0;
}


void solve() {
	array<string, 5> vs; for (int i = 0; i < 5; i++) cin >> vs[i];
	vector<pair<int, int>> r;
	int my = -1;
	for (int i = 1; i < 5; i++) if (vs[i][0] == '*') my = i - 1;
	Real res = 0.0;
	for (int i = 0; i < 6; i++) {
		if (vs[hidx[i]][widx[i]] == '_') {
			r.emplace_back(hidx[i], widx[i]);
			r.emplace_back(hidx[i], widx[i] + 2);
		}
	}
	int t[4];
	for (t[0] = 0; t[0] < 9; t[0]++)for (t[1] = 0; t[1] < 9; t[1]++)for (t[2] = 0; t[2] < 9; t[2]++) for (t[3] = 0; t[3] < 9; t[3]++) {
		for (int i = 0; i < 4; i++) vs[r[i].first][r[i].second] = (char)('0' + t[i]);
		res += calc(vs, my) * p[t[0]] * p[t[1]] * p[t[2]] * p[t[3]];
		for (int i = 0; i < 4; i++) vs[r[i].first][r[i].second] = '_';
	}
	cout << fixed << setprecision(12) << res << "\n";
}
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	vector<Real> f(10); f[0] = f[1] = 1; for (int i = 2; i < 10; i++) f[i] = f[i - 1] * i;
	for (int i = 0; i < 9; i++) {
		p[i] = f[8] / (f[i] * f[8 - i]) * pow(0.25, i) * pow(0.75, 8 - i);
	}
	for (int i = 0; i < 6; i++) {
		tidx[hidx[i]][widx[i]] = hidx[i] - 1;
		tidx[hidx[i]][widx[i] + 2] = (widx[i] - 6) / 5;
	}
	int kkt; cin >> kkt; while (kkt--) solve();
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 3416
final_rank: 14

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

vector<long double>p(9);

struct Team {
	vector<int>a, b, c;
	int index;
	Team() {
		a.resize(4);
		b.resize(4);
		c.resize(4);
	}
};

int Check_min(Team s, Team t, int bit = 15) {
	int sa, sb, sc, ta, tb, tc;
	sa = sb = sc = ta = tb = tc = 0;
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if ((bit >> j) & 1) {
				sa += s.a[j];
				sb += s.b[j];
				sc += s.c[j];
				ta += t.a[j];
				tb += t.b[j];
				tc += t.c[j];
			}
		}
	}
	auto sbox = make_pair(sa, make_pair(sb, sc));
	auto tbox = make_pair(ta, make_pair(tb, tc));
	if (sbox < tbox)return 0;
	if (sbox == tbox)return 1;
	return 2;
}

long double Check(vector<vector<int>>plus, vector<pair<int, int>>vp, vector<int>num, int cnt) {
	for (int i = 0; i < 4; i++) {
		plus[vp[i].first][vp[i].second] = num[i];
	}
	vector<Team>team(4);
	for (int i = 0; i < 4; i++) {
		team[i].index = i;
		for (int j = i + 1; j < 4; j++) {
			team[i].b[j] += plus[i][j];
			team[i].b[j] -= plus[j][i];
			team[j].b[i] += plus[j][i];
			team[j].b[i] -= plus[i][j];
			team[i].c[j] += plus[i][j];
			team[j].c[i] += plus[j][i];
			if (plus[i][j] > plus[j][i]) {
				team[i].a[j] = 3;
			}
			else if (plus[i][j] == plus[j][i]) {
				team[i].a[j] = team[j].a[i] = 1;
			}
			else {
				team[j].a[i] = 3;
			}
		}
	}
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 3; j++) {
			auto c = Check_min(team[j], team[j + 1]);
			if (c == 0)swap(team[j], team[j + 1]);
		}
	}
	if (Check_min(team[0], team[3]) == 1)return 0.5;
	if (Check_min(team[0], team[2]) == 1) {
		if (team[3].index == cnt)return 0;
		int bit = 0;
		for (int i = 0; i < 3; i++) {
			bit |= 1 << team[i].index;
		}
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 2; j++) {
				if (Check_min(team[j], team[j + 1], bit) == 0)swap(team[j], team[j + 1]);
			}
		}
		if (Check_min(team[0], team[2], bit) == 1)return (long double)2 / 3;
		if (Check_min(team[0], team[1], bit) == 1) {
			if (team[2].index == cnt)return 0;
			return 1;
		}
		if (Check_min(team[1], team[2]) == 1) {
			if (team[0].index == cnt)return 1;
			bit = 1 << team[1].index;
			bit |= 1 << team[2].index;
			if (Check_min(team[1], team[2], bit) == 0)swap(team[1], team[2]);
			if (Check_min(team[1], team[2], bit) == 1)return 0.5;
			if (team[1].index == cnt)return 1;
			return 0;
		}
		if (team[2].index == cnt)return 0;
		return 1;
	}
	if (Check_min(team[1], team[3]) == 1) {
		if (team[0].index == cnt)return 1;
		int bit = 0;
		for (int i = 1; i < 4; i++) {
			bit |= 1 << team[i].index;
		}
		for (int i = 0; i < 3; i++) {
			for (int j = 1; j < 3; j++) {
				if (Check_min(team[j], team[j + 1], bit) == 0)swap(team[j], team[j + 1]);
			}
		}
		if (Check_min(team[1], team[3], bit) == 1)return (long double)1 / 3;
		if (Check_min(team[2], team[3], bit) == 1) {
			if (team[1].index == cnt)return 1;
			return 0;
		}
		if (Check_min(team[1], team[2]) == 1) {
			if (team[3].index == cnt)return 0;
			bit = 1 << team[1].index;
			bit |= 1 << team[2].index;
			if (Check_min(team[1], team[2], bit) == 0)swap(team[1], team[2]);
			if (Check_min(team[1], team[2], bit) == 1)return 0.5;
			if (team[1].index == cnt)return 1;
			return 0;
		}
		if (team[1].index == cnt)return 1;
		return 0;
	}
	if (Check_min(team[1], team[2]) == 1) {
		if (team[0].index == cnt)return 1;
		if (team[3].index == cnt)return 0;
		int bit = 1 << team[1].index;
		bit |= 1 << team[2].index;
		if (Check_min(team[1], team[2], bit) == 1)return 0.5;
		if (Check_min(team[1], team[2], bit) == 0)swap(team[1], team[2]);
		if (team[1].index == cnt)return 1;
		return 0;
	}
	if (team[0].index == cnt || team[1].index == cnt)return 1;
	return 0;
}

long double Solve() {
	vector<string>s(5);
	for (auto &i : s)cin >> i;
	int cnt = -1;
	for (int i = 1; i < s.size(); i++) {
		if (s[i][0] == '*')cnt = i - 1;
	}
	vector<vector<int>>plus(4, vector<int>(4));
	vector<pair<int, int>>vp;
	for (int i = 0; i < 4; i++) {
		for (int j = i + 1; j < 4; j++) {
			if (s[i + 1][j * 5 + 6] == '_') {
				vp.push_back({ i,j });
				vp.push_back({ j,i });
		//		cout << i << " " << j << endl;
			}
			else {
				plus[i][j] = s[i + 1][j * 5 + 6] - '0';
				plus[j][i] = s[i + 1][j * 5 + 8] - '0';
			}
		}
	}
	vector<int>num(4);
	long double ret = 0;
	while (num[0] < 9) {
		auto by = Check(plus, vp, num, cnt);
		ret += p[num[0]] * p[num[1]] * p[num[2]] * p[num[3]] * by;
		num.back()++;
		int c = 3;
		while (c&&num[c] > 8) {
			num[c--] = 0;
			num[c]++;
		}
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	for (int i = 0; i <= 8; i++) {
		p[i] = 1;
		for (int j = 2; j <= 8; j++)p[i] *= j;
		for (int j = 1; j <= i; j++) {
			p[i] /= j;
		}
		for (int j = 1; j <= 8 - i; j++) {
			p[i] /= j;
			p[i] *= 3;
		}
		for (int j = 1; j <= 8; j++)p[i] /= 4;
	}
	cin >> T;
	while (T--) {
		auto box = Solve();
		cout << setprecision(20) << box << endl;
	}
}
accuracy: 1
time_ms: 2120
memory_kb: 3308
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  cout << fixed << setprecision(7);
  vector<int> fact(9);
  fact[0] = 1;
  for (int i = 1; i < 9; i++){
    fact[i]= fact[i - 1] * i;
  }
  vector<double> p(9);
  for (int i = 0; i < 9; i++){
    p[i] = (double) fact[8] / fact[i] / fact[8 - i] * pow(0.25, i) * pow(0.75, 8 - i);
  }
  int t;
  cin >> t;
  for (int i = 0; i < t; i++){
    vector<string> S(5);
    for (int j = 0; j < 5; j++){
      cin >> S[j];
    }
    vector<vector<int>> score(4, vector<int>(4, -1));
    for (int j = 0; j < 4; j++){
      for (int k = j + 1; k < 4; k++){
        char c1 = S[j + 1][k * 5 + 6];
        if (c1 != '_'){
          score[j][k] = c1 - '0';
        }
        char c2 = S[j + 1][k * 5 + 8];
        if (c2 != '_'){
          score[k][j] = c2 - '0';
        }
      }
    }
    vector<pair<int, int>> s;
    for (int j = 0; j < 4; j++){
      for (int k = 0; k < 4; k++){
        if (j != k){
          if (score[j][k] == -1){
            s.push_back(make_pair(j, k));
          }
        }
      }
    }
    int team;
    for (int j = 0; j < 4; j++){
      if (S[j + 1][0] == '*'){
        team = j;
      }
    }
    double ans = 0;
    vector<int> r(4);
    for (r[0] = 0; r[0] <= 8; r[0]++){
      for (r[1] = 0; r[1] <= 8; r[1]++){
        for (r[2] = 0; r[2] <= 8; r[2]++){
          for (r[3] = 0; r[3] <= 8; r[3]++){
            double tmp = 1;
            for (int j = 0; j < 4; j++){
              tmp *= p[r[j]];
            }
            vector<vector<int>> result = score;
            for (int j = 0; j < 4; j++){
              result[s[j].first][s[j].second] = r[j];
            }
            vector<set<int>> rank(1);
            for (int j = 0; j < 4; j++){
              rank[0].insert(j);
            }
            while (1){
              bool update = false;
              vector<set<int>> rank2;
              vector<int> points(4, 0);
              vector<int> gdiff(4, 0);
              vector<int> goals(4, 0);
              int cnt = rank.size();
              for (int j = 0; j < cnt; j++){
                int sz = rank[j].size();
                for (int k = 0; k < 4; k++){
                  for (int l = k + 1; l < 4; l++){
                    if (rank[j].count(k) && rank[j].count(l)){
                      if (result[k][l] > result[l][k]){
                        points[k] += 3;
                      } else if (result[k][l] < result[l][k]){
                        points[l] += 3;
                      } else {
                        points[k]++;
                        points[l]++;
                      }
                      gdiff[k] += result[k][l] - result[l][k];
                      gdiff[l] += result[l][k] - result[k][l];
                      goals[k] += result[k][l];
                      goals[l] += result[l][k];
                    }
                  }
                }
                map<tuple<int, int, int>, set<int>> mp;
                for (int k : rank[j]){
                  mp[make_tuple(points[k], gdiff[k], goals[k])].insert(k);
                }
                if (mp.size() > 1){
                  update = true;
                }
                for (auto itr = mp.rbegin(); itr != mp.rend(); itr++){
                  rank2.push_back((*itr).second);
                }
              }
              if (!update){
                break;
              } else {
                swap(rank, rank2);
              }
            }
            if (rank[0].count(team)){
              if (rank[0].size() <= 2){
                ans += tmp;
              } else {
                ans += tmp * 2 / rank[0].size();
              }
            } else if (rank[0].size() == 1 && rank[1].count(team)){
              ans += tmp / rank[1].size();
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 1230
memory_kb: 3412
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 100010;

int myteam;
pii board[4][4];
double fact[10], prob[10];
double ans;

vector<int> order(int S){
    vector<int> point(4, 0), diff(4, 0), score(4, 0);
    vector<int> id;
    rep(i, 4)if(S>>i & 1) id.push_back(i);
    rep(x, 4)if(S>>x & 1){
        For(y, x+1, 4)if(S>>y & 1){
            int s, t;
            tie(s, t) = board[x][y];
            score[x] += s; score[y] += t;
            diff[x] += s - t; diff[y] += t - s;

            if(s > t) point[x] += 3;
            else if(s < t) point[y] += 3;
            else point[x] += 1, point[y] += 1;
        }
    }
    auto cmp = [&](const int &i, const int &j){
        if(point[i] != point[j]) return point[i] > point[j];
        if(diff[i] != diff[j]) return diff[i] > diff[j];
        return score[i] > score[j];
    };
    sort(id.begin(), id.end(), cmp);

    vector<int> pri(4, -1);
    int cur = 0;
    for(int l=0; l<(int)id.size();){
        int r = l;
        int T = 0;
        while(r < (int)id.size() && !cmp(id[l], id[r])){
            T |= (1 << id[r]);
            ++r;
        }
        if(r - l == 1 || S == T){
            For(i, l, r) pri[id[i]] = cur;
            ++cur;
        }
        else{
            auto p = order(T);
            int ncur = cur + 1;
            For(i, l, r) pri[id[i]] = cur + p[id[i]], chmax(ncur, pri[id[i]] + 1);
            cur = ncur;
        }
        l = r;
    }

    return pri;
}

void dfs(int cur, double p){
    if(cur == 2){
        auto pri = order((1<<4)-1);
        int mypos, cnt[4];
        rep(i, 4) cnt[i] = 0;
        rep(i, 4){
            if(i == myteam) mypos = pri[i];
            ++cnt[pri[i]];
        }

        if(mypos == 0) p *= min(1.0, 2.0 / (double)cnt[0]);
        else if(mypos == 1 && (int)cnt[0] < 2){
            p *= 1.0 / (double)cnt[1];
        }
        else p = 0;

        ans += p;
        return;
    }

    rep(x, 4)For(y, x+1, 4)if(board[x][y].fi < 0){
        rep(s, 9)rep(t, 9){
            board[x][y] = {s, t};
            dfs(cur + 1, p * prob[s] * prob[t]);
        }
        board[x][y] = {-1, -1};
        return;
    }
}

void solve(){
    char gomi, name[5];
    rep(i, 4) scanf(" %c", &gomi);
    rep(i, 4){
        rep(j, 5) scanf(" %c", &name[j]);
        if(name[1] == '*') myteam = i;
    }

    rep(i, 4)rep(j, 4) board[i][j] = {-1, -1};
    rep(i, 4){
        char gomi, result[5];
        rep(k, 4) scanf(" %c", &gomi);
        rep(j, 4){
            rep(k, 5) scanf(" %c", &result[k]);
            if(isdigit(result[2])){
                board[i][j] = {result[2] - '0', result[4] - '0'};
            }
        }
    }

    ans = 0;
    dfs(0, 1.0);
    printf("%.10lf\n", ans);
}

int main(){
    fact[0] = 1;
    For(i, 1, 10) fact[i] = fact[i-1] * i;
    rep(p, 9){
        prob[p] = 40320 / fact[p] / fact[8-p] * pow(0.25, p) * pow(0.75, 8-p);
    }
    int t;
    scanf("%d", &t);
    rep(tt, t){
        solve();
    }
}
accuracy: 1
time_ms: 150
memory_kb: 3412
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
 
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;

bool print_space_enable = false;
void print() { 
    cout << endl; 
    print_space_enable = false;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    if (print_space_enable) cout << " ";
    cout << fixed << setprecision(7) << head;
    print_space_enable = true;
    print(std::forward<Tail>(tail)...);
}

template<typename T>
void print(vector<T> v) {
    for (size_t i = 0; i < v.size(); i++) {
        if (i > 0) std::cout << " ";
        std::cout << v[i];
    }
    std::cout << std::endl;
}

double pro[10];
pair<Int, Int> score[5][5];

void calc_pro() {
    double x[10];
    x[0] = x[1] = 1.0;
    for (int i = 1; i <= 8; i++) {
        x[i + 1] = x[i] * (double)(i + 1);
    }
    for (int i = 0; i <= 8; i++) {
        pro[i] = x[8] / (x[i] * x[8 - i]);
        for (int j = 0; j < i; j++) {
            pro[i] *= 1.0 / 4.0;
        }
        for (int j = 0; j < 8 - i; j++) {
            pro[i] *= 3.0 / 4.0;
        }
    }
}

void s(vector<vector<Int>> &v, Int x) {
    for (Int i = 0; i < (Int)v.size(); i++) {
        for (Int j = 0; j < (Int)v.size() - 1; j++) {
            if (v[j][x] < v[j + 1][x]) {
                swap(v[j], v[j + 1]);
            }
        }
    }
}

double f(vector<vector<Int>> v, Int check_team) {
    s(v, 2);
    s(v, 1);
    s(v, 0);
    vector<Int> rank[4];
    rank[0].push_back(v[0][3]);
    Int pre = 0;
    for (Int i = 1; i < 4; i++) {
        if (v[i - 1][0] == v[i][0] and v[i - 1][1] == v[i][1] and v[i - 1][2] == v[i][2]) {
            rank[pre].push_back(v[i][3]);
        } else {
            pre++;
            rank[pre].push_back(v[i][3]);
        }
    }
    if (rank[0].size() == 4) {
        return 1.0 / 2.0;
    }
    if (rank[0].size() == 3) {
        if (rank[1][0] == check_team) {
            return 0.0;
        } else {
            Int ex = rank[1][0];
            Int mp[4];
            for (Int i = 0; i < 4; i++) {
                mp[v[i][3]] = i;
            }
            for (Int i = 0; i < 4; i++) {
                if (i == ex) continue;
                Int a = score[ex][i].first;
                Int b = score[ex][i].second;
                if (a < b) {
                    v[mp[i]][0] -= 3;
                }
                if (a == b) {
                    v[mp[i]][0] -= 1;
                }
                v[mp[i]][1] -= b - a;
                v[mp[i]][2] -= b;
            }
            vector<vector<Int>> v2;
            for (Int i = 0; i < 4; i++) {
                if (v[i][3] == ex) continue;
                v2.push_back(v[i]);
            }
            s(v2, 2);
            s(v2, 1);
            s(v2, 0);
            vector<Int> rank2[3];
            rank2[0].push_back(v2[0][3]);
            Int pre = 0;
            for (Int i = 1; i < 3; i++) {
                if (v2[i - 1][0] == v2[i][0] and v2[i - 1][1] == v2[i][1] and v2[i - 1][2] == v2[i][2]) {
                    rank2[pre].push_back(v2[i][3]);
                } else {
                    pre++;
                    rank2[pre].push_back(v2[i][3]);
                }
            }
            if (rank2[0].size() == 3) {
                return 2.0 / 3.0;
            }
            if (rank2[0].size() == 2) {
                if (rank2[1][0] == check_team) {
                    return 0.0;
                } else {
                    return 1.0;
                }
            }
            if (rank2[0].size() == 1) {
                if (rank2[0][0] == check_team) {
                    return 1.0;
                } else {
                    if (rank2[1].size() == 1) {
                        if (rank2[1][0] == check_team) {
                            return 1.0;
                        } else {
                            return 0.0;
                        }
                    } else {
                        assert(rank2[1].size() == 2);
                        Int a = score[rank2[1][0]][rank2[1][1]].first;
                        Int b = score[rank2[1][0]][rank2[1][1]].second;
                        if (a == b) {
                            return 1.0 / 2.0;
                        } else {
                            if ((a > b and rank2[1][0] == check_team) or (a < b and rank2[1][1] == check_team)) {
                                return 1.0;
                            } else {
                                return 0.0;
                            }
                        }
                    }
                }
            }
        }
    }
    if (rank[0].size() == 2) {
        if (rank[0][0] == check_team or rank[0][1] == check_team) {
            return 1.0;
        } else {
            return 0.0;
        }
    }
    if (rank[0].size() == 1) {
        if (rank[0][0] == check_team) {
            return 1.0;
        } else {
            if (rank[1].size() == 3) {
                Int ex = rank[0][0];
                Int mp[4];
                for (Int i = 0; i < 4; i++) {
                    mp[v[i][3]] = i;
                }
                for (Int i = 0; i < 4; i++) {
                    if (i == ex) continue;
                    Int a = score[ex][i].first;
                    Int b = score[ex][i].second;
                    if (a < b) {
                        v[mp[i]][0] -= 3;
                    }
                    if (a == b) {
                        v[mp[i]][0] -= 1;
                    }
                    v[mp[i]][1] -= b - a;
                    v[mp[i]][2] -= b;
                }
                vector<vector<Int>> v2;
                for (Int i = 0; i < 4; i++) {
                    if (v[i][3] == ex) continue;
                    v2.push_back(v[i]);
                }
                s(v2, 2);
                s(v2, 1);
                s(v2, 0);
                vector<Int> rank2[3];
                rank2[0].push_back(v2[0][3]);
                Int pre = 0;
                for (Int i = 1; i < 3; i++) {
                    if (v2[i - 1][0] == v2[i][0] and v2[i - 1][1] == v2[i][1] and v2[i - 1][2] == v2[i][2]) {
                        rank2[pre].push_back(v2[i][3]);
                    } else {
                        pre++;
                        rank2[pre].push_back(v2[i][3]);
                    }
                }
                if (rank2[0].size() == 3) {
                    return 1.0 / 3.0;
                }
                if (rank2[0].size() == 2) {
                    if (rank2[1][0] == check_team) {
                        return 0.0;
                    } else {
                        Int a = score[rank2[0][0]][rank2[0][1]].first;
                        Int b = score[rank2[0][0]][rank2[0][1]].second;
                        if (a == b) {
                            return 1.0 / 2.0;
                        } else {
                            if ((a > b and rank2[0][0] == check_team) or (a < b and rank2[0][1] == check_team)) { 
                                return 1.0;
                            } else {
                                return 0.0;
                            }
                        }
                    }
                }
                if (rank2[0].size() == 1) {
                    if (rank2[0][0] == check_team) {
                        return 1.0;
                    } else {
                        return 0.0;
                    }
                }
            }
            if (rank[1].size() == 2) {
                if (rank[2][0] == check_team) {
                    return 0.0;
                } else {
                    Int a = score[rank[1][0]][rank[1][1]].first;
                    Int b = score[rank[1][0]][rank[1][1]].second;
                    if (a == b) {
                        return 1.0 / 2.0;
                    } else {
                        if ((a > b and rank[1][0] == check_team) or (a < b and rank[1][1] == check_team)) { 
                            return 1.0;
                        } else {
                            return 0.0;
                        }
                    }
                }
            }
            if (rank[1].size() == 1) {
                if (rank[1][0] == check_team) {
                    return 1.0;
                } else {
                    return 0.0;
                }
            }
        }
    }
}

int main() {
    Int t;
    cin >> t;
    calc_pro();
    vector<double> r;
    while (t--) {
        vector<vector<Int>> v(4, vector<Int>(4, 0));
        for (Int i = 0; i < 4; i++) v[i][3] = i;
        Int check_team = -1;
        pair<Int, Int> b1 = {-1, -1}, b2 = {-1, -1};
        for (Int i = 0; i <= 4; i++) {
            string s;
            cin >> s;
            if (i == 0) continue;
            if (s[0] == '*') {
                check_team = i - 1;
            }
            for (Int j = (i + 1) * 5; j < (Int)s.size(); j+=5) {
                Int team_A = i - 1;
                Int team_B = j / 5 - 1;
                Int point_A = -1, point_B = -1;
                for (Int k = 0; k < 4; k++) {
                    if (k == 1 and s[j + k] != '_') {
                        point_A = s[j + k] - '0';
                    }
                    if (k == 3 and s[j + k] != '_') {
                        point_B = s[j + k] - '0';
                    }
                }
                score[team_A][team_B] = {point_A, point_B};
                score[team_B][team_A] = {point_B, point_A};
                if (point_A < 0) {
                    if (b1.first < 0) {
                        b1 = {team_A, team_B};
                    } else {
                        b2 = {team_A, team_B};
                    }
                } else {
                    if (point_A > point_B) {
                        v[team_A][0] += 3;
                    } else if (point_A < point_B) {
                        v[team_B][0] += 3;
                    } else {
                        v[team_A][0] += 1;
                        v[team_B][0] += 1;
                    }
                    v[team_A][1] += point_A - point_B;
                    v[team_B][1] += point_B - point_A;
                    v[team_A][2] += point_A;
                    v[team_B][2] += point_B;
                }
            }
        }
        double res = 0;
        for (Int i = 0; i <= 8; i++) {
            for (Int j = 0; j <= 8; j++) {
                if (i > j) {
                    v[b1.first][0] += 3;
                } else if (i < j) {
                    v[b1.second][0] += 3;
                } else {
                    v[b1.first][0] += 1;
                    v[b1.second][0] += 1;
                }
                v[b1.first][1] += i - j;
                v[b1.second][1] += j - i;
                v[b1.first][2] += i;
                v[b1.second][2] += j;
                score[b1.first][b1.second] = {i, j};
                score[b1.second][b1.first] = {j, i};
                for (Int k = 0; k <= 8; k++) {
                    for (Int l = 0; l <= 8; l++) {
                        if (k > l) {
                            v[b2.first][0] += 3;
                        } else if (k < l) {
                            v[b2.second][0] += 3;
                        } else {
                            v[b2.first][0] += 1;
                            v[b2.second][0] += 1;
                        }
                        v[b2.first][1] += k - l;
                        v[b2.second][1] += l - k;
                        v[b2.first][2] += k;
                        v[b2.second][2] += l;
                        score[b2.first][b2.second] = {k, l};
                        score[b2.second][b2.first] = {l, k};
                        
                        // double p = f(v, check_team);

                        // if (i > j and k < l and p != 0 and p != 1) {
                        //     print("-------");
                        //     for (Int i = 0; i < (Int)v.size(); i++) {
                        //         print(v[i]);
                        //     }
                        //     print(f(v, check_team));
                        // }

                        res += pro[i] * pro[j] * pro[k] * pro[l] * f(v, check_team);

                        if (k > l) {
                            v[b2.first][0] -= 3;
                        } else if (k < l) {
                            v[b2.second][0] -= 3;
                        } else {
                            v[b2.first][0] -= 1;
                            v[b2.second][0] -= 1;
                        }
                        v[b2.first][1] -= k - l;
                        v[b2.second][1] -= l - k;
                        v[b2.first][2] -= k;
                        v[b2.second][2] -= l;
                    }
                }
                if (i > j) {
                    v[b1.first][0] -= 3;
                } else if (i < j) {
                    v[b1.second][0] -= 3;
                } else {
                    v[b1.first][0] -= 1;
                    v[b1.second][0] -= 1;
                }
                v[b1.first][1] -= i - j;
                v[b1.second][1] -= j - i;
                v[b1.first][2] -= i;
                v[b1.second][2] -= j;
            }
        }
        r.push_back(res);
        //print(ma);
    }
    for (Int i = 0; i < (Int)r.size(); i++) {
        print(r[i]);
    }
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 3176
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
#define double long double
const int mod=1000000007,MAX=203,INF=1<<20;

struct data{
    int point;
    int diff;
    int goal;
    int id;
    
    bool operator<(const data &another) const
    {
        if(point==another.point){
            if(diff==another.diff){
                if(goal==another.goal){
                    return id<another.id;
                }
                return goal<another.goal;
            }
            return diff<another.diff;
        }
        return point<another.point;
    };
    
    bool operator == (const data &another) const
    {
        return point==another.point&&diff==another.diff&&goal==another.goal;
    };
};

vector<int> dh={1,1,1,2,2,3},dw={11,16,21,16,21,21};

void f(data &a,data &b,int scorea,int scoreb){
    if(scorea==scoreb){
        a.point++;
        b.point++;
        a.goal+=scorea;
        b.goal+=scoreb;
    }else if(scorea>scoreb){
        a.point+=3;
        a.diff+=scorea-scoreb;
        b.diff+=scoreb-scorea;
        a.goal+=scorea;
        b.goal+=scoreb;
    }else{
        b.point+=3;
        a.diff+=scorea-scoreb;
        b.diff+=scoreb-scorea;
        a.goal+=scorea;
        b.goal+=scoreb;
    }
    
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    vector<double> p(9);
    for(double i=0.0;i<=8.0;i++){
        p[i]=1.0;
        for(double j=1.0;j<=8.0;j++) p[i]*=j;
        for(double j=1.0;j<=i;j++) p[i]/=j;
        for(double j=1.0;j<=8.0-i;j++) p[i]/=j;
        
        for(double j=1.0;j<=8.0-i;j++) p[i]*=3.0;
        for(double j=1.0;j<=8.0;j++) p[i]/=4.0;
    }
    
    while(Q--){
        vector<string> S(5);
        for(int i=0;i<5;i++) cin>>S[i];
        double ans=0.0;
        int my;
        vector<data> T(4);
        
        for(int i=1;i<5;i++) if(S[i][0]=='*') my=i-1;
        
        vector<pair<int,int>> ch;
        
        for(int k=0;k<6;k++){
            if(S[dh[k]][dw[k]]=='_'){
                ch.push_back({dh[k],dw[k]});
                ch.push_back({dh[k],dw[k]+2});
            }
        }
        
        for(int q1=0;q1<9;q1++){
            for(int q2=0;q2<9;q2++){
                for(int q3=0;q3<9;q3++){
                    for(int q4=0;q4<9;q4++){
                        double pp=p[q1]*p[q2]*p[q3]*p[q4];
                        vector<int> sco(4);
                        sco[0]=q1;
                        sco[1]=q2;
                        sco[2]=q3;
                        sco[3]=q4;
                        
                        for(int k=0;k<4;k++) S[ch[k].first][ch[k].second]=char('0'+sco[k]);
                        
                        vector<int> use(4,0);
                        
                        while(1){
                            vector<data> T(4);
                            int fi=0;
                            for(int i=0;i<4;i++){
                                T[i].id=i;
                                if(use[i]==1) T[i].point+=1000;
                                if(use[i]==2) T[i].point-=1000;
                                if(use[i]==0) fi++;
                            }
                            
                            for(int k=0;k<6;k++){
                                int a=S[dh[k]][dw[k]]-'0',b=S[dh[k]][dw[k]+2]-'0';
                                
                                if(use[dh[k]-1]||use[(dw[k]-6)/5]) continue;
                                
                                f(T[dh[k]-1],T[(dw[k]-6)/5],a,b);
                                
                                //cout<<a<<" "<<b<<endl;
                            }
                            
                            int cnta=0,cntb=0;
                            for(int i=0;i<4;i++){
                                //if(i==my) continue;
                                if(!(T[i]==T[my])){
                                    if(T[i]<T[my]) cntb++;
                                    if(T[my]<T[i]) cnta++;
                                }
                            }
                            
                            int posa,posb;
                            posa=cnta;
                            posb=3-cntb;
                            
                            sort(all(T));
                            reverse(all(T));
                            
                            if(posb<=1){
                                ans+=pp;
                                break;
                            }else if(posa>=2){
                                break;
                            }else{
                                if(fi==posb-posa+1){
                                    double cnt=0;
                                    for(int i=posa;i<=posb;i++) if(i<=1) cnt++;
                                    ans+=pp*cnt/(double)(fi);
                                    break;
                                }else{
                                    for(int i=0;i<posa;i++) use[T[i].id]=1;
                                    for(int i=posb+1;i<4;i++) use[T[i].id]=2;
                                    for(int i=posa;i<=posb;i++){
                                        //if(use[T[i].id]) cout<<"!"<<endl;
                                        use[T[i].id]=0;
                                    }
                                }
                            }
                        }
                        
                        for(int k=0;k<4;k++) S[ch[k].first][ch[k].second]='_';
                    }
                }
            }
        }
        cout<<fixed<<setprecision(7)<<ans<<endl;
    }
}
accuracy: 1
time_ms: 90
memory_kb: 3304
final_rank: 9

code_snippet: #define  _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144 * 4LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

void init() {
    iostream::sync_with_stdio(false);
    cout << fixed << setprecision(20);
}


//#define int ll

unsigned long xor128() {
    static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned long t = (x ^ (x << 11));
    x = y; y = z; z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
string names[4];
int target = 0;
vector<pair<pair<int, int>, pair<int, int>>> results;
long double calc() {
    vector<int> going{ 0,1,2,3 };
    vector<int> winner;
    REP(t, 3) {
        map<tuple<int, int, int>, vector<int>> next;
        tuple<int, int, int> cnter[4] = {};
        REP(i, going.size()) {
            REP(q, going.size()) {
                REP(j, results.size()) {
                    if (results[j].first == mp((int)going[i], (int)going[q])) {
                        if (results[j].second.first == results[j].second.second) {
                            get<0>(cnter[going[i]])--;
                            get<0>(cnter[going[q]])--;
                        }
                        else if (results[j].second.first > results[j].second.second) {
                            get<0>(cnter[going[i]]) -= 3;
                        }
                        else {
                            get<0>(cnter[going[q]]) -= 3;
                        }
                        get<1>(cnter[going[i]]) -= results[j].second.first - results[j].second.second;
                        get<1>(cnter[going[q]]) -= results[j].second.second - results[j].second.first;
                        get<2>(cnter[going[i]]) -= results[j].second.first;
                        get<2>(cnter[going[q]]) -= results[j].second.second;
                    }
                }
            }
        }
        REP(i, going.size()) {
            next[cnter[going[i]]].push_back(going[i]);
        }
        if ((*next.begin()).second.size() + winner.size() >= 2) {
            going = (*next.begin()).second;
        }
        else {
            vector<int> hoge = (*next.begin()).second;
            REP(j, hoge.size()) {
                winner.push_back(hoge[j]);
            }
            auto x = next.begin();
            x++;
            if (x != next.end()) {
                going = (*x).second;
            }
            else {
                going.clear();
                break;
            }
        }
    }
    REP(i, winner.size()) {
        if (winner[i] == target) {
            return 1.0L;
        }
    }
    long double now = 2 - (int)winner.size();
    REP(i, going.size()) {
        if (target == going[i]) {
            return now / (long double)(going.size());
        }
    }
    return 0;
}
long double memo[9];
long double scoring(int a) {
    if (memo[a]) return memo[a];
    long double ans = 1;
    for (int i = 2; i <= 8; ++i) {
        ans *= (long double)i;
    }
    for (int i = 2; i <= a; ++i) {
        ans /= (long double)i;
    }
    for (int i = 2; i <= 8 - a; ++i) {
        ans /= (long double)i;
    }
    REP(i, a) {
        ans /= (long double)4;
    }
    REP(i, 8 - a) {
        ans /= (long double)4;
        ans *= (long double)3;
    }
    return memo[a] = ans;
}
void solve() {
    results.clear();
    vector<string> inputs;
    string a;
    cin >> a;
    REP(i, 4) {
        string a;
        cin >> a;
        inputs.push_back(a);
    }
    REP(i, 4) {
        if (inputs[i][0] == '*') {
            target = i;
        }
        names[i] = inputs[i].substr(1, 3);
    }
    vector<pair<int, int>> no;
    REP(i, 4) {
        for (int q = i+1; q < 4; ++q) {
            string b = inputs[i].substr(6 + q * 5, 3);
            if (b[0] == '_') {
                no.push_back(mp(i, q));
            }
            else {
                pair<pair<int, int>, pair<int, int>> now = mp(mp((int)i, (int)q), mp((int)(b[0] - '0'), (int)(b[2] - '0')));
                results.push_back(now);
            }
        }
    }
    results.push_back(mp(no[0], mp(-1, -1)));
    results.push_back(mp(no[1], mp(-1, -1)));
    long double ans = 0;
    REP(i, 9) {
        REP(q, 9) {
            REP(j, 9) {
                REP(t, 9) {
                    long double prob = scoring(i) * scoring(q) * scoring(j) * scoring(t);
                    results[results.size()-1].second = mp(i, q);
                    results[results.size()-2].second = mp(j, t);
                    ans += prob * calc();
                }
            }
        }
    }
    cout << ans << endl;
}

#undef int
int main() {
    init();
    int t;
    cin >> t;
    REP(tea,t)
    solve();
}
accuracy: 1
time_ms: 350
memory_kb: 3296
final_rank: 12

code_snippet: #include <iomanip>
#include <iostream>
#include <cmath>
#include <vector>
#include <string>

using Bool = bool;
using Int = long long int;
using Real = long double;
using Char = char;
using String = std::string;
template <class T>
using Vector = std::vector<T>;
template <class T>
using Matrix = Vector<Vector<T>>;

struct Fout {
    Int precision;
    Fout(Int precision) : precision(precision) {}
};
std::ostream& operator<<(std::ostream& os, const Fout& fio) {
    os << std::fixed << std::setprecision(fio.precision);
    return os;
}

// p勝する確率
Vector<Real> prob;

void init() {
    prob.resize(9);
    prob[0] = std::pow(0.75L, 8);
    for (Int i = 1; i <= 8; ++i) {
        prob[i] = prob[i - 1] * Real(9 - i) / Real(i * 3);
    }
}

// 入力から得点表を生成
// 先頭が自チーム、埋まってない箇所は-1 ~ -4が振られる
Matrix<Int> parse() {
    String s;
    std::cin >> s;

    Vector<Vector<Int>> score(4, Vector<Int>(4, 0));

    {
        Int me = 0, cnt = -1;

        for (Int i = 0; i < 4; ++i) {
            std::cin >> s;
            if (s.front() == '*') me = i;

            for (Int j = i + 1; j < 4; ++j) {
                Char x = s[j * 5 + 6];
                score[i][j] = (x == '_' ? cnt-- : x - '0');
                x = s[j * 5 + 8];
                score[j][i] = (x == '_' ? cnt-- : x - '0');
            }
        }

        // 自チームを先頭へ移動
        std::swap(score[0], score[me]);
        for (auto& v : score) std::swap(v[0], v[me]);
    }

    return score;
}

void solve() {
    auto score = parse();
    Real ans = 0;

    // 空欄の点数を全列挙
    for (Int b = 0; b < 6561; ++b) {
        // デコード
        Vector<Int> pts(4);
        {
            Int c = b;
            for (Int i = 0; i < 4; ++i) {
                pts[i] = c % 9;
                c /= 9;
            }
        }

        Real pr = 1;
        for (auto p : pts) pr *= prob[p];

        // 空欄を埋める
        auto nscore = score;
        for (auto& v : nscore) {
            for (auto& x : v) {
                if (x < 0) x = pts[-x - 1];
            }
        }

        Vector<Vector<Int>> result(4);

        for (Int q = 0; q < 3; ++q) {
            // 現時点で自チームと同率のチームを列挙
            Vector<Bool> cons(4);
            for (Int i = 0; i < 4; ++i) {
                cons[i] = (result[i] == result[0]);
            }

            // 同率チーム間のポイント、得失点差、得点
            Matrix<Int> tmpres(4, Vector<Int>(3, 0));
            for (Int i = 0; i < 4; ++i) {
                if (!cons[i]) continue;
                for (Int j = 0; j < 4; ++j) {
                    if (i == j || !cons[j]) continue;

                    Int me = nscore[i][j], op = nscore[j][i];
                    tmpres[i][0] += (me > op ? 3 : me == op);
                    tmpres[i][1] += me - op;
                    tmpres[i][2] += me;
                }
            }

            // テーブルを拡張
            for (Int i = 0; i < 4; ++i) {
                if (cons[i])
                    std::move(tmpres[i].begin(), tmpres[i].end(),
                              std::back_inserter(result[i]));
            }
        }

        Int u = 0, m = 0;  // 自チームと比べて上、同率のチーム数
        for (Int i = 0; i < 4; ++i) {
            if (result[i] > result[0]) {
                ++u;
            } else if (result[i] == result[0]) {
                ++m;
            }
        }

        if (u >= 2) {  // 枠が埋まっている
            pr = 0;
        } else if (u + m >= 3) {  // 残り2-u枠をmチームで取り合う
            pr *= Real(2 - u) / Real(m);
        }
        ans += pr;
    }

    std::cout << Fout(7) << ans << std::endl;
}

int main() {
    init();
    Int n;
    std::cin >> n;
    for (Int i = 0; i < n; ++i) solve();
    return 0;
}
accuracy: 1
time_ms: 850
memory_kb: 3184
final_rank: 8

code_snippet: #if __has_include("../library/Basic/Debug.hpp")

#include "../library/Basic/Debug.hpp"

#else

/* ----- Header Files ----- */
// IO
#include <cstdio>
#include <iomanip>
#include <ios>
#include <iostream>

// algorithm
#include <algorithm>
#include <cmath>
#include <numeric>

// container
#include <vector>
#include <string>
#include <tuple>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>

// others
#include <random>
#include <limits>
#include <functional>
#include <ctime>
#include <cassert>
#include <cstdint>


/* ----- Type Alias ----- */
using Bool = bool;
using Int = long long int;
using Real = long double;
using Char = char;
using String = std::string;
template <class... Ts>
using Tuple = std::tuple<Ts...>;

template <class T>
using Vector = std::vector<T>;
template <class T>
using Matrix = Vector<Vector<T>>;
template <class T>
using Queue = std::queue<T>;
template <class T>
using Stack = std::stack<T>;
template <class T>
using Deque = std::deque<T>;

template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
using Set = std::set<T>;
template <class T, class U>
using Map = std::map<T, U>;

template <class T, class... Us>
using Func = std::function<T(Us...)>;

template <class T>
T genv(T v) { return v; }

template <class T, class... Ts>
auto genv(size_t l, Ts... ts) {
    return Vector<decltype(genv<T>(ts...))>(l, genv<T>(ts...));
}

template <class Cost = Int>
struct Edge {
    Int src, dst;
    Cost cost;
    Edge(Int src = -1, Int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = Int>
using Edges = Vector<Edge<Cost>>;
template <class Cost = Int>
using Graph = Vector<Vector<Edge<Cost>>>;

#endif

/* ----- Misc ----- */
void fastio() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
}

struct Fout {
    Int precision;
    Fout(Int precision) : precision(precision) {}
};
std::ostream& operator<<(std::ostream& os, const Fout& fio) {
    os << std::fixed << std::setprecision(fio.precision);
    return os;
}


/* ----- Constants ----- */
// constexpr Int INF = std::numeric_limits<Int>::max() / 3;
// constexpr Int MOD = 1000000007;
// constexpr Real PI = acos(-1);
// constexpr Real EPS = 1e-10;
// std::mt19937 mt(int(std::time(nullptr)));

Vector<Real> prob;

void init() {
    prob.resize(9);
    prob[0] = std::pow(0.75L, 8);
    for (Int i = 1; i <= 8; ++i) {
        prob[i] = prob[i - 1] * Real(9 - i) / Real(i * 3);
    }
}

void solve() {
    String s;
    std::cin >> s;

    Vector<Vector<Int>> score(4, Vector<Int>(4, 0));

    {
        Int me = 0, cnt = -1;

        for (Int i = 0; i < 4; ++i) {
            std::cin >> s;
            if (s.front() == '*') me = i;

            for (Int j = i + 1; j < 4; ++j) {
                Char x = s[j * 5 + 6];
                score[i][j] = (x == '_' ? cnt-- : x - '0');
                x = s[j * 5 + 8];
                score[j][i] = (x == '_' ? cnt-- : x - '0');
            }
        }

        std::swap(score[0], score[me]);
        for (auto& v : score) std::swap(v[0], v[me]);
    }

    Real ans = 0;
    for (Int b = 0; b < 6561; ++b) {
        Vector<Int> pts(4);
        {
            Int c = b;
            for (Int i = 0; i < 4; ++i) {
                pts[i] = c % 9;
                c /= 9;
            }
        }

        Real pr = 1;
        for (auto p : pts) pr *= prob[p];

        auto s = score;
        for (auto& v : s) {
            for (auto& x : v) {
                if (x < 0) x = pts[-x - 1];
            }
        }

        Vector<Vector<Int>> result(4);

        for (Int q = 0; q < 3; ++q) {
            Int cons = 1;
            for (Int i = 1; i < 4; ++i) {
                if (result[i] == result[0]) cons += (1 << i);
            }

            Vector<Vector<Int>> tmpres(4, Vector<Int>(3, 0));
            for (Int i = 0; i < 4; ++i) {
                if (((cons >> i) & 1) == 0) continue;
                for (Int j = 0; j < 4; ++j) {
                    if (i == j || ((cons >> j) & 1) == 0) continue;
                    tmpres[i][0] += (s[i][j] > s[j][i] ? 3 : s[i][j] == s[j][i]);
                    tmpres[i][1] += s[i][j] - s[j][i];
                    tmpres[i][2] += s[i][j];
                }
            }

            for (Int i = 0; i < 4; ++i) {
                if ((cons >> i) & 1)
                    std::move(tmpres[i].begin(), tmpres[i].end(),
                              std::back_inserter(result[i]));
            }
        }

        Int u = 0, m = 0;
        for (Int i = 0; i < 4; ++i) {
            if (result[i] > result[0]) {
                ++u;
            } else if (result[i] == result[0]) {
                ++m;
            }
        }

        if (u >= 2) {
            pr = 0;
        } else if (u + m >= 3) {
            pr *= Real(2 - u) / Real(m);
        }
        ans += pr;
    }

    std::cout << Fout(7) << ans << std::endl;
}

int main() {
    init();
    Int n;
    std::cin >> n;
    for (Int i = 0; i < n; ++i) solve();
    return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 3272
final_rank: 13

code_snippet: #include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
using namespace std;
double W[9];
int x[4][4]={{0,1,1,1},{1,0,2,2},{1,2,0,3},{1,2,3,0}};
int y[4][4]={{0,11,16,21},{11,0,16,21},{16,16,0,21},{21,21,21,0}};
int mx[6]={1,1,1,2,2,3},my[6]={11,16,21,16,21,21};
int ax[12]={1,1,1,1,1,1,2,2,2,2,3,3},ay[12]={11,13,16,18,21,23,16,18,21,23,21,23};
string s[5];
int winner;
double dfs(int id)
{
	if(id==12)
	{
		vector<pair<pair<int,pair<int,int> >,int> >A(4);
		for(int i=0;i<4;i++)
		{
			int p=0,d=0,g=0;
			for(int j=0;j<4;j++)
			{
				if(i==j)continue;
				int a=s[x[i][j]][y[i][j]]-'0';
				int b=s[x[i][j]][y[i][j]+2]-'0';
				if(x[i][j]>i)
				{
					p+=a>b?3:a==b?1:0;
					d+=a-b;
					g+=a;
				}
				else
				{
					p+=b>a?3:b==a?1:0;
					d+=b-a;
					g+=b;
				}
			}
			A[i]=make_pair(make_pair(-p,make_pair(-d,-g)),i);
		}
		sort(A.begin(),A.end());
		vector<int>B;
		int p=0;
		for(int i=1;i<=4;i++)
		{
			if(i<4&&A[p].first==A[i].first)continue;
			else
			{
				if(p+1==i)B.push_back(A[p].second);
				else
				{
					vector<int>I;
					for(int j=p;j<i;j++)I.push_back(A[j].second);
					int pre=114514;
					while(I.size()<pre)
					{
						pre=I.size();
						vector<pair<pair<int,pair<int,int> >,int> >C;
						for(int j=0;j<I.size();j++)
						{
							int p=0,d=0,g=0;
							for(int k=0;k<I.size();k++)
							{
								if(j==k)continue;
								int a=s[x[I[j]][I[k]]][y[I[j]][I[k]]]-'0';
								int b=s[x[I[j]][I[k]]][y[I[j]][I[k]]+2]-'0';
								if(x[I[j]][I[k]]>I[j])
								{
									p+=a>b?3:a==b?1:0;
									d+=a-b;
									g+=a;
								}
								else
								{
									p+=b>a?3:b==a?1:0;
									d+=b-a;
									g+=b;
								}
							}
							C.push_back(make_pair(make_pair(-p,make_pair(-d,-g)),I[j]));
						}
						sort(C.begin(),C.end());
						int q=0;
						vector<int>J;
						for(int j=1;j<=C.size();j++)
						{
							if(j<C.size()&&C[q].first==C[j].first)continue;
							else
							{
								if(q+1==j)B.push_back(C[q++].second);
								else
								{
									bool flag=0;
									for(int k=0;k<I.size();k++)flag|=I[k]==winner;
									if(!flag)
									{
										for(;q<j;q++)B.push_back(C[q].second);
									}
									else
									{
										for(;q<j;q++)J.push_back(C[q].second);
										break;
									}
								}
							}
						}
						I=J;
					}
					if(!I.empty())
					{
						bool flag=0;
						for(int j=0;j<I.size();j++)flag|=I[j]==winner;
						if(!flag)
						{
							for(int j=0;j<I.size();j++)B.push_back(I[j]);
						}
						else
						{
							if(B.size()>=2)return 0;
							else
							{
								double rest=2-B.size();
								return rest/I.size();
							}
						}
					}
				}
				p=i;
			}
		}
		return B[0]==winner||B[1]==winner;
	}
	else
	{
		if(s[ax[id]][ay[id]]!='_')return dfs(id+1);
		else
		{
			double ret=0;
			for(char c='0';c<='8';c++)
			{
				s[ax[id]][ay[id]]=c;
				ret+=W[c-'0']*dfs(id+1);
				s[ax[id]][ay[id]]='_';
			}
			return ret;
		}
	}
}
main()
{
	for(int p=0;p<=8;p++)
	{
		W[p]=1;
		for(int j=1;j<=8;j++)W[p]*=j;
		for(int j=1;j<=p;j++)W[p]/=j;
		for(int j=1;j<=8-p;j++)W[p]/=j;
		for(int j=1;j<=p;j++)W[p]/=4;
		for(int j=1;j<=8-p;j++)W[p]*=3./4;
	}
	int T;cin>>T;
	for(;T--;)
	{
		for(int i=0;i<5;i++)
		{
			cin>>s[i];
			if(s[i][0]=='*')winner=i-1;
		}
		cout<<fixed<<setprecision(9)<<dfs(0)<<endl;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3192
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ld=long double;
struct Match{
    int lhs,rhs;
    int lhsp,rhsp;
};

void debug(Match x){
    //cerr<<x.lhs<<"vs"<<x.rhs<<" "<<x.lhsp<<"-"<<x.rhsp<<endl;
}
void debug(vector<Match> x){
    //cerr<<"#These are match"<<endl;
    for(int i=0;i<x.size();i++){
        debug(x[i]);
    }
}
ld winner(const vector<Match> &match,int tar,int place){
    vector<int> pt(4);
    vector<int> gd(4);
    vector<int> sc(4);
    for(auto &m:match){
        pt[m.lhs]++;
        pt[m.rhs]++;
        if(m.lhsp<m.rhsp) pt[m.rhs]+=300;
        else if(m.lhsp>m.rhsp) pt[m.lhs]+=300;
        else pt[m.rhs]+=100,pt[m.lhs]+=100;
        gd[m.lhs]+=m.lhsp-m.rhsp;
        gd[m.rhs]+=m.rhsp-m.lhsp;
        sc[m.lhs]+=m.lhsp;
        sc[m.rhs]+=m.rhsp;
    }
    vector<int> rs(4);
    for(int i=0;i<4;i++) rs[i]=pt[i]*1e5+gd[i]*1e3+sc[i];
    int x=*max_element(rs.begin(),rs.end());
    if(rs[tar]==x){
        if(count(rs.begin(),rs.end(),x)==1) return 1;
        vector<Match> next;
        for(auto &m:match){
            if(rs[m.lhs]==x && rs[m.rhs]==x) next.push_back(m);
        }
        //cerr<<"#"<<next.size()<<endl;
        if(match.size()==next.size()){
            int cnt=count(rs.begin(),rs.end(),x);
            if(cnt==1) return 1;
            else return ld(place)/cnt;
        }
        else return winner(next,tar,place);
    }
    if(place<=count(rs.begin(),rs.end(),x)) return 0;
    int sndmax=0;
    for(int i=0;i<4;i++){
        if(rs[i]!=x) sndmax=max(sndmax,rs[i]);
    }
    if(rs[tar]==sndmax){
        if(count(rs.begin(),rs.end(),sndmax)==1) return 1;
        vector<Match> next;
        for(auto &m:match){
            if(rs[m.lhs]==sndmax && rs[m.rhs]==sndmax) next.push_back(m);
        }
        return winner(next,tar,1);
    }
    return 0;
}
ld pos(int point){
    ld res=1;
    for(int i=0;i<8;i++) res*=(i+1);
    for(int i=0;i<point;i++) res*=0.25/(i+1);
    for(int i=0;i<8-point;i++) res*=0.75/(i+1);
    return res;
}
int cnt=0;
ld dfs(int idx,vector<Match> &match,ld p,int tar){
    if(idx==match.size()){
        ld x=winner(match,tar,2);
        cnt++;
        debug(match);
        //cerr<<x<<endl;
        return p*x;
    }
    ld res=0;
    if(match[idx].lhsp==-1){
        for(int i=0;i<=8;i++){
            for(int j=0;j<=8;j++){
                match[idx].lhsp=i;
                match[idx].rhsp=j;
                res+=dfs(idx+1,match,p*pos(i)*pos(j),tar);
                match[idx].lhsp=-1;
                match[idx].rhsp=-1;
            }
        }
    }
    else{
        return dfs(idx+1,match,p,tar);
    }
    return res;
}
ld solve(){
    array<string,5> table;
    for(int i=0;i<5;i++) cin>>table[i];
    int tar=0;
    for(int i=0;i<4;i++){
        if(table[i+1][0]=='*') tar=i;
    }
    const int WIDTH=5;
    const int LHS=1;
    const int RHS=3;
    vector<Match> match;
    for(int i=0;i<4;i++){
        for(int j=0;j<i;j++){
            char lhsp=table[j+1][WIDTH*(i+1)+LHS];
            char rhsp=table[j+1][WIDTH*(i+1)+RHS];
            if(isdigit(lhsp)){
                assert(isdigit(rhsp));
                match.push_back(Match{j,i,lhsp-'0',rhsp-'0'});
            }
            else{
                match.push_back(Match{j,i,-1,-1});
            }
        }
    }
    assert(match.size()==6);
    return dfs(0,match,1,tar);
}
int main(){
    int n;
    cin>>n;
    cout<<setprecision(10)<<fixed;
    for(int i=0;i<n;i++){
        cout<<solve()<<endl;    
    }

    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3212
final_rank: 6

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-4;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
struct result {
	int x, y, cx, cy;
};
result a[6];
int target;
int c[4];
void a_calc() {
	rep(i, 4) {
		c[i] = 0;
	}
	rep(i, 6) {
		if (a[i].cx > a[i].cy)c[a[i].x] += 3;
		else if (a[i].cx < a[i].cy)c[a[i].y] += 3;
		else {
			c[a[i].x]++; c[a[i].y]++;
		}
	}
}
void b_calc() {
	rep(i, 4) {
		c[i] = 0;
	}
	rep(i, 6) {
		c[a[i].x] += a[i].cx - a[i].cy;
		c[a[i].y] += a[i].cy - a[i].cx;
	}
}
void c_calc() {
	rep(i, 4) {
		c[i] = 0;
	}
	rep(i, 6) {
		c[a[i].x] += a[i].cx;
		c[a[i].y] += a[i].cy;
	}
}
void d_calc(vector<int> v) {
	rep(i, 4) {
		c[i] = 0;
	}
	int exi[4] = {};
	rep(i, v.size()) {
		exi[v[i]] = 1;
	}
	rep(i, 6) {
		if (!exi[a[i].x] || !exi[a[i].y])continue;
		if (a[i].cx > a[i].cy)c[a[i].x] += 3;
		else if (a[i].cx < a[i].cy)c[a[i].y] += 3;
		else {
			c[a[i].x]++; c[a[i].y]++;
		}
	}
}
void e_calc(vector<int> v) {
	rep(i, 4) {
		c[i] = 0;
	}
	int exi[4] = {};
	rep(i, v.size()) {
		exi[v[i]] = 1;
	}
	rep(i, 6) {
		if (!exi[a[i].x] || !exi[a[i].y])continue;
		c[a[i].x] += a[i].cx - a[i].cy;
		c[a[i].y] += a[i].cy - a[i].cx;
	}
}
void f_calc(vector<int> v) {
	rep(i, 4) {
		c[i] = 0;
	}
	int exi[4] = {};
	rep(i, v.size()) {
		exi[v[i]] = 1;
	}
	rep(i, 6) {
		if (!exi[a[i].x] || !exi[a[i].y])continue;
		c[a[i].x] += a[i].cx;
		c[a[i].y] += a[i].cy;
	}
}
vector<int> a_rule(vector<int> u) {
	vector<int> res;
	a_calc();
	vector<P> v;
	rep(i, u.size()) {
		v.push_back({ c[u[i]],u[i] });
	}
	sort(v.begin(), v.end(),greater<P>());
	int cnt = 0;
	rep(i, v.size()) {
		res.push_back(v[i].second);
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; res.push_back(v[i].second);
		}
		cnt++;
		if (cnt == 1)break;
	}
	sort(res.begin(), res.end()); return res;
}
vector<int> b_rule(vector<int> v) {
	vector<int> res;
	b_calc();
	vector<P> u;
	rep(i, v.size()) {
		u.push_back({ c[v[i]],v[i] });
	}
	sort(u.begin(), u.end(), greater<P>());
	int cnt = 0;
	rep(i, u.size()) {
		res.push_back(u[i].second);
		while (i + 1 < u.size() && u[i].first == u[i + 1].first) {
			i++; res.push_back(u[i].second);
		}
		cnt++; if (cnt == 1)break;
	}
	sort(res.begin(), res.end()); return res;
}
vector<int> c_rule(vector<int> v) {
	vector<int> res; c_calc();
	vector<P> u;
	rep(i, v.size()) {
		u.push_back({ c[v[i]],v[i] });
	}
	sort(u.begin(), u.end(), greater<P>());
	int cnt = 0;
	rep(i, u.size()) {
		res.push_back(u[i].second);
		while (i + 1 < u.size() && u[i].first == u[i + 1].first) {
			i++; res.push_back(u[i].second);
		}
		cnt++; if (cnt == 1)break;
	}
	sort(res.begin(), res.end()); return res;
}
vector<int> d_rule(vector<int> v) {
	vector<int> res; d_calc(v);
	vector<P> u;
	rep(i, v.size()) {
		u.push_back({ c[v[i]],v[i] });
	}
	sort(u.begin(), u.end(), greater<P>());
	int cnt = 0;
	rep(i, u.size()) {
		res.push_back(u[i].second);
		while (i + 1 < u.size() && u[i].first == u[i + 1].first) {
			i++; res.push_back(u[i].second);
		}
		cnt++; if (cnt == 1)break;
	}
	sort(res.begin(), res.end()); return res;
}
vector<int> e_rule(vector<int> v) {
	vector<int> res; e_calc(v);
	vector<P> u;
	rep(i, v.size()) {
		u.push_back({ c[v[i]],v[i] });
	}
	sort(u.begin(), u.end(), greater<P>());
	int cnt = 0;
	rep(i, u.size()) {
		res.push_back(u[i].second);
		while (i + 1 < u.size() && u[i].first == u[i + 1].first) {
			i++; res.push_back(u[i].second);
		}
		cnt++; if (cnt == 1)break;
	}
	sort(res.begin(), res.end()); return res;
}
vector<int> f_rule(vector<int> v) {
	vector<int> res; f_calc(v);
	vector<P> u;
	rep(i, v.size()) {
		u.push_back({ c[v[i]],v[i] });
	}
	sort(u.begin(), u.end(), greater<P>());
	int cnt = 0;
	rep(i, u.size()) {
		res.push_back(u[i].second);
		while (i + 1 < u.size() && u[i].first == u[i + 1].first) {
			i++; res.push_back(u[i].second);
		}
		cnt++; if (cnt == 1)break;
	}
	sort(res.begin(), res.end()); return res;
}
ld ret() {
	bool one = false;
	vector<int> v = { 0,1,2,3 }; vector<int> cop;
	cop = v; v = a_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
		v = a_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}
	cop = v; v = b_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
		v = b_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}
	cop = v; v = c_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
		v = c_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}
	cop = v;
		v = d_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = d_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = e_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == i)continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = e_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = f_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v =f_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = d_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = d_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = e_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == i)continue;
			cv.push_back(cop[i]);
		}
		v = cv;
		v = e_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = f_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = f_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = d_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = d_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = e_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == i)continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = e_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}cop = v;
		v = f_rule(v);
	if (v.size() == 1 && !one) {
		one = true;
		if (v[0] == target)return 1;
		vector<int> cv;
		rep(i, cop.size()) {
			if (v[0] == cop[i])continue;
			cv.push_back(cop[i]);
		}
		v = cv;
			v = f_rule(v);
	}
	if (v.size() == 1) {
		if (v[0] == target)return 1;
		else return 0;
	}
	if (!one) {
		rep(i, v.size()) {
			if (v[i] == target) {
				return 2 / (ld)v.size();
			}
		}
		return 0;
	}
	else {
		rep(i, v.size()) {
			if (v[i] == target) {
				return 1 / (ld)v.size();
			}
		}
		return 0;
	}
}
ld pos[9];
ld pp[9];
void init() {
	pp[0] = 1;
	rep1(i, 8) {
		pp[i] = pp[i - 1] * i;
	}
	rep(i, 9) {
		pos[i] = pp[8] / (pp[i] * pp[8 - i]);
		rep(j, i) {
			pos[i] *= 0.25;
		}
		rep(j, 8 - i) {
			pos[i] *= 0.75;
		}
		//cout << pos[i] << endl;
	}
}
P check[6] = { {1,11},{1,16},{1,21},{2,16},{2,21},{3,21} };
P loc[6] = { {0,1},{0,2},{0,3},{1,2},{1,3},{2,3} };
int main() {
	init();
	cout << fixed << setprecision(8);
	int n; cin >> n;
	rep(aa, n) {
		string s[5];
		rep(i, 5) {
			cin >> s[i];
			if (s[i][0] == '*')target = i-1;
		}
		ld out = 0;
		//int num = 0, hie = 0;
		rep(p1, 9) {
			rep(p2, 9) {
				rep(p3, 9) {
					rep(p4, 9) {
						int now = 0; int cnow = 0;
						rep(i, 6) {
							int x = check[i].first; int y = check[i].second;
							if (s[x][y] == '_') {
								if (cnow == 0) {
									a[i] = { loc[i].first,loc[i].second,p1,p2 }; cnow++;
								}
								else {
									a[i] = { loc[i].first,loc[i].second,p3,p4 };
								}
							}
							else {
								a[i] = { loc[i].first,loc[i].second,s[x][y] - '0',s[x][y + 2] - '0' };
							}
						}
						out += pos[p1] * pos[p2] * pos[p3] * pos[p4] * ret();
						//if (ret() == 1)num++;
						//if (ret() == 0)hie++;
					}
				}
			}
		}
		cout << out << endl;
		//cout << num << " " << hie << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 3280
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 4

struct Info{
	bool operator<(const struct Info& arg) const{

		if(points != arg.points){

			return points > arg.points;

		}else if(goal_diff != arg.goal_diff){

			return goal_diff > arg.goal_diff;
		}else{
			return sum_goal > arg.sum_goal;
		}
	}
	bool operator==(const struct Info &arg) const{
		return points == arg.points && goal_diff == arg.goal_diff && sum_goal == arg.sum_goal;
	}

	int team_id,points,goal_diff,sum_goal;
};

struct Data{
	int from_team,to_team;
};

struct State{
	State(int arg_team_id,int arg_rank){

		team_id = arg_team_id;
		rank = arg_rank;
	}
	int team_id,rank;
};

int num_case;
char table[5][25];
double poss_point[9];
Info info[5];
Data data[2];


int get_point(char buf[3]){

	int from_point = buf[0]-'0';
	int to_point = buf[2]-'0';

	if(from_point > to_point){

		return 3;

	}else if(from_point == to_point){

		return 1;

	}else{

		return 0;
	}
}

int get_diff(char buf[3]){

	int from_point = buf[0]-'0';
	int to_point = buf[2]-'0';

	return from_point-to_point;
}

void init(vector<int> member){

	for(int i = 0; i < member.size(); i++){
		info[member[i]].points = 0;
		info[member[i]].goal_diff = 0;
		info[member[i]].sum_goal = 0;
	}
}

//最大2位以内を返す関数(1位が2チーム以上あったら、即返却)
vector<State> select(vector<int> member){

	Info tmp_info[5];
	for(int i = 0; i < member.size(); i++){

		tmp_info[member[i]].points = 0;
		tmp_info[member[i]].goal_diff = 0;
		tmp_info[member[i]].sum_goal = 0;
		tmp_info[member[i]].team_id = member[i];
	}

	int tmp_row,tmp_col,tmp;
	char calc_buf[3];

	for(int i = 0; i < member.size()-1; i++){
		for(int k = i+1; k < member.size(); k++){

			tmp_row = member[i];
			tmp_col = 4+5*(member[k]-1);

			for(int a = 2; a <= 4; a++){
				calc_buf[a-2] = table[tmp_row][tmp_col+a];
			}

			tmp = get_point(calc_buf);

			if(tmp == 1){

				tmp_info[member[i]].points += 1;
				tmp_info[member[k]].points += 1;

			}else{

				tmp_info[member[i]].points += tmp;
				tmp_info[member[k]].points += 3-tmp;
			}

			tmp = get_diff(calc_buf);

			tmp_info[member[i]].goal_diff += tmp;
			tmp_info[member[k]].goal_diff -= tmp;

			tmp_info[member[i]].sum_goal += calc_buf[0]-'0';
			tmp_info[member[k]].sum_goal += calc_buf[2]-'0';
		}
	}

	vector<State> ret;
	vector<Info> calc_info;

	int num = (int)member.size();
	//printf("num:%d\n",num);

	for(int i = 0; i < num; i++){
		calc_info.push_back(tmp_info[member[i]]);
	}

	sort(calc_info.begin(),calc_info.end());

	int base_index = 0,rank = 0,right;

	while(ret.size() < 2 && rank < 2 && base_index < num){

		ret.push_back(State(calc_info[base_index].team_id,rank));

		right = base_index+1;
		if(right == num)break;

		if(calc_info[base_index] == calc_info[right]){

			while(right < num && calc_info[base_index] == calc_info[right]){
				ret.push_back(State(calc_info[right].team_id,rank));
				right++;
			}
			base_index = right;

		}else{

			base_index = right;
		}
		rank++;
	}

	return ret;
}

void func(){

	int base_team,work_index,data_index = 0;

	char work[3];
	vector<int> member;

	for(int i = 1; i <= 4; i++){
		member.push_back(i);
		info[i].team_id = i;
	}

	init(member);

	int from_team,to_team,tmp;

	for(int row = 0; row <= NUM; row++){
		scanf("%s",table[row]);
		if(table[row][0] == '*'){
			base_team = row;
		}

		if(row == 0)continue;

		from_team = row;
		to_team = from_team+1;

		for(int start_col = 4+5*row; start_col <= 19; start_col += 5,to_team++){

			if(table[row][start_col+2] == '_'){ //★結果不明な組合せ★

				data[data_index].from_team = from_team;
				data[data_index++].to_team = to_team;

				continue;
			}

			work_index = 0;
			for(int i = 2; i <= 4; i++){
				work[work_index++] = table[row][start_col+i];
			}

			//勝ち点
			tmp = get_point(work);

			if(tmp == 1){

				info[from_team].points += 1;
				info[to_team].points += 1;

			}else{

				info[from_team].points += tmp;
				info[to_team].points += 3-tmp;
			}

			//goal_diff
			tmp = get_diff(work);
			info[from_team].goal_diff += tmp;
			info[to_team].goal_diff -= tmp;

			//goal総数
			info[from_team].sum_goal += work[0]-'0';
			info[to_team].sum_goal += work[2]-'0';
		}
	}

	double ans = 0;
	double tmp_poss;

	vector<State> Top_Group;

	int tmp_from,tmp_to;
	int pre_size,num_decided,tmp_index,num_rank_0;

	bool check[5];
	bool FLG;

	//未判明の、2試合の得点を全探索
	for(int from_1 = 0; from_1 <= 8;from_1++){
		for(int to_1 = 0; to_1 <= 8; to_1++){
			for(int from_2 = 0; from_2 <= 8; from_2++){
				for(int to_2 = 0; to_2 <= 8; to_2++){

					Top_Group.clear();
					tmp_poss = 1.0;

					//まず、このトーナメント表が出来上がる確率を求める
					tmp_poss *= poss_point[from_1];
					tmp_poss *= poss_point[to_1];
					tmp_poss *= poss_point[from_2];
					tmp_poss *= poss_point[to_2];

					member.clear();
					for(int i = 1; i <= 4; i++){
						member.push_back(i);
					}

					//★表を完成させる★

					for(int loop = 0; loop < 2; loop++){

						from_team = data[loop].from_team;
						to_team = data[loop].to_team;

						if(loop == 0){

							tmp_from = from_1;
							tmp_to = to_1;

						}else{

							tmp_from = from_2;
							tmp_to = to_2;
						}

						//★テーブルを埋める★
						table[from_team][4+5*(to_team-1)+2] = '0'+tmp_from;
						table[from_team][4+5*(to_team-1)+3] = '-';
						table[from_team][4+5*(to_team-1)+4] = '0'+tmp_to;
					}

					num_decided = 0;
					//上位チームを選んでみる(1回目)
					Top_Group = select(member);

					FLG = false;

					num_rank_0 = 0;
					for(int k = 0; k < Top_Group.size(); k++){

						if(Top_Group[k].rank == 0)num_rank_0++;

						if(base_team == Top_Group[k].team_id){
							tmp_index = k;
							FLG = true;
						}
					}

					if(Top_Group.size() == 2){

						//上位2チームに入っていればOK、そうでなければ不可
						if(FLG){
							ans += tmp_poss;
						}else{
							//Do nothing
						}
						continue;

					}else if(Top_Group.size() == 3){

						//★★上位3チームに含まれていなければ不可★★
						if(!FLG)continue;

						//★単独1位ならOK★
						if(Top_Group[tmp_index].rank == 0 && num_rank_0 == 1){
							ans += tmp_poss;
							continue;
						}

					}else{ //Top_Group.size() == 4

						//★単独1位ならOK★
						if(Top_Group[tmp_index].rank == 0 && num_rank_0 == 1){
							ans += tmp_poss;
							continue;
						}
					}

					//通過or脱落が確定した人物を取り除く
					if(num_rank_0 == (int)Top_Group.size()){ //1位が3個か4個

						if(num_rank_0 == 3){ //1人脱落

							for(int k = 1; k <= 4; k++)check[k] = false;

							for(int i = 0; i < Top_Group.size(); i++){
								check[Top_Group[i].team_id] = true;
							}
							member.clear();

							for(int k = 1; k <= 4; k++){
								if(check[k]){
									member.push_back(k);
								}
							}

						}else{ //1位が4人
							//Do nothing
						}

					}else if(num_rank_0 != (int)Top_Group.size()){ //num_rank_0が2以上なら、1ターン目で返る==Top_Groupのサイズと一致する。よって、num_rank_0 == 1

							for(int k = 1; k <= 4; k++)check[k] = false;

							for(int i = 0; i < Top_Group.size(); i++){
								check[Top_Group[i].team_id] = true;
							}
							member.clear();

							for(int k = 0; k < Top_Group.size(); k++){
								if(Top_Group[k].rank != 0)continue;

								num_decided++;
								check[Top_Group[k].team_id] = false; //通過が確定したので集合から除く
							}

							for(int k = 1; k <= 4; k++){
								if(check[k]){
									member.push_back(k);
								}
							}
					}

					int debug = (int)member.size();
					pre_size = (int)member.size();

					//★★2位通過が決定するまで、ループ★★
					while(true){

						Top_Group = select(member);

						FLG = false;
						num_rank_0 = 0;
						for(int k = 0; k < Top_Group.size(); k++){

							if(Top_Group[k].rank == 0)num_rank_0++;

							if(base_team == Top_Group[k].team_id){
								tmp_index = k;
								FLG = true;
							}
						}

						if(num_decided+(int)Top_Group.size() == 2){ //上位2チーム決定

							if(FLG){ //上位2チームに含まれれば良い

								ans += tmp_poss;

							}else{
								//Do nothing
							}

							break;

						}else if(num_decided+num_rank_0 == 2){ //1位のチームのみ抜き出せば、上位2チーム決定

							if(FLG == true && Top_Group[tmp_index].rank == 0){ //1位

								ans += tmp_poss;

							}else{ //★他のチームによって枠が取られた場合★

								//Do nothing
							}

							break;
						}


						//通過or脱落が確定した人物を取り除く
						if(num_rank_0 == (int)Top_Group.size()){ //1位が2～4個

							if(num_rank_0 <= 3){ //1人～2人脱落

								for(int k = 1; k <= 4; k++)check[k] = false;

								for(int i = 0; i < Top_Group.size(); i++){
									check[Top_Group[i].team_id] = true;
								}
								member.clear();

								for(int k = 1; k <= 4; k++){
									if(check[k]){
										member.push_back(k);
									}
								}

							}else{ //1位が4人
								//Do nothing
							}

						}else if(num_rank_0 != Top_Group.size()){

								if(FLG == true && Top_Group[tmp_index].rank == 0){ //単独1位
									ans += tmp_poss;
									break;
								}

								for(int k = 1; k <= 4; k++)check[k] = false;

								for(int i = 0; i < member.size(); i++){
									check[member[i]] = true;
								}
								member.clear();

								for(int k = 0; k < Top_Group.size(); k++){
									if(Top_Group[k].rank != 0)continue;

									num_decided++;
									check[Top_Group[k].team_id] = false; //通過が確定したので集合から除く
								}

								for(int k = 1; k <= 4; k++){
									if(check[k]){
										member.push_back(k);
									}
								}
						}


						if((int)member.size() == pre_size){

							if(FLG){

								if(num_decided == 1){ //1つ選ぶ

									ans += tmp_poss/(double)member.size();

								}else{ //★★★2つ選ぶ★★★

									ans += tmp_poss*2.0/(double)member.size();
								}

							}else{

								//Do nothing
							}

							break;
						}

						pre_size = (int)member.size();
					}

				}
			}
		}
	}

	printf("%.10lf\n",ans);

}

void calc_poss(){

	int nCm[9][9];

	for(int i = 0; i <= 8; i++){
		for(int k = 0; k <= 8; k++)nCm[i][k]= 0;
	}

	nCm[0][0] = 1;
	for(int n = 1; n <= 8; n++) {
		for (int k = 0; k <= 8; k++) {
			if(k > 0){
				nCm[n][k] = nCm[n-1][k]+nCm[n-1][k-1];
			}else{ //k == 0
				nCm[n][k] = 1;
			}
		}
	}

	double poss;

	for(int point = 0; point <= 8; point++){

		poss = nCm[8][point];

		for(int i = 0; i < point; i++){
			poss *= 0.25;
		}
		for(int i = 0; i < 8-point; i++){
			poss *= 0.75;
		}
		poss_point[point] = poss;
	}
}

int main(){

	calc_poss();

	scanf("%d",&num_case);

	for(int loop = 0; loop < num_case; loop++){
		func();
	}

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3252
final_rank: 7

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 4

struct Info{
	bool operator<(const struct Info& arg) const{

		if(points != arg.points){

			return points > arg.points;

		}else if(goal_diff != arg.goal_diff){

			return goal_diff > arg.goal_diff;
		}else{
			return sum_goal > arg.sum_goal;
		}
	}
	bool operator==(const struct Info &arg) const{
		return points == arg.points && goal_diff == arg.goal_diff && sum_goal == arg.sum_goal;
	}

	int team_id,points,goal_diff,sum_goal;
};

struct Data{
	int from_team,to_team;
};

struct State{
	State(int arg_team_id,int arg_rank){

		team_id = arg_team_id;
		rank = arg_rank;
	}
	int team_id,rank;
};

int num_case;
char table[5][25];
double poss_point[9];
Info info[5];
Data data[2];


int get_point(char buf[3]){

	int from_point = buf[0]-'0';
	int to_point = buf[2]-'0';

	if(from_point > to_point){

		return 3;

	}else if(from_point == to_point){

		return 1;

	}else{

		return 0;
	}
}

int get_diff(char buf[3]){

	int from_point = buf[0]-'0';
	int to_point = buf[2]-'0';

	return from_point-to_point;
}

void init(vector<int> member){

	for(int i = 0; i < member.size(); i++){
		info[member[i]].points = 0;
		info[member[i]].goal_diff = 0;
		info[member[i]].sum_goal = 0;
	}
}

//最大2位以内を返す関数(1位が2チーム以上あったら、即返却)
vector<State> select(vector<int> member){

	Info tmp_info[5];
	for(int i = 0; i < member.size(); i++){

		tmp_info[member[i]].points = 0;
		tmp_info[member[i]].goal_diff = 0;
		tmp_info[member[i]].sum_goal = 0;
		tmp_info[member[i]].team_id = member[i];
	}

	int tmp_row,tmp_col,tmp;
	char calc_buf[3];

	for(int i = 0; i < member.size()-1; i++){
		for(int k = i+1; k < member.size(); k++){

			tmp_row = member[i];
			tmp_col = 4+5*(member[k]-1);

			for(int a = 2; a <= 4; a++){
				calc_buf[a-2] = table[tmp_row][tmp_col+a];
			}

			tmp = get_point(calc_buf);

			if(tmp == 1){

				tmp_info[member[i]].points += 1;
				tmp_info[member[k]].points += 1;

			}else{

				tmp_info[member[i]].points += tmp;
				tmp_info[member[k]].points += 3-tmp;
			}

			tmp = get_diff(calc_buf);

			tmp_info[member[i]].goal_diff += tmp;
			tmp_info[member[k]].goal_diff -= tmp;

			tmp_info[member[i]].sum_goal += calc_buf[0]-'0';
			tmp_info[member[k]].sum_goal += calc_buf[2]-'0';
		}
	}

	vector<State> ret;
	vector<Info> calc_info;

	int num = (int)member.size();
	//printf("num:%d\n",num);

	for(int i = 0; i < num; i++){
		calc_info.push_back(tmp_info[member[i]]);
	}

	sort(calc_info.begin(),calc_info.end());

	int base_index = 0,rank = 0,right;

	while(ret.size() < 2 && rank < 2 && base_index < num){

		ret.push_back(State(calc_info[base_index].team_id,rank));

		right = base_index+1;
		if(right == num)break;

		if(calc_info[base_index] == calc_info[right]){

			while(right < num && calc_info[base_index] == calc_info[right]){
				ret.push_back(State(calc_info[right].team_id,rank));
				right++;
			}
			base_index = right;

		}else{

			base_index = right;
		}
		rank++;
	}

	return ret;
}

void func(){

	int base_team,work_index,data_index = 0;

	char work[3];
	vector<int> member;

	for(int i = 1; i <= 4; i++){
		member.push_back(i);
		info[i].team_id = i;
	}

	init(member);

	int from_team,to_team,tmp;

	for(int row = 0; row <= NUM; row++){
		scanf("%s",table[row]);
		if(table[row][0] == '*'){
			base_team = row;
		}

		if(row == 0)continue;

		from_team = row;
		to_team = from_team+1;

		for(int start_col = 4+5*row; start_col <= 19; start_col += 5,to_team++){

			if(table[row][start_col+2] == '_'){ //★結果不明な組合せ★

				data[data_index].from_team = from_team;
				data[data_index++].to_team = to_team;

				continue;
			}

			work_index = 0;
			for(int i = 2; i <= 4; i++){
				work[work_index++] = table[row][start_col+i];
			}

			//勝ち点
			tmp = get_point(work);

			if(tmp == 1){

				info[from_team].points += 1;
				info[to_team].points += 1;

			}else{

				info[from_team].points += tmp;
				info[to_team].points += 3-tmp;
			}

			//goal_diff
			tmp = get_diff(work);
			info[from_team].goal_diff += tmp;
			info[to_team].goal_diff -= tmp;

			//goal総数
			info[from_team].sum_goal += work[0]-'0';
			info[to_team].sum_goal += work[2]-'0';
		}
	}

	double ans = 0;
	double tmp_poss;

	vector<State> Top_Group;

	int tmp_from,tmp_to;
	int pre_size,num_decided,tmp_index,num_rank_0;

	bool check[5];
	bool FLG;

	//未判明の、2試合の得点を全探索
	for(int from_1 = 0; from_1 <= 8;from_1++){
		for(int to_1 = 0; to_1 <= 8; to_1++){
			for(int from_2 = 0; from_2 <= 8; from_2++){
				for(int to_2 = 0; to_2 <= 8; to_2++){

					Top_Group.clear();
					tmp_poss = 1.0;

					//まず、このトーナメント表が出来上がる確率を求める
					tmp_poss *= poss_point[from_1];
					tmp_poss *= poss_point[to_1];
					tmp_poss *= poss_point[from_2];
					tmp_poss *= poss_point[to_2];

					member.clear();
					for(int i = 1; i <= 4; i++){
						member.push_back(i);
					}

					//★表を完成させる★

					for(int loop = 0; loop < 2; loop++){

						from_team = data[loop].from_team;
						to_team = data[loop].to_team;

						if(loop == 0){

							tmp_from = from_1;
							tmp_to = to_1;

						}else{

							tmp_from = from_2;
							tmp_to = to_2;
						}

						//★テーブルを埋める★
						table[from_team][4+5*(to_team-1)+2] = '0'+tmp_from;
						table[from_team][4+5*(to_team-1)+3] = '-';
						table[from_team][4+5*(to_team-1)+4] = '0'+tmp_to;
					}

					num_decided = 0;
					//上位チームを選んでみる(1回目)
					Top_Group = select(member);

					FLG = false;

					num_rank_0 = 0;
					for(int k = 0; k < Top_Group.size(); k++){

						if(Top_Group[k].rank == 0)num_rank_0++;

						if(base_team == Top_Group[k].team_id){
							tmp_index = k;
							FLG = true;
						}
					}

					if(Top_Group.size() == 2){

						//上位2チームに入っていればOK、そうでなければ不可
						if(FLG){
							ans += tmp_poss;
						}else{
							//Do nothing
						}
						continue;

					}else if(Top_Group.size() == 3){

						//★★上位3チームに含まれていなければ不可★★
						if(!FLG)continue;

						//★単独1位ならOK★
						if(Top_Group[tmp_index].rank == 0 && num_rank_0 == 1){
							ans += tmp_poss;
							continue;
						}

					}else{ //Top_Group.size() == 4

						//★単独1位ならOK★
						if(Top_Group[tmp_index].rank == 0 && num_rank_0 == 1){
							ans += tmp_poss;
							continue;
						}
					}

					//通過or脱落が確定した人物を取り除く
					if(num_rank_0 == (int)Top_Group.size()){ //1位が3個か4個

						if(num_rank_0 == 3){ //1人脱落

							for(int k = 1; k <= 4; k++)check[k] = false;

							for(int i = 0; i < Top_Group.size(); i++){
								check[Top_Group[i].team_id] = true;
							}
							member.clear();

							for(int k = 1; k <= 4; k++){
								if(check[k]){
									member.push_back(k);
								}
							}

						}else{ //1位が4人
							//Do nothing
						}

					}else if(num_rank_0 != (int)Top_Group.size()){ //num_rank_0が2以上なら、1ターン目で返る==Top_Groupのサイズと一致する。よって、num_rank_0 == 1

							for(int k = 1; k <= 4; k++)check[k] = false;

							for(int i = 0; i < Top_Group.size(); i++){
								check[Top_Group[i].team_id] = true;
							}
							member.clear();

							for(int k = 0; k < Top_Group.size(); k++){
								if(Top_Group[k].rank != 0)continue;

								num_decided++;
								check[Top_Group[k].team_id] = false; //通過が確定したので集合から除く
							}

							for(int k = 1; k <= 4; k++){
								if(check[k]){
									member.push_back(k);
								}
							}
					}

					int debug = (int)member.size();
					pre_size = (int)member.size();

					//★★2位通過が決定するまで、ループ★★
					while(true){

						Top_Group = select(member);

						FLG = false;
						num_rank_0 = 0;
						for(int k = 0; k < Top_Group.size(); k++){

							if(Top_Group[k].rank == 0)num_rank_0++;

							if(base_team == Top_Group[k].team_id){
								tmp_index = k;
								FLG = true;
							}
						}

						if(num_decided+(int)Top_Group.size() == 2){ //上位2チーム決定

							if(FLG){ //上位2チームに含まれれば良い

								ans += tmp_poss;

							}else{
								//Do nothing
							}

							break;

						}else if(num_decided+num_rank_0 == 2){ //1位のチームのみ抜き出せば、上位2チーム決定

							if(FLG == true && Top_Group[tmp_index].rank == 0){ //1位

								ans += tmp_poss;

							}else{ //★他のチームによって枠が取られた場合★

								//Do nothing
							}

							break;
						}


						//通過or脱落が確定した人物を取り除く
						if(num_rank_0 == (int)Top_Group.size()){ //1位が2～4個

							if(num_rank_0 <= 1){
								printf("Screw\n");
							}

							if(num_rank_0 <= 3){ //1人～2人脱落

								for(int k = 1; k <= 4; k++)check[k] = false;

								for(int i = 0; i < Top_Group.size(); i++){
									check[Top_Group[i].team_id] = true;
								}
								member.clear();

								for(int k = 1; k <= 4; k++){
									if(check[k]){
										member.push_back(k);
									}
								}

							}else{ //1位が4人
								//Do nothing
							}

						}else if(num_rank_0 != Top_Group.size()){

								if(num_rank_0 >= 2){
									printf("ABAB498\n");
								}

								if(FLG == true && Top_Group[tmp_index].rank == 0){ //単独1位
									ans += tmp_poss;
									break;
								}

								for(int k = 1; k <= 4; k++)check[k] = false;

								for(int i = 0; i < member.size(); i++){
									check[member[i]] = true;
								}
								member.clear();

								for(int k = 0; k < Top_Group.size(); k++){
									if(Top_Group[k].rank != 0)continue;

									num_decided++;
									check[Top_Group[k].team_id] = false; //通過が確定したので集合から除く
								}

								for(int k = 1; k <= 4; k++){
									if(check[k]){
										member.push_back(k);
									}
								}
						}


						if((int)member.size() == pre_size){

							if(FLG){

								if(num_decided == 1){

									ans += tmp_poss/(double)member.size();

								}else{

									ans += tmp_poss*2.0/(double)member.size();
								}

							}else{

								//Do nothing
							}

							break;
						}

						pre_size = (int)member.size();
					}

				}
			}
		}
	}

	printf("%.10lf\n",ans);

}

void calc_poss(){

	int nCm[9][9];

	for(int i = 0; i <= 8; i++){
		for(int k = 0; k <= 8; k++)nCm[i][k]= 0;
	}

	nCm[0][0] = 1;
	for(int n = 1; n <= 8; n++) {
		for (int k = 0; k <= 8; k++) {
			if(k > 0){
				nCm[n][k] = nCm[n-1][k]+nCm[n-1][k-1];
			}else{ //k == 0
				nCm[n][k] = 1;
			}
		}
	}

	double poss;

	for(int point = 0; point <= 8; point++){

		poss = nCm[8][point];

		for(int i = 0; i < point; i++){
			poss *= 0.25;
		}
		for(int i = 0; i < 8-point; i++){
			poss *= 0.75;
		}
		poss_point[point] = poss;
	}
}

int main(){

	calc_poss();
	//return 0;

	scanf("%d",&num_case);

	for(int loop = 0; loop < num_case; loop++){
		func();
	}

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3112
final_rank: 1

code_snippet: #include <bits/stdc++.h>
#define double long double
#define int long long
using namespace std;

typedef pair<int,int> P;
typedef pair<string,string> P1;
typedef pair<int,string> P2;
typedef pair<P,P2> P3;

int fac(int n){
  int res = 1;
  for(int i=1;i<=n;i++) res *= i;
  return res;
}

double get(int i){
  return fac(8) * pow(1.0/4,i) * pow(3.0/4,8-i) / ( fac(i) * fac( 8 - i ) );
}

string s[5];
string target;
double ans;

vector<P3> calcOrder(set<string> teams){
  
  map<string,int> points, diff, ggoals;
  
  for(int i=0;i<5;i++){
    
    for(int j=0;j<(int)s[i].size();j++){
      
      if( s[i][j] != '-' ) continue;
      
      string t1 = s[i].substr( 1, 3 );
      string t2 = s[0].substr( j - 1, 3 );
      
      if( teams.count(t1) == 0 ) continue;
      if( teams.count(t2) == 0 ) continue;
      
      int t1p = s[i][j-1] - '0';
      int t2p = s[i][j+1] - '0';
      
      if( t1p > t2p ) points[t1] += 3;
      if( t1p == t2p ) points[t1]++, points[t2]++;
      if( t1p < t2p ) points[t2] += 3;
      
      diff[t1] += t1p - t2p;
      diff[t2] += t2p - t1p;
      
      ggoals[t1] += t1p;
      ggoals[t2] += t2p;
      
    }
    
  }
  
  vector<P3> res;
  
  for(string team : teams){
    res.push_back(P3(P(points[team],diff[team]),P2(ggoals[team],team)));
  }
  
  sort( res.begin(), res.end(), greater<P3>() );
  
  return res;
}

void cal(double p){
  
  set<string> teams;
  
  for(int i=0;i<5;i++){
    
    for(int j=0;j<(int)s[i].size();j++){
      
      if( s[i][j] != '-' ) continue;
      
      string t1 = s[i].substr( 1, 3 );
      string t2 = s[0].substr( j - 1, 3 );
      
      teams.insert(t1);
      teams.insert(t2);
      
    }
    
  }
    
  vector<P3> order = calcOrder( teams );
  
  int cnt = 5, add = 0, L, R;
  
  while(cnt--){
  
    int A, B, C;
  
    for(int i=0;i<(int)order.size();i++){
    
      if( order[i].second.second == target ){
	A = order[i].first.first;
	B = order[i].first.second;
	C = order[i].second.first;
      }
    
    }
    
    int l = 4, r = 0;
      
    for(int i=0;i<(int)order.size();i++){
    
      if( A == order[i].first.first && B == order[i].first.second && C == order[i].second.first ){
      
	l = min( l, i );
	r = max( r, i );
      
      }
    
    }
    
    teams.clear();
    
    for(int i=l;i<=r;i++) teams.insert( order[i].second.second );
    
    add += l;
    
    L = l, R = r;
    
    order = calcOrder( teams );
    
  }
  
  if( L == R ){
    
    if( add <= 1 ) ans += p;
    
  }else{
    
    if( add <= 1 ){
    
      double chance = 1 - add + 1;
      double all = R - L + 1;
    
      ans += p * chance / all;
    
    }
    
  }
  
}

vector<P> YX;

signed main(){
  
  int T;
  cin>>T;
  
  while(T--){
    
    YX.clear();
    ans = 0;
    
    for(int i=0;i<5;i++) cin>>s[i];
  
    for(int i=0;i<(int)s[0].size();i++){
      if( s[0][i] == '*' ) target = s[0].substr( i + 1, 3 );
    }
  
    for(int i=0;i<5;i++){
      for(int j=0;j<(int)s[i].size();j++){
	if( s[i][j] == '-' && s[i][j-1] == '_' ) YX.push_back(P(i,j-1));

	if( s[i][j] == '-' && s[i][j+1] == '_' ) YX.push_back(P(i,j+1));
      }
    }
  
    for(int i=0;i<=8;i++){
      for(int j=0;j<=8;j++){
	for(int k=0;k<=8;k++){
	  for(int l=0;l<=8;l++){
	    
	    s[YX[0].first][YX[0].second] = i + '0';
	    s[YX[1].first][YX[1].second] = j + '0';
	    s[YX[2].first][YX[2].second] = k + '0';
	    s[YX[3].first][YX[3].second] = l + '0';
	    
	    cal( get(i) * get(j) * get(k) * get(l) );
	    
	  }
	}
      }
    }
  
    printf("%.7Lf\n", ans);

  }
  
  return 0;
}
accuracy: 1
time_ms: 5180
memory_kb: 3472
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;
typedef pair<P,P>P2;
#define F first
#define S second
int a[4][4],kati[4],goal[4],goaled[4],gdiff[4],id;
string s[5];
void Perse(){
	memset(a,0,sizeof(a));
	a[0][1]=s[1][11]-'0';
	a[0][2]=s[1][16]-'0';
	a[0][3]=s[1][21]-'0';
	a[1][0]=s[1][13]-'0';
	a[1][2]=s[2][16]-'0';
	a[1][3]=s[2][21]-'0';
	a[2][0]=s[1][18]-'0';
	a[2][1]=s[2][18]-'0';
	a[2][3]=s[3][21]-'0';
	a[3][0]=s[1][23]-'0';
	a[3][1]=s[2][23]-'0';
	a[3][2]=s[3][23]-'0';
	r(i,5)if(s[i][0]=='*')id=i-1;
}
vector<P>use;
void FILL(vector<char> v){
	int c=0;
	if(s[1][11]=='_')s[1][11]=v[c++],use.push_back(P(1,11));
	if(s[1][16]=='_')s[1][16]=v[c++],use.push_back(P(1,16));
	if(s[1][21]=='_')s[1][21]=v[c++],use.push_back(P(1,21));
	if(s[1][13]=='_')s[1][13]=v[c++],use.push_back(P(1,13));
	if(s[2][16]=='_')s[2][16]=v[c++],use.push_back(P(2,16));
	if(s[2][21]=='_')s[2][21]=v[c++],use.push_back(P(2,21));
	if(s[1][18]=='_')s[1][18]=v[c++],use.push_back(P(1,18));
	if(s[2][18]=='_')s[2][18]=v[c++],use.push_back(P(2,18));
	if(s[3][21]=='_')s[3][21]=v[c++],use.push_back(P(3,21));
	if(s[1][23]=='_')s[1][23]=v[c++],use.push_back(P(1,23));
	if(s[2][23]=='_')s[2][23]=v[c++],use.push_back(P(2,23));
	if(s[3][23]=='_')s[3][23]=v[c++],use.push_back(P(3,23));
}

void calc(vector<int> v){
	memset(kati,0,sizeof(kati));
	memset(goal,0,sizeof(goal));
	memset(gdiff,0,sizeof(gdiff));
	memset(goaled,0,sizeof(goaled));
	int c[4]={};
	r(i,v.size())c[v[i]]=1;
	r(i,4)if(c[i]){
		r(j,4)if(c[j])if(i!=j){
			if(a[i][j]>a[j][i])kati[i]+=3;
			else if(a[i][j]==a[j][i])kati[i]+=1;
			goal[i]+=a[i][j];
			goaled[i]+=a[j][i];
		}
		gdiff[i]=goal[i]-goaled[i];
	}
}

void END(){
	exit(0);
}

double get_score(){
	Perse();
	vector<int>v;
	int ue=0;
	r(i,4)v.push_back(i);
	int CC=10;
	while(CC--){
		calc(v);
		vector<P2>sco;
		r(i,v.size()){
			sco.push_back(P2(P(kati[v[i]],gdiff[v[i]]),P(goal[v[i]],v[i])));
		}
		sort(sco.begin(),sco.end(),greater<P2>());
		int cnt=0;
		vector<int>vip[4];
		vip[0].push_back(sco[0].S.S);
		r(i,sco.size()-1){
			if(sco[i].F.F==sco[i+1].F.F&&
				sco[i].F.S==sco[i+1].F.S&&
					sco[i].S.F==sco[i+1].S.F){
						vip[cnt].push_back(sco[i+1].S.S);
					}
			else{
				vip[++cnt].push_back(sco[i+1].S.S);
			}
		}
		v.clear();
		r(i,4){
			r(j,vip[i].size()){
				if(vip[i][j]==id){
					if(i==0&&vip[i].size()==1){
						if(ue<=1)return 1;
						else return 0;
					}
					r(k,vip[i].size()){
						v.push_back(vip[i][k]);
					}
					goto L;
				}
			}
			ue+=vip[i].size();
		}
		L:;
	}
	return (2.0-ue)/v.size();
}
double kai[10];
int main(){
	int test;
	cin>>test;
	kai[0]=kai[1]=1;
	for(int i=2;i<10;i++) kai[i]=kai[i-1]*i;
	while(test--){
		double ans=0;
		r(i,5)cin>>s[i];
		r(i,9)r(j,9)r(k,9)r(l,9){
			double d1=(kai[8]/(kai[i]*kai[8-i]))*pow(0.25,i)*pow(0.75,8-i);
			double d2=(kai[8]/(kai[j]*kai[8-j]))*pow(0.25,j)*pow(0.75,8-j);
			double d3=(kai[8]/(kai[k]*kai[8-k]))*pow(0.25,k)*pow(0.75,8-k);
			double d4=(kai[8]/(kai[l]*kai[8-l]))*pow(0.25,l)*pow(0.75,8-l);
			vector<char>v;
			v.push_back(char(i+'0'));
			v.push_back(char(j+'0'));
			v.push_back(char(k+'0'));
			v.push_back(char(l+'0'));
			FILL(v);
			ans+=d1*d2*d3*d4*get_score();
			r(m,4){
				s[use[m].F][use[m].S]='_';
			}
			use.clear();
		}
		printf("%.9f\n",ans);
	}
}
accuracy: 1
time_ms: 390
memory_kb: 3276
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

using pii = pair<int, int>;

struct team {
    int id;
    int goal, score, goal_diff;

    team() : id(-1), goal(0), score(0), goal_diff(0) {}

    bool operator<(const team& t) const {
        if(score != t.score) return score < t.score;
        if(goal_diff != t.goal_diff) return goal_diff < t.goal_diff;
        return goal < t.goal;
    }
    bool operator==(const team& t) const {
        return score == t.score && goal_diff == t.goal_diff && goal == t.goal;
    }
};

int main() {
    cout << fixed << setprecision(7);

    vector<double> possibility(9);
    vector<double> fact(9, 1);
    for(int i = 2; i <= 8; ++i) {
        fact[i] = fact[i - 1] * i;
    }
    for(int i = 0; i <= 8; ++i) {
        possibility[i] = fact[8] / fact[i] / fact[8 - i] * pow(0.25, i) * pow(0.75, 8 - i);
    }

    int T;
    cin >> T;
    while(T--) {
        int target = 0;
        vector<team> teams(4);
        vector<vector<int>> goal(4, vector<int>(4));
        for(int i = 0; i < 4; ++i) {
            teams[i].id = i;
        }

        auto calc_scores = [](team& t1, team& t2, int g1, int g2) {
            t1.goal += g1;
            t1.goal_diff += g1 - g2;
            t2.goal += g2;
            t2.goal_diff += g2 - g1;
            if(g1 > g2) {
                t1.score += 3;
            } else if(g2 > g1) {
                t2.score += 3;
            } else {
                t1.score += 1;
                t2.score += 1;
            }
        };

        vector<pii> not_yet;
        for(int i = 0; i < 5; ++i) {
            string s;
            cin >> s;
            if(s[0] == '*') {
                target = i - 1;
            }
            if(i == 0) continue;
            for(int j = 6 + i * 5; j <= 21; j += 5) {
                int a = i - 1, b = (j - 6) / 5;
                if(s[j] == '_') {
                    not_yet.emplace_back(a, b);
                    continue;
                }
                calc_scores(teams[a], teams[b], s[j] - '0', s[j + 2] - '0');
                goal[a][b] = s[j] - '0';
                goal[b][a] = s[j + 2] - '0';
            }
        }
        assert(not_yet.size() == 2);

        function<double(vector<team>, int, int)> order = [&](vector<team> teams, int rest, int prev_size) {
            const int n = teams.size();
            double res = 0;
            sort(rbegin(teams), rend(teams));
            for(int i = 0; i < n && rest > 0; ++i) {
                vector<team> concerned(1);
                concerned[0].id = teams[i].id;
                while(i + 1 < n && teams[i] == teams[i + 1]) {
                    team new_team;
                    new_team.id = teams[++i].id;
                    concerned.push_back(move(new_team));
                }
                const int m = concerned.size();

                if(m == prev_size) {
                    bool found = false;
                    for(auto& t : concerned) {
                        found |= t.id == target;
                    }

                    return found ? min((double)rest / m, 1.0) : 0.0;
                } else {
                    for(int ti = 0; ti < m; ++ti) {
                        for(int tj = ti + 1; tj < m; ++tj) {
                            const int i1 = concerned[ti].id, i2 = concerned[tj].id;
                            calc_scores(concerned[ti], concerned[tj], goal[i1][i2], goal[i2][i1]);
                        }
                    }
                    res += order(concerned, rest, m);
                }

                rest -= m;
            }

            return res;
        };

        double ans = 0;
        for(int i = 0; i <= 8; ++i) {
            for(int j = 0; j <= 8; ++j) {
                for(int k = 0; k <= 8; ++k) {
                    for(int l = 0; l <= 8; ++l) {
                        const int a = not_yet[0].first, b = not_yet[0].second;
                        const int c = not_yet[1].first, d = not_yet[1].second;
                        auto tmp_teams = teams;
                        calc_scores(tmp_teams[a], tmp_teams[b], i, j);
                        calc_scores(tmp_teams[c], tmp_teams[d], k, l);
                        goal[a][b] = i, goal[b][a] = j;
                        goal[c][d] = k, goal[d][c] = l;
                        ans += order(tmp_teams, 2, 4) * possibility[i] * possibility[j] * possibility[k] * possibility[l];
                    }
                }
            }
        }

        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 150
memory_kb: 3420
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <utility>
#include <iomanip>
using namespace std;
double fact(int n){
    if(n==0) return 1;
    return n*fact(n-1);
}

double solve(int t, int r, vector<int> tl, vector<vector<int> > &score){
    int n = tl.size();
    vector<int> gp(n, 0), gd(n, 0), gn(n, 0);
    vector<pair<pair<pair<int,int>, int>, int> > cmp(n);
    for(int i=0; i<n; i++){
        for(int j=i+1; j<n; j++){
            int ti = tl[i], tj = tl[j];
            gd[i] += score[ti][tj] -score[tj][ti];
            gd[j] += score[tj][ti] -score[ti][tj];
            gn[i] += score[ti][tj];
            gn[j] += score[tj][ti];
            if(score[ti][tj] < score[tj][ti]){
                gp[j] += 3;
            }else if(score[ti][tj] > score[tj][ti]){
                gp[i] += 3;
            }else{
                gp[i]++;
                gp[j]++;
            }
        }
    }
    for(int i=0; i<n; i++){
        cmp[i] = make_pair(make_pair(make_pair(-gp[i], -gd[i]), -gn[i]), i);
    }
    sort(cmp.begin(), cmp.end());
    vector<int> ord(n);
    for(int i=0; i<n; i++) ord[i] = cmp[i].second;

    int samecount=1;
    int start=0;
    for(int i=1; i<n; i++){
        if(gp[ord[i]]==gp[ord[0]] && gd[ord[i]]==gd[ord[0]] && gn[ord[i]]==gn[ord[0]]){
            samecount++;
        }else{
            break;
        }
    }
    if(samecount==n){
        return (double)r/n;
    }

    if(samecount==1 && r==2){
        if(tl[ord[0]]==t){
            return 1;
        }else{
            start = 1;
            samecount = 1;
            r--;
            for(int i=2; i<n; i++){
                if(gp[ord[i]]==gp[ord[1]] && gd[ord[i]]==gd[ord[1]] && gn[ord[i]]==gn[ord[1]]){
                    samecount++;
                }else{
                    break;
                }
            }
        }
    }

    for(int i=start+samecount; i<n; i++){
        if(tl[ord[i]] == t) return 0;
    }
    if(samecount==r){
        return 1;
    }
    vector<int> ntl(samecount);
    for(int i=0; i<samecount; i++){
        ntl[i] = tl[ord[start +i]];
    }
    return solve(t, r, ntl, score);
}

int main(){
    double table[9];
    for(int i=0; i<=8; i++){
        table[i] = fact(8) /(fact(i)*fact(8-i)) *pow(0.25, i) *pow(0.75, 8-i);
    }
    cout << fixed;
    cout << setprecision(7);
    
    int ds;
    cin >> ds;
    for(int r=0; r<ds; r++){
        string s;
        cin >> s;
        int t;
        vector<vector<int> > score(4, vector<int>(4, 0));
        vector<vector<int> > emp(2);
        for(int i=0; i<4; i++){
            cin >> s;
            if(s[0]=='*') t=i;
            for(int j=i+1; j<4; j++){
                int fs = 6 +5*j;
                int ss = fs +2;
                if(s[fs]=='_'){
                    emp[0].push_back(i);
                    emp[1].push_back(j);
                }else{
                    score[i][j] = s[fs]-'0';
                    score[j][i] = s[ss]-'0';
                }
            }
        }

        double ans=0;
        vector<int> tl(4);
        for(int i=0; i<4; i++) tl[i]=i;
        for(int i=0; i<=8; i++){
            for(int j=0; j<=8; j++){
                for(int k=0; k<=8; k++){
                    for(int l=0; l<=8; l++){
                        score[emp[0][0]][emp[1][0]] = i;
                        score[emp[1][0]][emp[0][0]] = j;
                        score[emp[0][1]][emp[1][1]] = k;
                        score[emp[1][1]][emp[0][1]] = l;
                        ans += table[i]*table[j]*table[k]*table[l] *solve(t, 2, tl, score);
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3212
final_rank: 5