Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

問題文

文字列 $S$ が与えられる。$S$ のすべてのアナグラムのうち、回文になっているものの個数を求めよ。

文字列 $X$ が $Y$ のアナグラムであるとは、$X$ が $Y$ と等しいか、$X$ の文字を並び替えたら $Y$ に等しくなることをいう。例えば文字列abcdに対して、abcdやcbdaなどはアナグラムであるが、abedやcabやabcddなどはアナグラムでない。

文字列 $X$ が回文であるとは、$X$ を逆から読んだものが $X$ 自身と等しくなることをいう。例えばabcやabは回文でなく、aやabccbaなどは回文である。

入力

入力は以下の形式に従う。

$S$

制約

$1 \leq |S| \leq 40$ ( $|S|$ は文字列 $S$ の長さ)

$S$ は小文字の英字のみを含む。

答は $2^{63}$ 未満であることが保証される。

出力

個数を1行に出力せよ。

Sample Input 1

ab

Output for the Sample Input 1

0

abのアナグラムはabとbaの二つがあるが、どちらも回文になっていない。

Sample Input 2

abba

Output for the Sample Input 2

2

abbaとbaabの二つの文字列が回文かつアナグラムになっている。



Sample input:
ab


Sample output:
0

Submission status: Wrong Answer

Solution code:
import collections
from math import *
s=input()
a=collections.Counter(s)
o=0
for x in a:
    if a[x]&1:o+=1
    if o>1:print(0);break
    a[x]-=1
else:
    b=factorial(len(s)//2)
    for x in a.values():
        b//=factorial(x//2)
    print(b)

Submission status: Time Limit Exceeded

Solution code:
import itertools
c=0;b=[]
for x in itertools.permutations(input()):
    if x not in b and x==x[::-1]:c+=1;b+=[x]
print(c)

Submission status: Accepted

Solution code:
from collections import Counter
import math

s = input()
dic = Counter(s)

n_odd = 0
for c in dic:
    if dic[c] & 1:
        n_odd += 1
        if n_odd > 1:
            print(0)
            exit()
        dic[c] -= 1

ans = math.factorial(len(s) // 2)
for num in dic.values():
    ans //= math.factorial(num // 2)

print(ans)

Submission status: Accepted

Solution code:
import collections
from math import *
s=input()
a=collections.Counter(s)
o=0
for x in a:
    if a[x]&1:
        o+=1
        if o>1:print(0);break
        a[x]-=1
else:
    b=factorial(len(s)//2)
    for x in a.values():
        b//=factorial(x//2)
    print(b)

Submission status: Accepted

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

S = raw_input()
l,s = len(S),set(list(S))
lens = len(s)
d = [S.count(i) for i in s]
if l%2 == 0:
	for i in range(lens):
		if d[i]%2 == 1:
			print 0
			break
	else:
		denomi = 1
		for i in range(lens):
			denomi *= fact(d[i]/2)
		print fact(l/2)/denomi
else:
	for i in range(lens):
		if sum([d[i]%2 for i in range(lens)]) != 1:
			print 0
			break
	else:
		denomi = 1
		for i in range(lens):
			denomi *= fact(d[i]/2) if d[i]%2 == 0 else fact((d[i]-1)/2)
		print fact((l-1)/2)/denomi

Submission status: Accepted

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

S = raw_input()
lS,ls = len(S),len(set(list(S)))
d = [S.count(i) for i in set(list(S))]
dd = sum([d[i]%2 for i in range(ls)])
if (lS%2 == 0 and dd != 0 )or (lS%2 == 1 and dd != 1): 
		print 0
else:
	denomi = 1
	for i in range(ls):
		denomi *= fact(d[i]/2)
	print fact(lS/2)/denomi

Submission status: Accepted

Solution code:
from math import factorial
S = raw_input()
lS = len(S)
d = [S.count(i) for i in set(list(S))]
dd = sum([i%2 for i in d])
if (lS%2 == 0 and dd != 0 ) or (lS%2 == 1 and dd != 1): 
		print 0
else:
	denomi = 1
	for i in d:
		denomi *= factorial(i/2)
	print factorial(lS/2)/denomi

Submission status: Accepted

Solution code:
from math import factorial as f
S = raw_input()
lS = len(S)
d = [S.count(i) for i in set(list(S))]
dd = sum([i%2 for i in d])
if (lS%2 == 0 and dd != 0 ) or (lS%2 == 1 and dd != 1): 
		print 0
else:
	dnm = 1
	for i in d:
		dnm *= f(i/2)
	print f(lS/2)/dnm

Submission status: Accepted

Solution code:
from math import factorial as f
S = raw_input()
L = len(S)
d = [S.count(i) for i in set(list(S))]
dd = sum([i%2 for i in d])
if (L%2 == 0 and dd != 0 ) or (L%2 == 1 and dd != 1): 
		print 0
else:
	ans = f(L/2)
	for i in d:
		ans /= f(i/2)
	print ans

Submission status: Runtime Error

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

S = raw_input()
l,s = len(S),set(list(S))
lens = len(s)
d = [S.count(i) for i in s]
if l%2 == 0:
	for i in range(lens):
		if d[i]%2 == 1:
			print 0
			break
	else:
		denomi = 1
		for i in range(lens):
			denomi *= fact(d[i]/2)
		print fact(l/2)/denomi
else:
	for i in range(m):
		if sum([d[i]%2 for i in range(lens)]) != 1:
			print 0
			break
	else:
		denomi = 1
		for i in range(lens):
			denomi *= fact(d[i]/2) if d[i]%2 == 0 else fact((d[i]-1)/2)
		print fact((l-1)/2)/denomi