QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#638856#7753. Energy DistributionShwStoneWA 457ms4104kbC++141.5kb2024-10-13 17:06:082024-10-13 17:06:08

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 10:22:30]
  • hack成功,自动添加数据
  • (/hack/1089)
  • [2024-10-13 17:06:08]
  • 评测
  • 测评结果:WA
  • 用时:457ms
  • 内存:4104kb
  • [2024-10-13 17:06:08]
  • 提交

answer

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

const int MAXN(15), MAXEPOCH(3e6);

int n;
double A[MAXN][MAXN], M[MAXN][MAXN];
double u[MAXN], v[MAXN];
double du[MAXN], dv[MAXN];

double myexp(double x) {
	if (x > 1e36) return 1e36;
	else if (x < -1e36) return 1e-36;
	return exp(x);
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			scanf("%lf", &A[i][j]);
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			M[i][j] = A[i][j];
		}
	}

	for (double rate = 1. / n; rate >= 0.01; rate /= 10) {
		for (int epoch = 0; epoch < MAXEPOCH; epoch++) {
			double sumv = 0; 
			for (int i = 1; i <= n; i++) sumv += myexp(v[i]);
			for (int i = 1; i <= n; i++) u[i] = myexp(v[i]) / sumv;
			
			for (int i = 1; i <= n; i++) {
				du[i] = 0;
				for (int j = 1; j <= n; j++) {
					du[i] += M[i][j] * u[j];
				}
			}
			for (int i = 1; i <= n; i++) {
				dv[i] = 0;
				for (int j = 1; j <= n; j++) {
					if (i == j) dv[i] += du[j] * u[j] * (1 - u[j]);
					else dv[i] -= du[j] * u[i] * u[j];
				}
			}
			for (int i = 1; i <= n; i++) v[i] += rate * dv[i];
		}
	}

	double sumv = 0; 
	for (int i = 1; i <= n; i++) sumv += exp(v[i]);
	for (int i = 1; i <= n; i++) u[i] = exp(v[i]) / sumv;

	double ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			ans += u[i] * u[j] * A[i][j];
		}
	}
	printf("%.9lf\n", ans);

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 188ms
memory: 4040kb

input:

2
0 1
1 0

output:

0.250000000

result:

ok found '0.2500000', expected '0.2500000', error '0.0000000'

Test #2:

score: 0
Accepted
time: 326ms
memory: 4104kb

input:

3
0 2 1
2 0 2
1 2 0

output:

0.571428571

result:

ok found '0.5714286', expected '0.5714290', error '0.0000004'

Test #3:

score: 0
Accepted
time: 336ms
memory: 4096kb

input:

3
0 1 2
1 0 1
2 1 0

output:

0.499999848

result:

ok found '0.4999998', expected '0.5000000', error '0.0000002'

Test #4:

score: -100
Wrong Answer
time: 457ms
memory: 4084kb

input:

4
0 3 1 0
3 0 1 0
1 1 0 2
0 0 2 0

output:

0.749998788

result:

wrong answer 1st numbers differ - expected: '0.7500000', found: '0.7499988', error = '0.0000012'