codeforces 621 D. Rat Kwesh and Cheese(log处理)

Description

Wet Shark asked Rat Kwesh to generate three positive real numbers x, y 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 x, y and z to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options:
这里写图片描述
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 x, y and z (0.1 ≤ x, y, z ≤ 200.0). Each of x, y and z is given with exactly one digit after the decimal point.

Output

Find the maximum value of expression among xyz, xzy, (xy)z, (xz)y, yxz, yzx, (yx)z, (yz)x, zxy, zyx, (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种方法求得的最大值,如果有多个相同的,输出编号最小的一个的表达式。

解题思路

思路1
对表达式取一次log,精度在long double 范围内可以保证,直接比较结果。
思路2
对表达式取两次log,
当x,y,z均小于1 时,如 xyz x y z log(xyz) l o g ( x y z ) = yzlog(x) y z l o g ( x ) ,此时log(x)将会小于0;为了解决这种情况可以取倒数求值,即 xyz x y z =( 11xyz 1 1 x ) y z = 1(1x)yz 1 ( 1 x ) y z ,要求最大值,只需求 (1x)yz ( 1 x ) y z 的最小值,取两次log为 zlog(y)+log(log(x)) z l o g ( y ) + l o g ( − l o g ( x ) ) ,依次类推比较分母的最小值;
当x,y,z至少有1个大于1,小于1 的对应取值一定小于大于1的对应取值,所以直接比较大于1 的对应取值即可,如若只有x>1,则只需比较a1,a2,a3,a4的值。

代码实现

思路1

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
char str[13][13]={
    "",
    "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"
};
ld num[13];
int main()
{
    IO;
    ld x,y,z;
    cin>>x>>y>>z;
    num[1]=log(x)*pow(y,z);
    num[2]=log(x)*pow(z,y);
    num[3]=log(x)*(y*z);
    num[5]=log(y)*pow(x,z);
    num[6]=log(y)*pow(z,x);
    num[7]=log(y)*(x*z);
    num[9]=log(z)*pow(x,y);
    num[10]=log(z)*pow(y,x);
    num[11]=log(z)*(x*y);
    ld ans;
    int mark;
    for(int i=1;i<12;i++)
    {
        if(i==4||i==8) continue;
        if(i==1||ans<num[i])
        {
            ans=num[i];
            mark=i;
        }
    }
    cout<<str[mark]<<endl;
    return 0;
}

思路2

#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
typedef long double ld;
char str[13][13]={
    "",
    "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"
};
ld num[13];
int main()
{
    IO;
    ld x,y,z;
    cin>>x>>y>>z;
    if(x<1&&y<1&&z<1)
    {
        num[1]=z*log(y)+log(-log(x));
        num[2]=y*log(z)+log(-log(x));
        num[3]=log(y*z)+log(-log(x));
        num[5]=z*log(x)+log(-log(y));
        num[6]=x*log(z)+log(-log(y));
        num[7]=log(x*z)+log(-log(y));
        num[9]=y*log(x)+log(-log(z));
        num[10]=x*log(y)+log(-log(z));
        num[11]=log(x*y)+log(-log(z));
        ld ans;
        int mark;
        for(int i=1;i<12;i++)
        {
            if(i==4||i==8) continue;
            if(i==1||ans>num[i])
            {
                ans=num[i];
                mark=i;
            }
        }
        cout<<str[mark]<<endl;
    }
    else
    {
        if(x<1) num[1]=num[2]=num[3]=-1;
        if(y<1) num[5]=num[6]=num[7]=-1;
        if(z<1) num[9]=num[10]=num[11]=-1;
        if(num[1]!=-1) num[1]=z*log(y)+log(log(x));
        if(num[2]!=-1) num[2]=y*log(z)+log(log(x));
        if(num[3]!=-1) num[3]=log(y*z)+log(log(x));
        if(num[5]!=-1) num[5]=z*log(x)+log(log(y));
        if(num[6]!=-1) num[6]=x*log(z)+log(log(y));
        if(num[7]!=-1) num[7]=log(x*z)+log(log(y));
        if(num[9]!=-1) num[9]=y*log(x)+log(log(z));
        if(num[10]!=-1) num[10]=x*log(y)+log(log(z));
        if(num[12]!=-1) num[11]=log(x*y)+log(log(z));
        ld ans;
        int mark=0;
        for(int i=1;i<12;i++)
        {
            if(i==4||i==8) continue;
            if(num[i]!=-1&&(mark==0||ans<num[i]))
            {
                ans=num[i];
                mark=i;
            }
        }
        cout<<str[mark]<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值