#include <bits/stdc++.h>
using namespace std;
const int N = 2 ;
struct matrix
{
double x[ N+ 1 ] [ N+ 1 ] ;
matrix( ) { memset ( x, 0 , sizeof ( x) ) ; }
matrix( double init)
{
memset ( x, 0 , sizeof ( x) ) ;
for ( int i = 1 ; i <= N; i++ )
x[ i] [ i] = init;
}
matrix operator * ( matrix that)
{
matrix ret;
for ( int i = 1 ; i <= N; i++ )
for ( int j = 1 ; j <= N; j++ )
for ( int k = 1 ; k <= N; k++ )
ret.x [ i] [ j] = ( ret.x [ i] [ j] + x[ i] [ k] * that.x [ k] [ j] ) ;
return ret;
}
} I( 1 ) ;
matrix power( matrix b, long long e)
{
matrix ret = I;
while ( e)
{
if ( e& 1 ) ret = ret * b;
b = b * b;
e / = 2 ;
}
return ret;
}
double p;
double prob( int n, int k)
{
if ( k > n) return 1 ;
if ( k < 1 ) return 0 ;
matrix trans, init;
trans.x [ 1 ] [ 1 ] = 1 / p; trans.x [ 1 ] [ 2 ] = ( p- 1 ) / p; trans.x [ 2 ] [ 1 ] = 1 ;
init.x [ 1 ] [ 1 ] = 1 ; init.x [ 2 ] [ 1 ] = 0 ;
init.x [ 1 ] [ 1 ] = 1 / ( power( trans, n) * init) .x [ 1 ] [ 1 ] ;
return ( power( trans, k- 1 ) * init) .x [ 1 ] [ 1 ] ;
}
class CatsOnTheCircle
{
public : double getProb( int N, int K, int _p)
{
p = _p / 1000000000.0 ;
if ( p < 0.5 )
{
p = 1 - p;
K = N - K;
}
double probR = prob( N- 3 , N- K- 1 ) ;
double probL = 1 - probR;
if ( probR < 0 || probR > 1 ) // INF
return 0 ;
return probR * ( 1 - prob( N- 2 , N- 2 ) ) + probL * prob( N- 2 , 1 ) ;
}
} ;
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDI7CgpzdHJ1Y3QgbWF0cml4CnsKCWRvdWJsZSB4W04rMV1bTisxXTsKCW1hdHJpe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