import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } // n * (n + 1) // modifies a real[] solveLinearEq(real[][] a) { const n = cast(int)(a.length); foreach (h; 0 .. n) { int im = h; foreach (i; h + 1 .. n) { if (abs(a[im][h]) < abs(a[i][h])) { im = i; } } swap(a[h], a[im]); a[h][] /= a[h][h]; foreach (i; h + 1 .. n) { foreach_reverse (j; h .. n + 1) { a[i][j] -= a[i][h] * a[h][j]; } } } auto x = new real[n]; foreach_reverse (i; 0 .. n) { x[i] = a[i][n]; foreach (j; i + 1 .. n) { x[i] -= a[i][j] * x[j]; } } return x; } // https://oeis.org/A225233 long f(long x, long y) { const n = x + y; return (2 * n - x) * x; } void main() { debug {{ // E[time | reach 0] foreach (n; 1 .. 10 + 1) { auto a = new real[][](n, n + 1); foreach (x; 0 .. n) { a[x][] = 0.0L; a[x][x] += 1.0L; if (x > 0) { a[x][n] = 1.0L * (n - x) / n; { a[x][x - 1] -= 0.5L; } if (x + 1 < n) { a[x][x + 1] -= 0.5L; } } } auto es = a.solveLinearEq; foreach (x; 0 .. n) { es[x] /= (1.0L * (n - x) / n); } es[] *= 3.0L; writeln(es); foreach (x; 0 .. n) { assert(abs(f(x, n - x) - es[x]) <= 1e-6L); } } }} try { for (; ; ) { const N = readInt; const S = readToken; int[] xs; foreach (i; 1 .. N) if (S[i] == '0') { xs ~= i; } const len = cast(int)(xs.length); xs = xs ~ [0] ~ xs; xs[0 .. len] -= N; debug { writeln("xs = ", xs); } long ans; foreach (j; 0 .. len + 1) { const long l = -xs[j]; const long r = xs[j + len]; // 0 -> -l -> +r // 0 -> +r -> -l /* long sum; sum += r * (N - l - r) * (f(l, r) + f(l + r, N - l - r)); sum += l * (N - l - r) * (f(r, l) + f(l + r, N - l - r)); assert(sum % (l + r) == 0); sum /= (l + r); ans += sum; */ ans += (l + r - N) * (l * l - l * r + r * r - 2 * l * N - 2 * r * N); } assert(ans % (3 * N) == 0); ans /= (3 * N); writeln(ans); } } catch (EOFException e) { } }