Codeforces Round #341 (Div. 2) D. Rat Kwesh and Cheese (pow,复数

传送门

D. Rat Kwesh and Cheese
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Wet Shark asked Rat Kwesh to generate three positive real numbers xy and z, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.

Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers xy and z to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options:

  1. a1 = xyz;
  2. a2 = xzy;
  3. a3 = (xy)z;
  4. a4 = (xz)y;
  5. a5 = yxz;
  6. a6 = yzx;
  7. a7 = (yx)z;
  8. a8 = (yz)x;
  9. a9 = zxy;
  10. a10 = zyx;
  11. a11 = (zx)y;
  12. a12 = (zy)x.

Let m be the maximum of all the ai, and c be the smallest index (from 1 to 12) such that ac = m. Rat's goal is to find that c, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that ac.

Input

The only line of the input contains three space-separated real numbers xy and z (0.1 ≤ x, y, z ≤ 200.0). Each of xy and z is given with exactly one digit after the decimal point.

Output

Find the maximum value of expression among xyzxzy(xy)z(xz)yyxzyzx(yx)z(yz)xzxyzyx(zx)y(zy)x and print the corresponding expression. If there are many maximums, print the one that comes first in the list.

xyz should be outputted as x^y^z (without brackets), and (xy)z should be outputted as (x^y)^z (quotes for clarity).

Examples
input
1.1 3.4 2.5
output
z^y^x
input
2.0 2.0 2.0
output
x^y^z
input
1.9 1.8 1.7
output
(x^y)^z

给你三个数x,y,z,比较这12个式子,问你哪个式子最大

0.1 ≤ x, y, z ≤ 200.0

x^(y^z)

这个式子log一下

变成

原式 = y^z*log(x)

再log一下变成

= log(y^z*log(x))

= log(y^z) + log(log(x))

= z * log(y) + log(log(x))

 

本来这样就可以比较了

可是题目的范围是0.1

log()小数会产生负数

log负数就没意义了

所以对于log(log(x))这么写不行

double范围 -1.7*10(-308)~1.7*10(308)
long double范围 128 18-19 -1.2*10(-4932)~1.2*10(4932)

虽然他们两精度都是16位,但是200的200次方long double竟然存的下

 

所以只要一次log就好了

然后愉快的写代码吧

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
char str[12][10] =
{
    "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 x, y, z;
long double mx, t;
int pos;
void judge(int x)
{
    if(fabs(mx - t) <= 1e-6) return ;
    else if(mx < t)
    {
        pos = x;
        mx = t;
    }
}
int main()
{
    cin >> x >> y >> z;
    pos = 0;
    mx = pow(y, z)*log(x);
    t = pow(z, y)*log(x);
    judge(1);
    t = z*log(pow(x, y));
    judge(2);
    t = y*log(pow(x, z));
    judge(3);

    t = pow(x, z)*log(y);
    judge(4);
    t = pow(z, x)*log(y);
    judge(5);
    t = z*log(pow(y, x));
    judge(6);
    t = x*log(pow(y, z));
    judge(7);

    t = pow(x, y)*log(z);
    judge(8);
    t = pow(y, x)*log(z);
    judge(9);
    t = y*log(pow(z, x));
    judge(10);
    t = x*log(pow(z, y));
    judge(11);

    printf("%s\n", str[pos]);
}

其实log()一个负数是可以解的

 

 

还记得当年大明湖畔的欧拉公式吗

e = -1

 

因为e的i∏次方等于-1

所以log(-1) = i∏

 

所以负数迎刃而解

log(-2) = log(-1 * 2) = log(-1) + log(2)

 

那log(i)呢

 

根号-1等于i

所以log(i) = log( -1^(1/2) ) = 1/2 * log(-1) = 1/2 * i∏

 

那log(a + bi)

 

欧拉原公式写作

eix = cosx + isinx

#include <iostream>
#include <complex>
#include <string>
using namespace std;
bool bigger (complex<long double> a, complex<long double> b)
{
    if (imag(a) == 0 && imag(b) == 0)
    {//没有虚部
        return real(a) > real(b);//比较实部
    }
    else if (imag(a) == 0 && imag(b) != 0)
    { //有虚部的肯定小
        return true;
    }
    else if (imag(a) != 0 && imag(b) == 0)
    {
        return false;
    }
    else if (imag(a) != 0 && imag(b) != 0)
    {//都有虚部,按实部反过来比
        return real(a) < real(b);
    }
}

int main ()
{
    long double ax, ay, az;
    cin >> ax >> ay >> az;

    complex<long double> x (ax, 0.0L);
    complex<long double> y (ay, 0.0L);
    complex<long double> z (az, 0.0L);

    complex<long double> cmaz (3, 3);
    string ans = "xd";

    if (bigger(z * log(y) + log(log(x)), cmaz))
    {
        cmaz = z * log(y) + log(log(x));
        ans = "x^y^z";
    }
    if (bigger(y * log(z) + log(log(x)), cmaz))
    {
        cmaz = y * log(z) + log(log(x));
        ans = "x^z^y";
    }
    if (bigger(log(y * z) + log(log(x)), cmaz))
    {
        cmaz = log(y * z) + log(log(x));
        ans = "(x^y)^z";
    }

    if (bigger(z * log(x) + log(log(y)), cmaz))
    {
        cmaz = z * log(x) + log(log(y));
        ans = "y^x^z";
    }
    if (bigger(x * log(z) + log(log(y)), cmaz))
    {
        cmaz = x * log(z) + log(log(y));
        ans = "y^z^x";
    }
    if (bigger(log(x * z) + log(log(y)), cmaz))
    {
        cmaz = log(x * z) + log(log(y));
        ans = "(y^x)^z";
    }

    if (bigger(y * log(x) + log(log(z)), cmaz))
    {
        cmaz = y * log(x) + log(log(z));
        ans = "z^x^y";
    }
    if (bigger(x * log(y) + log(log(z)), cmaz))
    {
        cmaz = x * log(y) + log(log(z));
        ans = "z^y^x";
    }
    if (bigger(log(x * y) + log(log(z)), cmaz))
    {
        cmaz = log(x * y) + log(log(z));
        ans = "(z^x)^y";
    }

    cout << ans << endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值