Problem:
Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports, unused_macros)]

use kyoproio::*;
use std::{
    collections::*,
    io::{self, prelude::*},
    iter,
    mem::{replace, swap},
};

fn main() -> io::Result<()> {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024)
        .spawn(solve)?
        .join()
        .unwrap();
    Ok(())
}

fn solve() {
    let stdin = io::stdin();
    let mut kin = KInput::new(stdin.lock());
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    macro_rules! output { ($($args:expr),+) => { write!(&mut out, $($args),+).unwrap(); }; }
    macro_rules! outputln {
        ($($args:expr),+) => { output!($($args),+); outputln!(); };
        () => { output!("\n"); if cfg!(debug_assertions) { out.flush().unwrap(); } }
    }

    let pi = std::f64::consts::PI;
    let (a, b, h, m): (f64, f64, f64, f64) = kin.input();
    let hz = Complex::polar(a, (h + m / 60.) / 12. * 2. * pi);
    let mz = Complex::polar(b, m / 60. * 2. * pi);
    let ans = (hz - mz).abs();
    outputln!("{}", ans);
}

#[derive(Clone, Copy, Default, PartialEq, Debug)]
pub struct Complex(pub f64, pub f64);
impl Complex {
    pub fn polar(r: f64, theta: f64) -> Self {
        Self(r * theta.cos(), r * theta.sin())
    }
    pub fn conj(self) -> Self {
        Self(self.1, self.0)
    }
    pub fn inv(self) -> Self {
        let d = self.norm();
        Self(self.0 / d, -self.1 / d)
    }
    pub fn norm(self) -> f64 {
        self.0.powi(2) + self.1.powi(2)
    }
    pub fn abs(self) -> f64 {
        self.0.hypot(self.1)
    }
    pub fn arg(self) -> f64 {
        self.1.atan2(self.0)
    }
    pub fn exp(self) -> Self {
        let (s, c) = self.1.sin_cos();
        Self(c, s).scale(self.0.exp())
    }
    pub fn powf(self, s: f64) -> Self {
        Self::polar(self.abs().powf(s), s * self.arg())
    }
    pub fn normalize(self) -> Self {
        self.scale(1. / self.abs())
    }
    pub fn scale(self, s: f64) -> Self {
        Self(s * self.0, s * self.1)
    }
    pub fn dot(self, other: Self) -> f64 {
        (self * other).0
    }
    pub fn cross(self, other: Self) -> f64 {
        (self.conj() * other).1
    }
    pub fn project_onto(self, onto: Self) -> Self {
        onto.scale(self.dot(onto) / self.norm())
    }
    pub fn reflect(self, normal: Self) -> Self {
        self.project_onto(normal).scale(2.) - self
    }
}
use std::ops::*;
impl Neg for Complex {
    type Output = Self;
    fn neg(self) -> Self {
        Self(-self.0, -self.1)
    }
}
impl Add for Complex {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        Self(self.0 + other.0, self.1 + other.1)
    }
}
impl AddAssign for Complex {
    fn add_assign(&mut self, other: Self) {
        self.0 += other.0;
        self.1 += other.1;
    }
}
impl Sub for Complex {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        Self(self.0 - other.0, self.1 - other.1)
    }
}
impl SubAssign for Complex {
    fn sub_assign(&mut self, other: Self) {
        self.0 -= other.0;
        self.1 -= other.1;
    }
}
impl Mul for Complex {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        let Self(re1, im1) = self;
        let Self(re2, im2) = other;
        Self(re1 * re2 - im1 * im2, im1 * re2 + re1 * im2)
    }
}
impl MulAssign for Complex {
    fn mul_assign(&mut self, other: Self) {
        *self = *self * other;
    }
}
impl Div for Complex {
    type Output = Self;
    fn div(self, other: Self) -> Self {
        let Self(re1, im1) = self;
        let Self(re2, im2) = other;
        Self(re1 * re2 + im1 * im2, im1 * re2 - re1 * im2).scale(other.norm())
    }
}
impl DivAssign for Complex {
    fn div_assign(&mut self, other: Self) {
        *self = *self / other;
    }
}
impl From<f64> for Complex {
    fn from(x: f64) -> Self {
        Self(x, 0.)
    }
}

// -----------------------------------------------------------------------------
pub mod kyoproio {
    use std::io::prelude::*;
    pub trait Input {
        fn str(&mut self) -> &str;
        fn input<T: InputParse>(&mut self) -> T {
            T::input(self)
        }
        fn iter<T: InputParse>(&mut self) -> Iter<T, Self> {
            Iter(self, std::marker::PhantomData)
        }
        fn seq<T: InputParse, B: std::iter::FromIterator<T>>(&mut self, n: usize) -> B {
            self.iter().take(n).collect()
        }
    }
    pub struct KInput<R> {
        src: R,
        buf: String,
        pos: usize,
    }
    impl<R: BufRead> KInput<R> {
        pub fn new(src: R) -> Self {
            Self {
                src,
                buf: String::with_capacity(1024),
                pos: 0,
            }
        }
        pub fn src(&mut self) -> &mut R {
            &mut self.src
        }
    }
    impl<R: BufRead> Input for KInput<R> {
        fn str(&mut self) -> &str {
            loop {
                if self.pos >= self.buf.len() {
                    self.pos = 0;
                    self.buf.clear();
                    if self.src.read_line(&mut self.buf).expect("io error") == 0 {
                        return &self.buf;
                    }
                }
                let range = self.pos
                    ..self.buf[self.pos..]
                        .find(|c: char| c.is_ascii_whitespace())
                        .map(|i| i + self.pos)
                        .unwrap_or_else(|| self.buf.len());
                self.pos = range.end + 1;
                if range.end > range.start {
                    return &self.buf[range];
                }
            }
        }
    }
    pub struct Iter<'a, T, I: ?Sized>(&'a mut I, std::marker::PhantomData<*const T>);
    impl<'a, T: InputParse, I: Input + ?Sized> Iterator for Iter<'a, T, I> {
        type Item = T;
        fn next(&mut self) -> Option<T> {
            Some(self.0.input())
        }
    }
    pub trait InputParse: Sized {
        fn input<I: Input + ?Sized>(src: &mut I) -> Self;
    }
    impl InputParse for Vec<u8> {
        fn input<I: Input + ?Sized>(src: &mut I) -> Self {
            src.str().as_bytes().to_owned()
        }
    }
    macro_rules! from_str_impl {
        { $($T:ty)* } => {
            $(impl InputParse for $T {
                fn input<I: Input + ?Sized>(src: &mut I) -> Self {
                    src.str().parse::<$T>().expect("parse error")
                }
            })*
        }
    }
    from_str_impl! {
        String char bool f32 f64 isize i8 i16 i32 i64 i128 usize u8 u16 u32 u64 u128
    }
    macro_rules! tuple_impl {
        ($H:ident $($T:ident)*) => {
            impl<$H: InputParse, $($T: InputParse),*> InputParse for ($H, $($T),*) {
                fn input<I: Input + ?Sized>(src: &mut I) -> Self {
                    ($H::input(src), $($T::input(src)),*)
                }
            }
            tuple_impl!($($T)*);
        };
        () => {}
    }
    tuple_impl!(A B C D E F G);
    #[macro_export]
    macro_rules! kdbg {
        ($($v:expr),*) => {
            if cfg!(debug_assertions) {
                dbg!($($v),*)
            } else {
                ($($v),*)
            }
        }
    }
}