Problem:


Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7



Sample input:
5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI


Sample output:
2

Submission status: Runtime Error

Solution code:
def factorial(n):
    if n==1 or n==0:
        return 1
    else:
        return n * factorial(n-1)

def combination(n, r):
    if n<r:
        print (0)
        exit()
    return int(factorial(n)/factorial(n-r)/factorial(r))

N = int(input())
names = []
count = [0] * 5
overlap3 = [0] * 5
overlap2 = [0] * 5

for i in range(N):
    names.append(input())
    if names[i][0] == 'M':
        count[0] += 1
    elif names[i][0] == 'A':
        count[1] += 1
    elif names[i][0] == 'R':
        count[2] += 1
    elif names[i][0] == 'C':
        count[3] += 1
    elif names[i][0] == 'H':
        count[4] += 1

if sum(count)<3:
    print (0)
    exit()

for i in range(5):
    if count[i] > 2:
        overlap3[i] = combination(count[i], 3)
    if count[i] == 2:
        overlap2[i] = combination(count[i], 2)

out = combination(sum(count),3) - sum(overlap2)*(sum(count)-2) - sum(overlap3)

"""
print ('1: ', combination(sum(count),3))
print ('2: ', sum(overlap2)*(sum(count)-2))
print ('3: ', sum(overlap3))
"""

if out > 0:
    print (out)
else:
    print (0)

Submission status: Runtime Error

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input()[0])

def qwe(y):
    m = 0
    a = 0
    r = 0
    c = 0
    h = 0
    for i in y:
        if i == 'M':
            m += 1
        elif i == 'A':
            a += 1
        elif i == 'R':
            r += 1
        elif i == 'C':
            c += 1
        elif i == 'H':
            h += 1
        else:
            pass
    q = [m,a,r,c,h]
    return q

def qwer(arr):
    count = 0
    for j in range(len(arr)):
        if arr[j] == 0:
            count += 1
            arr[j] = "D"
    arr.remove("D")
    if count == 5:
        return 0
    if count == 4:
        return 0
    if count == 3:
        return 0
    if count == 2:
        wer = arr[0] * arr[1] * arr[2]
        return wer
    if count == 1:
        asd_1 = arr[0]*arr[1]*arr[2]
        asd_2 = arr[0]*arr[1]*arr[3]
        asd_3 = arr[0]*arr[2]*arr[3]
        asd_4 = arr[1]*arr[2]*arr[3]
        asde = asd_1 + asd_2 + asd_3 + asd_4
        return asde
    if count == 0:
        asf_1 = arr[0]*arr[1]*arr[2]
        asf_2 = arr[0]*arr[1]*arr[3]
        asf_3 = arr[0]*arr[1]*arr[4]
        asf_4 = arr[0]*arr[2]*arr[3]
        asf_5 = arr[0]*arr[2]*arr[4]
        asf_6 = arr[0]*arr[3]*arr[4]
        asf_7 = arr[1]*arr[2]*arr[3]
        asf_8 = arr[1]*arr[2]*arr[4]
        asf_9 = arr[1]*arr[3]*arr[4]
        asf_10 = arr[2]*arr[3]*arr[4]
        asdf =  asf_1 + asf_2 + asf_3 + asf_4 + asf_5 + asf_6 + asf_7 + asf_8 + asf_9 + asf_10
        return asdf
v = qwe(s)
print(qwer(v))

Submission status: Runtime Error

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input()[0])

def qwe(y):
    m = 0
    a = 0
    r = 0
    c = 0
    h = 0
    for i in y:
        if i == 'M':
            m += 1
        elif i == 'A':
            a += 1
        elif i == 'R':
            r += 1
        elif i == 'C':
            c += 1
        elif i == 'H':
            h += 1
        else:
            pass
    q = [m,a,r,c,h]
    return q

