题目
题目链接:http://codeforces.com/problemset/problem/621/D
题目来源:cf#341
简要题意:给定公式求值最大中下标最小的公式。
题解
这题需要对于浮点数有比较高深的理解。首先复习一下浮点数的范围。
类型 | 位数 | 有效数字 | 数值范围 |
---|---|---|---|
float | 32 | 6-7 | ±3.4×1038 |
double | 64 | 15-16 | ±1.7×10308 |
long double | 128 | 18-19 | ±1.2×104932 |
考虑取一个对数的话,值就算用double也是不够的,要用long double。
然后将对应的公式取对数就行了。取pow的时候需要使用powl,这是long double版本的pow函数。
由于考察的知识比较偏门,所以说这道D过的人比E还要少,不过A了它的确可以学到新姿势。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
char out[12][15] = {
"x^y^z",
"x^z^y",
"(x^y)^z",
"(x^z)^y",
"y^x^z",
"y^z^x",
"(y^x)^z",
"(y^z)^x",
"z^x^y",
"z^y^x",
"(z^x)^y",
"(z^y)^x"
};
long double a[15];
int main() {
double x, y, z;
scanf("%lf%lf%lf", &x, &y, &z);
long double logx = log(x);
long double logy = log(y);
long double logz = log(z);
a[0] = logx * powl(y, z);
a[1] = logx * powl(z, y);
a[2] = logx * y * z;
a[3] = -1;
a[4] = logy * powl(x, z);
a[5] = logy * powl(z, x);
a[6] = logy * x * z;
a[7] = -1;
a[8] = logz * powl(x, y);
a[9] = logz * powl(y, x);
a[10] = logz * x * y;
a[11] = -1;
long double ans = a[0];
int ind = 0;
for (int i = 1; i < 12; i++) {
if (!fabs(ans-a[i]) < 1e-8 && a[i] > ans) {
ans = a[i];
ind = i;
}
}
printf("%s\n", out[ind]);
return 0;
}