SRM 462 DIV2

Easy::Archery

問題文を読み違えて時間かかりました。何故かradiusを直径のことだと勘違い・・・。

public class Archery
{
    public double expectedPoints(int N, int[] ringPoints)
    {
        double[] array = new double[N + 1];
        double sum = 0.0;
        double r = 0.5;
        double tmp = 0.0;
        for (int i = 0; i < N + 1; i++)
        {
            array[i] = r * r - tmp;
            sum += array[i];
            tmp = r * r;
            r += 0.5;
        }
        double ret = 0.0;
        for (int i = 0; i < N + 1; i++)
        {
            ret += (array[i] / sum) * (double)ringPoints[i];
        }
        return ret;
    }
}

Medium::AgeEncoding

バイナリ文字列とある数(age)が与えられて、どの基数だと文字列がageを表すことになるのか求める。
本番で解けなかったんで、ちょっと回答を参考にしながら解きました。

public class AgeEncoding
{
    double calc(double b, string s)
    {
        double ret = (double)Char.GetNumericValue(s[0]);
        for (int i = 1; i < s.Length; i++)
        {
            ret *= b;
            ret += (double)Char.GetNumericValue(s[i]);
        }
        return ret;
    }

    public double getRadix(int age, string candlesLine)
    {
        int sum = 0;
        foreach (char c in candlesLine)
        {
            if (c == '1')
            {
                sum++;
            }
        }

        if (sum == 0)
        {
            return -1;
        }

        if (candlesLine[candlesLine.Length - 1] == '1')
        {
            if (sum == 1)
            {
                if (age == 1)
                {
                    return -2;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (age == 1)
                {
                    return -1;
                }
            }
        }

        double a = 0.0;
        double b = 100.0;

        while (b - a > 0.00000000001)
        {
            if (calc((a + b) / 2.0, candlesLine) > (double)age)
            {
                b = (a + b) / 2.0;
            }
            else
            {
                a = (a + b) / 2.0;
            }

        }
        return a;
    }

}