def qwer(arr):
    count = 0
    for j in range(len(arr)):
        if arr[j] == 0:
            count += 1
            arr.pop(j)
    if count == 5:
        return 0
    if count == 4:
        return 0
    if count == 3:
        return 0
    if count == 2:
        wer = arr[0] * arr[1] * arr[2]
        return wer
    if count == 1:
        asd_1 = arr[0]*arr[1]*arr[2]
        asd_2 = arr[0]*arr[1]*arr[3]
        asd_3 = arr[0]*arr[2]*arr[3]
        asd_4 = arr[1]*arr[2]*arr[3]
        asde = asd_1 + asd_2 + asd_3 + asd_4
        return asde
    if count == 0:
        asf_1 = arr[0]*arr[1]*arr[2]
        asf_2 = arr[0]*arr[1]*arr[3]
        asf_3 = arr[0]*arr[1]*arr[4]
        asf_4 = arr[0]*arr[2]*arr[3]
        asf_5 = arr[0]*arr[2]*arr[4]
        asf_6 = arr[0]*arr[3]*arr[4]
        asf_7 = arr[1]*arr[2]*arr[3]
        asf_8 = arr[1]*arr[2]*arr[4]
        asf_9 = arr[1]*arr[3]*arr[4]
        asf_10 = arr[2]*arr[3]*arr[4]
        asdf =  asf_1 + asf_2 + asf_3 + asf_4 + asf_5 + asf_6 + asf_7 + asf_8 + asf_9 + asf_10
        return asdf
v = qwe(s)
print(qwer(v))

Submission status: Runtime Error

Solution code:
import math

N=int(input())
S=[]
T=[]
m=a=r=c=h=0
for i in range(N):
    S.append(list(input()))
    if S[i][0]=="M" or S[i][0]=="A" or S[i][0]=="R" or S[i][0]=="C" or S[i][0]=="H":
        T.append(S[i][0])
K=set(T)
P=[]
for i in K:
    P.append(T.count(i))
if P==[]: ans=0
else: ans=math.factorial(sum(P))//(6*math.factorial(sum(P)-3))

