#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const double x264_cabac_entropy[128] = {
    0.0273, 5.7370, 0.0288, 5.6618, 0.0303, 5.5866, 0.0320, 5.5114,
    0.0337, 5.4362, 0.0355, 5.3610, 0.0375, 5.2859, 0.0395, 5.2106,
    0.0416, 5.1354, 0.0439, 5.0602, 0.0463, 4.9851, 0.0488, 4.9099,
    0.0515, 4.8347, 0.0543, 4.7595, 0.0572, 4.6843, 0.0604, 4.6091,
    0.0637, 4.5339, 0.0671, 4.4588, 0.0708, 4.3836, 0.0747, 4.3083,
    0.0788, 4.2332, 0.0832, 4.1580, 0.0878, 4.0828, 0.0926, 4.0076,
    0.0977, 3.9324, 0.1032, 3.8572, 0.1089, 3.7820, 0.1149, 3.7068,
    0.1214, 3.6316, 0.1282, 3.5565, 0.1353, 3.4813, 0.1429, 3.4061,
    0.1510, 3.3309, 0.1596, 3.2557, 0.1686, 3.1805, 0.1782, 3.1053,
    0.1884, 3.0301, 0.1992, 2.9549, 0.2107, 2.8797, 0.2229, 2.8046,
    0.2358, 2.7294, 0.2496, 2.6542, 0.2642, 2.5790, 0.2798, 2.5038,
    0.2964, 2.4286, 0.3142, 2.3534, 0.3331, 2.2782, 0.3532, 2.2030,
    0.3748, 2.1278, 0.3979, 2.0527, 0.4226, 1.9775, 0.4491, 1.9023,
    0.4776, 1.8271, 0.5082, 1.7519, 0.5412, 1.6767, 0.5768, 1.6015,
    0.6152, 1.5263, 0.6568, 1.4511, 0.7020, 1.3759, 0.7513, 1.3008,
    0.8050, 1.2256, 0.8638, 1.1504, 0.9285, 1.0752, 1.0000, 1.0000,
};

const int x264_cabac_transition[128][2] =
{
    {  0,   0}, {  1,   1}, {  2,  50}, { 51,   3}, {  2,  50}, { 51,   3}, {  4,  52}, { 53,   5},
    {  6,  52}, { 53,   7}, {  8,  52}, { 53,   9}, { 10,  54}, { 55,  11}, { 12,  54}, { 55,  13},
    { 14,  54}, { 55,  15}, { 16,  56}, { 57,  17}, { 18,  56}, { 57,  19}, { 20,  56}, { 57,  21},
    { 22,  58}, { 59,  23}, { 24,  58}, { 59,  25}, { 26,  60}, { 61,  27}, { 28,  60}, { 61,  29},
    { 30,  60}, { 61,  31}, { 32,  62}, { 63,  33}, { 34,  62}, { 63,  35}, { 36,  64}, { 65,  37},
    { 38,  66}, { 67,  39}, { 40,  66}, { 67,  41}, { 42,  66}, { 67,  43}, { 44,  68}, { 69,  45},
    { 46,  68}, { 69,  47}, { 48,  70}, { 71,  49}, { 50,  72}, { 73,  51}, { 52,  72}, { 73,  53},
    { 54,  74}, { 75,  55}, { 56,  74}, { 75,  57}, { 58,  76}, { 77,  59}, { 60,  78}, { 79,  61},
    { 62,  78}, { 79,  63}, { 64,  80}, { 81,  65}, { 66,  82}, { 83,  67}, { 68,  82}, { 83,  69},
    { 70,  84}, { 85,  71}, { 72,  84}, { 85,  73}, { 74,  88}, { 89,  75}, { 76,  88}, { 89,  77},
    { 78,  90}, { 91,  79}, { 80,  90}, { 91,  81}, { 82,  94}, { 95,  83}, { 84,  94}, { 95,  85},
    { 86,  96}, { 97,  87}, { 88,  96}, { 97,  89}, { 90, 100}, {101,  91}, { 92, 100}, {101,  93},
    { 94, 102}, {103,  95}, { 96, 104}, {105,  97}, { 98, 104}, {105,  99}, {100, 108}, {109, 101},
    {102, 108}, {109, 103}, {104, 110}, {111, 105}, {106, 112}, {113, 107}, {108, 114}, {115, 109},
    {110, 116}, {117, 111}, {112, 118}, {119, 113}, {114, 118}, {119, 115}, {116, 122}, {123, 117},
    {118, 122}, {123, 119}, {120, 124}, {125, 121}, {122, 126}, {127, 123}, {124, 127}, {126, 125}
};

double encode(double prob, int init_state)
{
    double bits = 0;
    double state[128] = {0};
    double state2[128];
    state[init_state] = 1;
    for(int i=0; i<1000; i++)
    {
        memset(state2, 0, sizeof(state2));
        for(int s=0; s<128; s++)
            for(int b=0; b<2; b++)
            {
                double p = state[s] * (b ? prob : 1-prob);
                bits += p * x264_cabac_entropy[s^b];
                state2[x264_cabac_transition[s][b]] += p;
            }
        memcpy(state, state2, sizeof(state2));
    }
    return bits;
}

int main(int argc, char **argv)
{
    if(argc != 2)
        exit(1);
    double prob = atof(argv[1]);
    double bits[128];
    double min = 1e99;
    for(int s=1; s<=126; s++)
        bits[s] = encode(prob, (s&64 ? 127-2*(s&63) : 2*(s&63)));
    for(int s=1; s<=126; s++)
        if(min > bits[s]) min = bits[s];
    for(int s=1; s<=126; s++)
        printf("%d %f\n", s, bits[s] - min);
    return 0;
}
