A. Dr. TC
Source: codeforces
Tags: brute force, math

In order to test his patients' intelligence, Dr. TC created the following test.
First, he creates a binary string$^{\text{∗}}$ $s$ having $n$ characters. Then, he creates $n$ binary strings $a_1, a_2, \ldots, a_n$. It is known that $a_i$ is created by first copying $s$, then flipping the $i$'th character ($\texttt{1}$ becomes $\texttt{0}$ and vice versa). After creating all $n$ strings, he arranges them into a grid where the $i$'th row is $a_i$.
For example,
• If
$s = \texttt{101}$
,
$a = [\texttt{001}, \texttt{111}, \texttt{100}]$
.
• If
$s = \texttt{0000}$
,
$a = [\texttt{1000}, \texttt{0100}, \texttt{0010}, \texttt{0001}]$
.
The patient needs to count the number of $1$s written on the board in less than a second. Can you pass the test?
$^{\text{∗}}$A binary string is a string that only consists of characters $\texttt{1}$ and $\texttt{0}$.

-----Input-----
The first line of the input consists of a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 10$) — the length of the binary string $s$.
The second line of each test case contains a single binary string $s$ of size $n$.

-----Output-----
For each test case, output a single integer, the number of $\texttt{1}$s on the board.

-----Examples-----
Input:
5
3
101
1
1
5
00000
2
11
3
010
Output:
5
0
5
2
4

-----Note-----
The first example is explained in the statement.
For the second example, the only string written on the board will be the string $\texttt{0}$; therefore, the answer is $0$.
In the third example, the following strings will be written on the board: $[\texttt{10000}, \texttt{01000}, \texttt{00100}, \texttt{00010}, \texttt{00001}]$; so there are five $\texttt{1}$s written on the board.

C++ solution:
#include <iostream>
#include <string>

using namespace std;

int main() {
    int t;
    cin >> t;
    
    while (t--) {
        int n;
        cin >> n;
        
        string s;
        cin >> s;
        
        // Validate input length matches n
        if (s.length() != n) {
            cerr << "Error: String length doesn't match n" << endl;
            return 1;
        }
        
        int ones = 0;
        for (char c : s) {
            if (c == '1') {
                ones++;
            } else if (c != '0') {
                cerr << "Error: Invalid character in binary string" << endl;
                return 1;
            }
        }
        
        int zeros = n - ones;
        int total_ones = ones * (ones - 1) + zeros * (ones + 1);
        
        cout << total_ones << endl;
    }
    
    return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanLines)
	
	// Read the number of test cases
	if !scanner.Scan() {
		fmt.Println("Error reading number of test cases")
		return
	}
	t, err := strconv.Atoi(scanner.Text())
	if err != nil {
		fmt.Println("Error converting number of test cases:", err)
		return
	}
	
	for i := 0; i < t; i++ {
		// Read the length of the binary string
		if !scanner.Scan() {
			fmt.Println("Error reading length of string")
			return
		}
		n, err := strconv.Atoi(scanner.Text())
		if err != nil {
			fmt.Println("Error converting length of string:", err)
			return
		}
		
		// Read the binary string
		if !scanner.Scan() {
			fmt.Println("Error reading binary string")
			return
		}
		s := scanner.Text()
		
		// Count the number of '1's and '0's
		x := strings.Count(s, "1")
		y := n - x
		
		// Calculate the total number of '1's on the board
		total := x*(x-1) + y*(x+1)
		
		// Print the result
		fmt.Println(total)
	}
}

Java solution:
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        scanner.nextLine(); // Consume the newline character after reading t
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();
            scanner.nextLine(); // Consume the newline character after reading n
            String s = scanner.nextLine();
            int x = 0;
            for (int j = 0; j < n; j++) {
                if (s.charAt(j) == '1') {
                    x++;
                }
            }
            int y = n - x;
            System.out.println(x * (x - 1) + y * (x + 1));
        }
        scanner.close();
    }
}

JavaScript solution:
const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];
let currentLine = 0;

rl.on('line', (line) => {
    input.push(line);
});

rl.on('close', () => {
    solve();
});

function solve() {
    let t = parseInt(input[0]);
    let line = 1;
    for (let i = 0; i < t; i++) {
        let n = parseInt(input[line++]);
        let s = input[line++];
        let x = 0;
        for (let c of s) {
            if (c === '1') {
                x++;
            }
        }
        let y = n - x;
        console.log(x * (x - 1) + y * (x + 1));
    }
}

Python solution:
for _ in range(int(input())):
    n=int(input())
    s=input()
    x=s.count('1')
    y=n-x
    print(x*(x-1)+y*(x+1))

Ruby solution:
input = $stdin.read.split("\n")
t = input.shift.to_i
t.times do
  n = input.shift.to_i
  s = input.shift.chomp
  x = s.count('1')
  y = n - x
  puts x * (x - 1) + y * (x + 1)
end