for i in range(len(P)):
    if P[i]>2:
        ans-=(math.factorial(P[i])//6)*(len(P)-1)
    if P[i]>1:
        ans-=(math.factorial(P[i])//2)*(len(P)-1)

print(ans)

Submission status: Runtime Error

Solution code:
import math

N=int(input())
S=[]
T=[]
m=a=r=c=h=0
for i in range(N):
    S.append(list(input()))
    if S[i][0]=="M" or S[i][0]=="A" or S[i][0]=="R" or S[i][0]=="C" or S[i][0]=="H":
        T.append(S[i][0])
K=set(T)
P=[]
for i in K:
    P.append(T.count(i))
if P==[]: ans=0
else: ans=math.factorial(sum(P))//(6*math.factorial(sum(P)-3))

for i in range(len(P)):
    if P[i]>2:
        ans-=(math.factorial(P[i])//6)*(len(P)-1)
    if P[i]>1:
        ans-=math.factorial(P[i])//2*(len(P)-1)

print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    people_count = int(input())

    person_name = list()

    for person in range(people_count):
        person_name.append(str(input()))

    initial_strings = list()

    for name in person_name:
        initial_strings.append(name[0])

    mod_initial_strings = set(initial_strings)

    count = 0

    matching_strings = ['M', 'A', 'R', 'C', 'H']
    matching_strings_count = {'M': 0, 'A': 0, 'R': 0, 'C': 0, 'H': 0}

    for string in matching_strings:
        if string in mod_initial_strings:
            count += initial_strings.count(string)

    for string in initial_strings:
        matching_strings_count[string] += 1

    if count >= 3:
        print(matching_strings_count)
    else:
        print(0)

Submission status: Runtime Error

Solution code:
n = int(input())
s = []
for i in range(n):
    s.append(input()[0])

def qwe(y):
    m = 0
    a = 0
    r = 0
    c = 0
    h = 0
    for i in y:
        if i == 'M':
            m += 1
        elif i == 'A':
            a += 1
        elif i == 'R':
            r += 1
        elif i == 'C':
            c += 1
        elif i == 'H':
            h += 1
        else:
            pass
    q = [m,a,r,c,h]
    return q

def qwer(arr):
    count = 0
    coun = 0
    for j in range(len(arr)):
        if arr[j] == 0:
            count += 1
            arr.pop(j)
        else:
            coun += 1
    if count == 5:
        return 0
    if coun == 4:
        asd_1 = arr[0]*arr[1]*arr[2]
        asd_2 = arr[0]*arr[1]*arr[3]
        asd_3 = arr[0]*arr[2]*arr[3]
        asd_4 = arr[1]*arr[2]*arr[3]
        return (asd_1 + asd_2 + asd_3 + asd_4)
    if coun == 5:
        asf_1 = arr[0]*arr[1]*arr[2]
        asf_2 = arr[0]*arr[1]*arr[3]
        asf_3 = arr[0]*arr[1]*arr[4]
        asf_4 = arr[0]*arr[2]*arr[3]
        asf_5 = arr[0]*arr[2]*arr[4]
        asf_6 = arr[0]*arr[3]*arr[4]
        asf_7 = arr[1]*arr[2]*arr[3]
        asf_8 = arr[1]*arr[2]*arr[4]
        asf_9 = arr[1]*arr[3]*arr[4]
        asf_10 = arr[2]*arr[3]*arr[4]
        return (asf_1 + asf_2 + asf_3 + asf_4 + asf_5 + asf_6 + asf_7 + asf_8 + asf_9 + asf_10)
v = qwe(s)
print(qwer(v))

Submission status: Runtime Error

Solution code:
def factorial(n):
    if n==1 or n==0:
        return 1
    else:
        return n * factorial(n-1)

def combination(n, r):
    if n<r:
        print (0)
        exit()
    return int(factorial(n)/factorial(n-r)/factorial(r))

N = int(input())
names = []
count = [0] * 5
overlap3 = [0] * 5
overlap2 = [0] * 5

for i in range(N):
    names.append(input())
    if names[i][0] == 'M':
        count[0] += 1
    elif names[i][0] == 'A':
        count[1] += 1
    elif names[i][0] == 'R':
        count[2] += 1
    elif names[i][0] == 'C':
        count[3] += 1
    elif names[i][0] == 'H':
        count[4] += 1

if sum(count)<3:
    print (0)
    exit()

for i in range(5):
    if count[i] > 2:
        overlap3[i] = combination(count[i], 3)
    if count[i] == 2:
        overlap2[i] = combination(count[i], 2)

out = combination(sum(count),3) - sum(overlap2)*(sum(count)-2) - sum(overlap3)

if out > 0:
    print (out)
else:
    print (0)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define MAX 100001
using namespace std;

int main(){
    int n;
    cin >> n;

    vector<string> A(n);
    vector<int> count(5);
    for(int i=0;i<n;i++){
        cin >> A[i];

       if(A[i][0]=='M')
            count[0]++;
        else if(A[i][0]=='A')
            count[1]++;
        else if(A[i][0]=='R')
            count[2]++;
        else if(A[i][0]=='C')
            count[3]++;
        else if(A[i][0]=='H')
            count[4]++;
    }

    int ans=0;
    for(int i=0;i<(1<<5)-1;i++){
        int a=1,b=0;
        for(int j=0;j<5;j++){
            if((i>>j)&1==1){
                a*=count[j];
                b++;
            }
        }
        if(b==3)
            ans+=a;
    }

    cout << ans << endl;

    return 0;
}

Submission status: Runtime Error

Solution code:
def factorial(n):
    if n==1 or n==0:
        return 1
    else:
        return n * factorial(n-1)

def combination(n, r):
    return int(factorial(n)/factorial(n-r)/factorial(r))

N = int(input())
names = []
count = [0] * 5
overlap3 = [0] * 5
overlap2 = [0] * 5

for i in range(N):
    names.append(input())
    if names[i][0] == 'M':
        count[0] += 1
    elif names[i][0] == 'A':
        count[1] += 1
    elif names[i][0] == 'R':
        count[2] += 1
    elif names[i][0] == 'C':
        count[3] += 1
    elif names[i][0] == 'H':
        count[4] += 1

if sum(count)<3:
    print (0)
    exit()

for i in range(5):
    if count[i] > 2:
        overlap3[i] = combination(count[i], 3)
    if count[i] == 2:
        overlap2[i] = combination(count[i], 2)

out = combination(sum(count),3) - sum(overlap2)*(sum(count)-2) - sum(overlap3)

if out > 0:
    print (out)
else:
    print (0)

Submission status: Runtime Error

Solution code:
h,w,d = map(int,input().split(" "))
board = [input().split(" ") for i in range(h)]

q = int(input())
for i in range(q):
    l,r = map(int,input().split(" "))
    n = int((r-l)/d)
    count = 0
    x = []
    y = []
    for k in range(h):
        if str(l) in board[k]:
            x.append(board[k].index(str(l)))
            y.append(k)
    for j in range(n):
        for k in range(h):
            if str(l+(j+1)*d) in board[k]:
                x.append(board[k].index(str(l+(j+1)*d)))
                y.append(k)
        count = count + abs(int(x[j+1])-int(x[j])) + abs(y[j+1]-y[j])
    print(count)

Submission status: Runtime Error

Solution code:
n = int(input().strip())
s = [input().strip() for _ in range(n)]
num_m = len([True for _s in s if _s[0] == "M"])
num_a = len([True for _s in s if _s[0] == "A"])
num_r = len([True for _s in s if _s[0] == "R"])
num_c = len([True for _s in s if _s[0] == "C"])
num_h = len([True for _s in s if _s[0] == "H"])
print(num_m * num_a * num_r + num_m * num_a * num_c + num_m * num_a * num_h + num_m * num_r * num_c
      + num_m * num_r * num_h + num_m * num_c * num_h + num_a * num_r * num_c +
      num_a * num_r * num_h + num_a * num_c * num_h + num_r * num_c * num_h

Submission status: Runtime Error

Solution code:
N = int(raw_input())
lis = []

for i in range(N):
    te = raw_input()
    if te[0] == "M" or te[0] == "A" or te[0] == "R" or te[0] == "C" or te[0] == "H":
        lis.append(te)

count = 0
for i in range(len(lis)):
    if len(lis) >= 3:
        a = lis[i][0]
        for j in range(len(lis)):
            if j<=i:
                continue
            if lis[j][0] != a and len(lis)>=3:
                b = lis[j][0]
                for k in range(len(lis)):
                    if len(lis)<3:
                        break
                    if k<=i or k<=j:
                        continue
                    if lis[k][0] != a and lis[k][0] != b:
                        c = lis[k][0]
                        count += 1
print(count)

Submission status: Runtime Error

Solution code:
import sys
n=int(input())
s=[input()[0] for i in range(n)]
m,a,r,c,h=0,0,0,0,0
for i in range(n):
    if s[i]=="M": m+=1
    elif s[i]=="A": a+=1
    elif s[i]=="R": r+=1
    elif s[i]=="C": c+=1
    elif s[i]=="H": h+=1
t=sorted([m,a,r,c,h])
if t[2]==0:
    print(0)
    sys.exit()
if t[1]==0:
    print(t[2]*t[3]*t[4])
    sys.exit()
if t[0]==0:
    t[1],t[2],t[3],t[4]=a,b,c,d
    print(a*b*c+a*b*d+a*c*d+b*c*d)
    sys.exit()
else:
    t[0],t[1],t[2],t[3],t[4]=a,b,c,d,e
    x=a*b*c+a*b*d+a*b*e+a*c*d+a*c*e+a*d*e
    y=b*c*d+b*c*e+b*d*e+c*d*e
    print(x+y)

Submission status: Runtime Error

Solution code:
import numpy
import sys
import math

N=int(input())
a = []
for i in range(N):
  a.append(input())
dic = {}
for n in a:
  if n[0] in ['M','A','R','C','H']:
    if n[0] not in dic:
      dic[n[0]] = [n]
    else:
      dic[n[0]].append(n)
if len(dic)==0:
  print(0)
  exit()

basic = int(math.factorial(len(dic))/(math.factorial(3)*math.factorial(len(dic)-3)))
c = 0
for i in dic:
  names = dic[i]
  if len(names) == 1:
    c+=1
  if len(names)>1:
    basic *= (2 * (len(names)-1))
if c>=3:
  basic-=1
print(basic)

Submission status: Runtime Error

Solution code:
import numpy
import sys
import math

N=int(input())
a = []
for i in range(N):
  a.append(input())
dic = {}
for n in a:
  if n[0] in ['M','A','R','C','H']:
    if n[0] not in dic:
      dic[n[0]] = [n]
    else:
      dic[n[0]].append(n)
print(dic)
if len(dic)==0:
  print(0)
  exit()

basic = int(math.factorial(len(dic))/(math.factorial(3)*math.factorial(len(dic)-3)))
c = 0
for i in dic:
  names = dic[i]
  if len(names) == 1:
    c+=1
  if len(names)>1:
    basic *= (2 * (len(names)-1))
if c>=3:
  basic-=1
print(basic)

Submission status: Runtime Error

Solution code:
N = int(input())

name_capital = {
    'M': 0,
    'A': 0,
    'R': 0,
    'C': 0,
    'H': 0,
}
name_list = ['M', 'A', 'R', 'C', 'H']
for _ in range(N):
    name = input()
    for word in name_list:
        if name[0] == word:
            name_capital[word] += 1
            break

nv = list(name_capital.values())
answer = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(j+1, N):
            answer += nv[i] * nv[j] * nv[k]

print(answer)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

N = input()
m,a,r,c,h = 0
for i in range(N):
	S = raw_input()
	if S[:1] == "M":
		m += 1
	elif S[:1] == "A":
		a += 1
	elif S[:1] == "R":
		r += 1
	elif S[:1] == "C":
		c += 1
	elif S[:1] == "H":
		h += 1
print m*a*r+m*a*c+m*a*c+m*a*h+m*r*c+m*r*h+m*c*h+m*c*h+a*r*c+a*r*h+a*c*h+r*c*h

Submission status: Runtime Error

Solution code:
n = int(input())
s = [None for x in range(n) ]

march = "MARCH"
march_list = [0 for x in range(5)] 

for i in range(n):
    s[i] = input()
    if s[i][0] in march:
        march_list[march.find(s[i][0])] += 1

ans = 0

for i in range(n):
    for j in range(i+1,n+1):
        for k in range(j+1,n):
            ans += march_list[i] * march_list[j] * march_list[k]
            
print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

N = input()
for i in range(N):
	S = raw_input()
	if S[:1] == "M":
		m += 1
	elif S[:1] == "A":
		a += 1
	elif S[:1] == "R":
		r += 1
	elif S[:1] == "C":
		c += 1
	elif S[:1] == "H":
		h += 1
print m*a*r+m*a*c+m*a*c+m*a*h+m*r*c+m*r*h+m*c*h+m*c*h+a*r*c+a*r*h+a*c*h+r*c*h

Submission status: Runtime Error

Solution code:
N = int(input())
S = {"M":0,"A":0,"R":0,"C":0,"H":0}
for i in range(N):
    s = input()
    if s[0] in ["M","A","R","C","H"]
        S[s[0]] += 1

print(S["M"] * S["A"] * (S["R"]+S["C"]+S["H"]) + S["M"] * S["R"] * (S["C"] + S["H"]) + S["M"] * S["C"] * S["H"] + S["A"] * S["R"] * (S["C"] + S["H"]) + S["C"] * S["A"] * S["H"] +  S["R"] * S["C"] * S["H"])

Submission status: Runtime Error

Solution code:
N=int(input())
i=0
L=[0,0,0,0,0]
while i < N:
    name=input()
    if name[0] == 'M':
        L[0] += 1
    elif name[0] == 'A':
        L[1] += 1
    elif name[0] == 'R':
        L[2] += 1
    elif name[0] == 'C':
        L[3] += 1
    elif name[0] == 'H':
        L[4] += 1
    i += 1

l=L[0]+L[1]+L[2]+L[3]+L[4]
if l < 3:
    print('0')
else:
    q = 0
    Q=[]
    j = 0
    while j < 5:
        if L[j] != 0:
            q += 1
            Q.append(L[j])
        j += 1
    if q == 3:
        p = Q[0]*Q[1]*Q[2]
    elif q == 4:
        p = Q[0]*Q[1]*Q[2] + Q[0]*Q[2]*Q[3] + Q[0]*Q[1]*Q[3] + Q[1]*Q[2]*Q[3]
    else:
        p = Q[0]*Q[1]*Q[2]*Q[3] + Q[0]*Q[1]*Q[2]*Q[4] + Q[0]*Q[1]*Q[3]*Q[4] + Q[0]*Q[2]*Q[3]*Q[4] + Q[4]*Q[1]*Q[2]*Q[3]
    print(p)

Submission status: Runtime Error

Solution code:
import itertools as it

def c(n,k):
  if k>n:
    return 0
  ret = 1
  for i in range(n, n-k, -1):
    ret *= i
  for i in range(k, 1, -1):
    ret /= i
  return ret


n = int(input())
dic = {'M':0, 'A':0, 'R':0, 'C':0, 'H':0}
for person in range(n):
  name = input()
  if name[0] in 'MARCH':
    dic[name[0]] += 1

answer = 0
for a,b,c = it.combinations('MARCH', 3):
  answer += dic[a] * dic[b] * dic[c]
print(answer)