Skip to content

Instantly share code, notes, and snippets.

@andreymir
Created March 28, 2015 08:36
Show Gist options
  • Select an option

  • Save andreymir/f6fd80f3403af340c3a4 to your computer and use it in GitHub Desktop.

Select an option

Save andreymir/f6fd80f3403af340c3a4 to your computer and use it in GitHub Desktop.
probability
class Solution
{
static void Main(String[] args)
{
var t = long.Parse(Console.ReadLine());
for (var tt = 0; tt < t; tt++)
{
var s = Console.ReadLine();
var k = int.Parse(s.Split()[1]);
s = Console.ReadLine();
var n = s.Length;
var arr = new long[n];
var count = 0L;
var ones = 0L;
for (var a = 0; a < n; a++)
{
if (s[a] == '1')
{
ones++;
count += ones;
if (a - k > 0)
{
count -= arr[a - k - 1];
}
}
arr[a] = ones;
}
count = count * 2 - arr[arr.Length - 1];
long nn = (long)n * n;
var gcd = Gcd(nn, count);
Console.Write(count / gcd);
Console.Write('/');
Console.WriteLine(nn / gcd);
}
}
private static long Gcd(long a, long b)
{
while (b != 0)
b = a % (a = b);
return a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment