Problem:


覆面算

 数式の一部を隠してしまって、隠した数字を探す問題を覆面算といいます。今回は、数式の中のある数字のいくつかを X で隠してしまった式を扱います。以下の数式を入力して、結果を出力するプログラムを作成してください。

  数式

「数字列 + 数字列 = 数字列」の形で、1 行の単純な足し算式です。

 「数字列」は、数字 0 から 9 と文字 X の並びです。

 2 桁以上の「数字列」の左端の数字は 0 ではないものとします。

  X は、数式全体の中で必ず 1 つ以上含まれます。

  結果

  覆面算の答えです。数式が成り立つ様な X の値で 0 ~ 9 のどれか 1 つです。2 つ以上の答えはないものとします。

  答えがない場合、結果は“NA”としてください。

Input

複数のデータセットが与えられます。各データセットとして、X を 1 つ以上含む足し算式(空白を含まない 126 文字以内の文字列) が１行に与えられます。データセットの数は 150 を超えません。

Output

各データセットについて、覆面算の結果を１行に出力してください。数字 0 ~ 9 または NA を出力してください。

Sample Input

123+4X6=X79
12X+4X6=X79
XX22+89=X2XX

Output for the Sample Input

5
NA
1



Sample input:
123+4X6=X79
12X+4X6=X79
XX22+89=X2XX


Sample output:
5
NA
1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main():
    for line in stdin:
        first, line = line.split('+')
        second, answer = line.split('=')

        if len(first) > 1 and first[0] == 'X':
            candidate = '123456789'
        elif len(second) > 1 and second[0] == 'X':
            candidate = '123456789'
        elif len(answer) > 1 and answer[0] == 'X':
            candidate = '123456789'
        else:
            candidate = '0123456789'

        for c in candidate:
            f = int(first.replace('X', c))
            s = int(second.replace('X', c))
            a = int(answer.replace('X', c))
            if f + s == a:
                print(c)
                break
        else:
            print('NA')
    exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
while True:
	try:
		e = map(str, raw_input().split("="))
		for x in range(10):
			if eval(e[0].replace("X",str(x))) == int(e[1].replace("X",str(x))):
				print x
				break
		else:
			print "NA"
	except:
		break

Submission status: Wrong Answer

Solution code:
while True:
	try:
		a,b,c = map(str, raw_input().replace("+","=").split("="))
		i = 0 if "X" not in [a[0],b[0],c[0]] else 1
		for x in range(i,10):
			if eval((a+"+"+b).replace("X",str(x))) == int(c.replace("X",str(x))):
				print x
				break
		else:
			print "NA"
	except:
		break

Submission status: Accepted

Solution code:
import re
while True:
    try:
        s = input()
    except EOFError:
        break

    for x in '0123456789':
        t = s.replace('X', x)
        a, b, c = re.split(r'[+=]', t)

        if len(a) > 1 and a[0] == '0': continue
        if len(b) > 1 and b[0] == '0': continue
        if len(c) > 1 and c[0] == '0': continue
        a, b, c = map(int, (a, b, c))
        if a + b != c: continue

        print(x)
        break
    else:
        print('NA')

Submission status: Accepted

Solution code:
while(True):
    try:
        a,c = input().split("=")
        a,b = a.split("+")
        for i in range(10):
            if i==0 and ((len(a)>1 and a[0]=="X") or (len(b)>1 and b[0]=="X") or (len(c)>1 and c[0]=="X")):
                continue
            if int(a.replace("X",str(i)))+int(b.replace("X",str(i))) == int(c.replace("X",str(i))):
                print(i)
                break
        else:
            print("NA")
    except:
        break

Submission status: Accepted

Solution code:
#import math

def cheak(x):
    if x[0] == "0" and len(x) > 1:
        return True
    return False

def find(ss):
    for i in range(0,10):
        s = ss.replace("X",str(i))
        ab, c = list(s.split("=="))
        a, b = list(ab.split("+"))
        if i == 0 and (cheak(a) or cheak(b) or cheak(c)):
            continue
        if eval(s):
            print(i)
            return
    print("NA")

while True:
    try:
        s = input().replace("=","==")
        find(s)
    except:
        break

Submission status: Accepted

Solution code:
# Edit: 2014/09/28
# Lang: Python3
# Time: 0.xxs
#

if __name__ == "__main__":
    while True:
        try:
            st0 = input()
            # print(st0)
            st0 = st0.strip("\n").replace("+", ",").replace("=", ",")
            # print(st0)
            #strA, strB, strC = str.split(",")
            #print(strA, strB, strC)

            for i in range(10):
                #print(i,str(i))
                a, b, c = st0.replace("X", str(i)).split(",")
                if a[0] == "0" and len(a) > 1:
                    continue
                if b[0] == "0" and len(b) > 1:
                    continue
                if c[0] == "0" and len(c) > 1:
                    continue
                #print(strI)
                #pass
                if int(a) + int(b) == int(c):
                    print(i)
                    break
            else:
                print("NA")
        except:
            break

Submission status: Accepted

Solution code:
# Edit: 2014/09/28
# Lang: Python3
# Time: 0.xxs
#

if __name__ == "__main__":
    while True:
        try:
            st0 = input().strip("\n").replace("+", ",").replace("=", ",")

            for j in range(10):
                i = 9 - j
                a, b, c = st0.replace("X", str(i)).split(",")
                if i == 0:
                    if a[0] == "0" and len(a) > 1:
                        continue
                    if b[0] == "0" and len(b) > 1:
                        continue
                    if c[0] == "0" and len(c) > 1:
                        continue

                if int(a) + int(b) == int(c):
                    print(i)
                    break
            else:
                print("NA")
        except:
            break

Submission status: Accepted

Solution code:
import sys

X="X"
N="0123456789"
for s0 in sys.stdin:
  s=s0[:-1]
  i=s.find("+")
  j=s.find("=")
  x=[s[:i],s[i+1:j],s[j+1:]]
  f=0
  for e in x:
    if e[0]==X and len(e)>1: f=1; break
  a="NA"
  for c in N[f:]:
    b=s.replace(X,c)
    if eval(b[:j])==eval(b[j+1:]): a=c; break
  print a

Submission status: Accepted

Solution code:
import sys

X="X"
N="0123456789"
for s0 in sys.stdin:
  s=s0[:-1]
  j=s.find("=")
  A=[0,s.find("+")+1,j+1,len(s)+1]
  f=0
  for a,b in zip(A[:-1],A[1:]):
    if s[a]==X and b-a>2: f=1; break
  a="NA"
  for c in N[f:]:
    b=s.replace(X,c)
    if eval(b[:j])==eval(b[j+1:]): a=c; break
  print a

Submission status: Accepted

Solution code:
import sys

for s0 in sys.stdin:
  s=s0[:-1]
  i=s.find("+")
  j=s.find("=")
  A=[s[0:i],s[i+1:j],s[j+1:]]
  f=0
  for e in A:
    if e[0]=="X" and len(e)>1: f=1; break
  s="NA"
  for c in "0123456789"[f:]:
    x,y,z=map(int,[e.replace("X",c) for e in A])
    if x+y==z: s=c
  print s

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
  i=s.find("+")
  j=s.find("=")
  A=[s[0:i],s[i+1:j],s[j+1:-1]]
  f=0
  for e in A:
    if e!=e[0]=="X": f=1; break
  s="NA"
  for c in "0123456789"[f:]:
    x,y,z=map(int,[e.replace("X",c) for e in A])
    if x+y==z: s=c; break
  print s

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
  i=s.find("+")
  j=s.find("=")
  A=[s[0:i],s[i+1:j],s[j+1:-1]]
  f=0
  for e in A:
    if e!=e[0]=="X": f=1; break
  m="NA"
  for c in "0123456789"[f:]:
    if eval(s[:-1].replace("X",c).replace("=","==")): m=c; break
  print m

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
  i,j=s.find("+"),s.find("=")
  A=[s[0:i],s[i+1:j],s[j+1:-1]]
  f=0
  for e in A:
    if e!=e[0]=="X": f=1; break
  m="NA"
  s=s.replace("=","==")
  for c in "0123456789"[f:]:
    if eval(s[:-1].replace("X",c)): m=c; break
  print m

Submission status: Accepted

Solution code:
import sys

for s0 in sys.stdin:
  s=s0[:-1]
  a,b=s.split("+")
  b,c=b.split("=")
  f=any([e!=e[0]=="X" for e in [a,b,c]])

  m="NA"
  s=a+"+"+b+"=="+c
  for e in "0123456789"[f:]:
    try:
      if eval(s.replace("X",e)): m=e; break
    except: pass
  print m

Submission status: Accepted

Solution code:
import sys

for s0 in sys.stdin:
  s=s0[:-1]
  d,c=s.split("=")
  a,b=d.split("+")
  f=any([e!=e[0]=="X" for e in [a,b,c]])

  m="NA"
  s=d+"=="+c
  for e in "0123456789"[f:]:
    try:
      if eval(s.replace("X",e)): m=e; break
    except: pass
  print m

Submission status: Accepted

Solution code:
import sys

for s0 in sys.stdin:
  s=s0[:-1]
  d,c=s.split("=")
  a,b=d.split("+")
  f=any([e!=e[0]=="X" for e in [a,b,c]])
  N="0123456789"[f:]
  m="NA"
  s=d+"=="+c
  for e in N:
    if eval(s.replace("X",e)): m=e; break
  print m

Submission status: Accepted

Solution code:
import sys

for s0 in sys.stdin:
  s=s0[:-1]
  d,c=s.split("=")
  a,b=d.split("+")
  f=any([e!=e[0]=="X" for e in [a,b,c]])

  m="NA"
  s=d+"=="+c
  for e in "0123456789"[f:]:
    if eval(s.replace("X",e)): m=e; break
  print m

Submission status: Accepted

Solution code:
import re

def f(s):
    for i in range(0,10):
        t = s.replace("X", str(i))
        a, b, c = map(int, re.split(r'\+|\=', t))
        if a + b == c and str(a) + "+" + str(b) + "=" + str(c) == t:
            return i
    return "NA"

while True:
    try:
        print f(raw_input())
    except:
        break

Submission status: Accepted

Solution code:
while True:
    try:
        formula = raw_input().split("=")
        sum = formula[1]
        left = formula[0].split("+")
        a = left[0]
        b = left[1]
        ok = 0
        for i in range(10):
            A = a.replace('X', str(i))
            B = b.replace('X', str(i))
            Sum = sum.replace("X", str(i))
            if ((len(A) > 1 and A[0] == '0') or ((len(B) > 1and B[0] == '0')) or ((len(Sum) > 1 and Sum[0] == 'X'))):
                continue
            if (int(A) + int(B) == int(Sum)):
                ok = 1
                print i
        if (ok == 0):
            print "NA"
    except:
        break

Submission status: Accepted

Solution code:
while(1):
    try:
        eq=raw_input()
    except:
        break
    N=[0,0,0] #C,B,A
    D=[0,0,0]
    n=0
    d=0
    Xtop=0
    na=1
    for c in eq[::-1]:
        if c=='+'or c=='=':
            d=0
            n=n+1
        elif c=='X':
            D[n]=D[n]+10**d
            d=d+1
        else:
            N[n]=N[n]+int(c)*10**d
            d=d+1
    for i in range(3):
        if D[i]>N[i] and D[i]>1:
            Xtop=1
    for i in range(10):
        if N[0]+D[0]*i==N[1]+D[1]*i+N[2]+D[2]*i:
            if not (Xtop and i==0):
                print i
                na=0
                break
    if na:
        print 'NA'

Submission status: Accepted

Solution code:
while True:
    try:
        string = raw_input()
    except EOFError:
        break
    for i in range(10):
        translated = string.replace("X",str(i))
        left,right = translated.split("=")
        first,second = left.split("+")
        if i == 0 and any((first[0]=="0" and len(first)>1, second[0]=="0" and len(second)>1, right[0]=="0" and len(right)>1)):
            continue
        elif int(first) + int(second) == int(right):
            print i
            break
    else:
        print "NA"

Submission status: Accepted

Solution code:
import sys

for line in sys.stdin:
    n = ''
    lis = []
    for c in line.rstrip():
        if c.isdigit() or c == 'X':
            n += c
        elif c == '+':
            lis.append(n)
            n = ''
        elif c == '=':
            lis.append(n)
            n = ''
    else:
        lis.append(n)
     
    c_lis = lis[:]   
    for n in range(10):
        for i, v in enumerate(lis):
            if 'X' in v:
                c_lis[i] = c_lis[i].replace('X', str(n))
        else:
            flag = False
            if n == 0:
                for v in c_lis:
                    if len(v) >= 2 and v[0] == '0':
                        flag = True
                        break
            if not flag:
                if int(c_lis[0]) + int(c_lis[1]) == int(c_lis[2]):
                    print n
                    break
            c_lis = lis[:]
    else:
        print 'NA'

Submission status: Accepted

Solution code:
while True:
	try:
		code = raw_input()
	except EOFError:
		break
	
	a,b = map(str,code.split("+"))
	b,c = map(str,b.split("="))
	num = [a,b,c]
	
	judge = True
	for i in xrange(10):
		if len(num[0]) > 1 and num[0][:1] == "X" and i == 0:
			continue
		if len(num[1]) > 1 and num[1][:1] == "X" and i == 0:
			continue
		if len(num[2]) > 1 and num[2][:1] == "X" and i == 0:
			continue
		if int(num[0].replace("X", str(i))) + int(num[1].replace("X", str(i))) == int(num[2].replace("X", str(i))):
			judge = False
			print i
			
	if judge:
		print "NA"

Submission status: Accepted

Solution code:
while True:
  try:
    l = input().rstrip()
    [a,b] = l.split("+")
    [c,d] = b.split("=")
    for i in range(10):
      if i == 0:
        if (a[0] == "X" != a) or (c[0] == "X" != c) or (d[0] == "X" != d):
          pass
        else:
          A = int(a.replace("X",str(i)))
          C = int(c.replace("X",str(i)))
          D = int(d.replace("X",str(i)))
          if A+C == D:
            print(i)
            break
      else:
        A = int(a.replace("X",str(i)))
        C = int(c.replace("X",str(i)))
        D = int(d.replace("X",str(i)))
        if A+C == D:
          print(i)
          break
    else:
      print('NA')
  except:
    break

Submission status: Accepted

Solution code:
import sys

for s in sys.stdin:
    #?????????????????????
    s=s[:-1]
    #????????¨??????????????????
    p=s.split("=")

    ans="NA"
    st=0

    #X??¨??????0????¨±?????????????????????
    #2?????\?????§?????????X???????????¨????????????
    tmp=p[0].split("+")
    if tmp[0][0]=="X" and len(tmp[0])>1:
        st=1
    if tmp[1][0]=="X" and len(tmp[1])>1:
        st=1
    if p[1][0]=="X" and len(p[1])>1:
        st=1

    #print(p)
    for i in range(st,10):
        a=eval(p[0].replace("X",str(i)))
        b=eval(p[1].replace("X",str(i)))
        if a==b:
            ans=i
            break

    print(ans)

Submission status: Accepted

Solution code:
import sys
data = sys.stdin.readlines()

def func (list):
	for i in list:
		if len(i)>=2 and i[0]=='0': return 0
	return 1

for s in data:
	for i in range(10):
		tmp = s.replace('X',str(i))
#		print tmp
		tmp = tmp.replace('+','=')
		tmp = tmp[:-1]
		list = tmp.split('=')
#		print (func(list))
		if func(list)==1 :
			if int(list[0])+int(list[1])==int(list[2]) :
				print i
				break
		if i==9 :
			print "NA"

Submission status: Accepted

Solution code:
import sys
import re
def line():return sys.stdin.readline().strip()

while True:
    try:
        e = re.findall(r"[0-9X]+",line())

        ans = -1
        for i in range(10):
            a,b,c = [_.replace("X",str(i)) for _ in e]
            if i == 0 \
                and (\
                (a[0] == "0" and len(a) != 1)\
                or (b[0] == "0" and len(b) != 1)\
                or (c[0] == "0" and len(c) != 1)\
                ):
                continue 
            if int(a) + int(b) == int(c) :
                ans = i
                break

        if ans == -1:
            print("NA")
        else:
            print(ans)
    except:
        break

Submission status: Accepted

Solution code:
import sys
import re
for line in sys.stdin:
	line=line.replace('\n','')
	s=re.split('[+=]',line)
	line=line.replace('=','==')
	ans="NA"
	f=any([x!='X' and x[0]=='X' for x in s])
	for i in '0123456789'[f:]:
		if eval(line.replace('X',i)):
			ans=i
			break
	print(ans)

Submission status: Accepted

Solution code:
inputs=[]
while True:
  try:
    inputs.append(input())
  except EOFError:
    break

for i in inputs:
  a,c=i.split("=")
  a,b=a.split("+")
  # a+b=c
  for x in range(0,10):
    if x==0:
      if (a[0]=="X" and len(a)>1)or(b[0]=="X" and len(b)>1)or(c[0]=="X" and len(c)>1):
          continue
    A=a.replace("X",str(x))
    B=b.replace("X",str(x))
    C=c.replace("X",str(x))
    if int(A)+int(B)==int(C):
      print(x)
      break
    if x == 9:
      print("NA")

Submission status: Accepted

Solution code:
import re
while 1:
    try:f=input()
    except:break
    s=any([len(x)>1 and x[0]=='X' for x in re.split('[+=]',f)])
    f=f.replace('=','==')
    for i in '0123456789'[s:]:
        if eval(f.replace('X',i)):print(i);break
    else:print('NA')

Submission status: Accepted

Solution code:
import copy
try:
	while True:
		A = [[]]
		L = input()
		i = 0
		flag = 0
		for a in L:
			if a != "+" and a != "=":
				A[i].append(a)
			else:
				i = i + 1
				A.append([])
		for a in A:
			if "X" == a[0] and len(a) != 1:
				flag = 1
		for i in range(10):
			B = copy.deepcopy(A)
			for Bi,b in enumerate(B):
				for bi,k in enumerate(b):
					if k == "X":
						B[Bi][bi] = str(i)
				B[Bi] = int("".join(B[Bi]))
			if B[0] + B[1] == B[2]:
				if i == 0 and flag == 1:
					print("NA")
					flag = 2
				else:
					print(i)
					flag = 2
		if flag == 1 or flag == 0:
			print("NA")
except EOFError:
	pass

Submission status: Accepted

Solution code:
while True:
    try:
        dataset = input()
    except:
        break
    flag = True
    for i in range(10):
        sequence = dataset.replace("=", "+").split("+")
        if i == 0 and any([True if seq[0]=="X" and len(seq) >= 2 else False for seq in sequence]):
            continue
        a, b, c = map(int, [seq.replace("X", str(i)) for seq in sequence])
        if a+b == c:
            print(i)
            flag = False
            break
    if flag:
        print("NA")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math
import re

for s in sys.stdin:
    equation = s.strip()
    equation = equation.replace('=', '==')
    found = False

    for i in range(10):
        candidate = equation.replace('X', str(i))

        a = candidate.split('+')[0]
        b_c = candidate.split('+')[1]
        b = b_c.split('==')[0]
        c = b_c.split('==')[1]

        #print(a, b, c)

        # zero check
        if (a[0] == '0' and len(a) != 1) or (b[0] == '0' and len(b) != 1) or (c[0] == '0' and len(c) != 1):
            pass
        else:
            if eval(candidate):
                print(i)
                found = True
    if not found:
        print('NA')

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0110
"""
# runtime error
import sys
import re

def solve(exp):
    p1 = re.compile('^0+')
    for i in range(10):
        modified_exp = exp.replace('X', str(i))
        left, right = modified_exp.split('=')
        left1, left2 = left.split('+')

        left1 = p1.sub('', left1)
        if left1 == '':
            left1 = '0'
        left2 = p1.sub('', left2)
        if left2 == '':
            left2 = '0'
        right = p1.sub('', right)
        if right == '':
            right = '0'

        # print('{} + {} <=> {}'.format(left1, left2, right))
        if int(left1) + int(left2) == int(right):
            return i
    return 'NA'



def solve2(exp):
    for i in range(10):
        left, right = exp.split('=')
        left1, left2 = left.split('+')

        left1 = left1.replace('X', str(i))
        left2 = left2.replace('X', str(i))
        right = right.replace('X', str(i))
        if len(left1) > 1 and left1[0] == '0':
            continue
        if len(left2) > 1 and left2[0] == '0':
            continue
        if len(right) > 1 and right[0] == '0':
            continue
        if int(left1, base=10) + int(left2, base=10) == int(right, base=10):
            return i
    return 'NA'




def main(args):
    for line in sys.stdin:
        #result = solve(line.strip())
        #print(result)
        result = solve2(line.strip())
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import re
import sys

s = sys.stdin.readline()
while s:
    for i in range(10):
        tmp = s.replace("X",str(i))
        a,b,c = map(int,re.split("[+=]",tmp))
        if a+b-c == 0:
            if len(str(a)+str(b)+str(c))+2 == len(tmp.rstrip()):
                print(i)
                break
            else:
                continue
    else:
        print("NA")
    s = sys.stdin.readline()

Submission status: Accepted

Solution code:
import re
while True:
    try:
        line = input()
    except:
        break

    arr = re.findall(r"[0-9X]+", line)

    ans = -1
    for i in "0123456789":
        a, b, r = [s.replace("X", str(i)) for s in arr]
        if i == "0" and \
            (a[0] == "0" and len(a) > 1) or \
            (b[0] == "0" and len(b) > 1) or \
            (r[0] == "0" and len(r) > 1):
                continue
#        print([a, b, r])
        if int(a) + int(b) == int(r):
            ans = i
            break
    if ans == -1:
        print("NA")
    else:
        print(ans)

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t and'+'!=e[1]) or('+X='not in e and'+X'in e )or(e[-1]==t and'=X'in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t and'+'!=e[1])or('+X='not in e and'+X'in e)or(e[-1]==t and'=X'in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*('+'!=e[1])or('+X'in e)*('+X='not in e)or('=X'in e)*(e[-1]==t):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!='+')or('+X'in e)*('+X='not in e)or('=X'in e)*(e[-1]==t):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!='+')or('+X'in e)*('+X='not in e)or(e[-2:]=='=X'):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!='+')or('+X'in e)*('+X='not in e)or(e[-1]==t and '=X'in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys,re
for e in sys.stdin:
    s=any([len(x)>1 and x[0]=='X' for x in re.split('[+=]',e.strip())])
    for i in '0123456789'[s:]:
        if eval(e.replace('X',i).replace('=','==')):print(i);break
    else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!='+')or('+X'in e)*('+X='not in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!='+')or('+X'in e)*('+X='not in e):]:
  l,r=e.replace(t,i).split('=')
  if eval(l)==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import re,sys
t='X'
for e in sys.stdin:
 for i in'0123456789'[any([len(x)>1 and x[0]=='X' for x in re.split('[+=]',e.strip())]):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys,re
for e in sys.stdin:
 s=any([len(x)>1 and x[0]=='X' for x in re.split('[+=]',e.strip())])
 for i in '0123456789'[s:]:
  if eval(e.replace('X',i).replace('=','==')):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import re,sys
for e in sys.stdin:
 s=any([len(x)>1 and x[0]=='X' for x in re.split('[+=]',e.strip())])
 for i in '0123456789'[s:]:
  if eval(e.replace('X',i).replace('=','==')):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys,re
for e in sys.stdin:
 for i in'0123456789'[any([len(x)>1 and x[0]=='X' for x in re.split('[+=]',e.strip())]):]:
  l,r=e.replace('X',i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
for e in sys.stdin:
 for i in'0123456789'[(e[0]=='X')*(e[1]!='+')or('+X'in e)*('+X='not in e):]:
  l,r=e.replace('X',i).split('=')
  if eval(l)==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
p,t='+X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!=p)or(p+t in e)*('+X='not in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split(p)))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
p,t,q='+X='
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!=p)or(p+t in e)*('+X='not in e):]:
  l,r=e.replace(t,i).split(q)
  if sum(map(int,l.split(p)))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
p,t='+X=','X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!='+')or(p[:2]in e)*(p not in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split('+')))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
import sys
p,t='+X'
for e in sys.stdin:
 for i in'0123456789'[(e[0]==t)*(e[1]!=p)or('+X'in e)*('+X='not in e):]:
  l,r=e.replace(t,i).split('=')
  if sum(map(int,l.split(p)))==int(r):print(i);break
 else:print('NA')

Submission status: Accepted

Solution code:
def restore(sec, x):
  return int(sec.replace("X", str(x)))

while True:
  try:
    s = input()
    p_ind = s.index("+")
    e_ind = s.index("=")
    sec1 = s[:p_ind]
    sec2 = s[p_ind + 1:e_ind]
    sec3 = s[e_ind + 1:]

    for x in range(10):
      if not (not x and
        ((sec1[0] == "X" and len(sec1) > 1) or
         (sec2[0] == "X" and len(sec2) > 1) or
         (sec3[0] == "X" and len(sec3) > 1))):
        if restore(sec1, x) + restore(sec2, x) == restore(sec3, x):
          print(x)
          break
    else:
      print("NA")

  except EOFError:
    break

Submission status: Accepted

Solution code:
import re,sys
def chk(t):
    for i in range(10):
        e=re.split("[+=]",re.sub("X",str(i),t))
        if (e[0][0]=="0" and len(e[0])>1) or (e[1][0]=="0" and len(e[1])>1) or (e[2][0]=="0" and len(e[2])>1): continue
        if eval(e[0]+"+"+e[1]) == eval(e[2]):
            return i
    return 10

l=[chk(i.strip()) for i in sys.stdin]
[print(i) if i != 10 else print("NA") for i in l]

Submission status: Accepted

Solution code:
# AOJ 0110 Alphametic
# Python3 2018.6.21 bal4u

while 1:
	try:
		ab, s = list(input().split('='))
		a, b = list(ab.split('+'))
		start = 0
		if (len(a) > 1 and a[0] == 'X') or (len(b) > 1 and b[0] == 'X') \
			or (len(s) > 1 and s[0] == 'X'): start = 1
		f = 1
		for x in range(start, 10):
			na = a.replace('X', str(x))
			nb = b.replace('X', str(x))
			ns = s.replace('X', str(x))
			if int(na) + int(nb) == int(ns):
				print(x)
				f = 0
				break
		if f: print("NA")
	except: break

Submission status: Accepted

Solution code:
def form_check(t):
    plus = t.index('+')
    equal = t.index('=')
    n = [t[:plus], t[plus + 1:equal], t[equal + 1:]]
    for a in n:
        if len(a) > 1 and a[0] == 'X':
            return False
    return True

def check(t):
    if eval(t.replace('=', ' == ')):
        return True
    else:
        return False

while True:
    try:
        line = raw_input()
    except EOFError:
        break

    if form_check(line):
        nums = range(0, 10)
    else:
        nums = range(1, 10)
    a = -1
    for num in nums:
        if check(line.replace('X', str(num))):
            a = num
    if a >= 0:
        print a
    else:
        print 'NA'

Submission status: Accepted

Solution code:
while True:
    try:
        NA = True
        formula = raw_input()
        plus = formula.find('+')
        equal = formula.find('=')
        term1, term2, term3 = formula[:plus], formula[plus+1:equal], formula[equal+1:]
        for i in range(10):
            if len(term1) == 1 and len(term2) == 1 and len(term3) == 1:
                pass
            elif i == 0 and (term1[0] == 'X' or term2[0] == 'X' or term3[0] == 'X'):
                    continue
            if int(term1.replace('X', str(i))) + int(term2.replace('X', str(i))) == int(term3.replace('X', str(i))):
                NA = False
                print i
                break
        if NA: print "NA"
    except (EOFError):
        break

Submission status: Accepted

Solution code:
import re
while 1:
    try:
        ss = re.split('[+=]', raw_input())
        for i in range(1 if any([len(s) >= 2 and s[0] == 'X' for s in ss]) else 0, 10):
            ns = [long(s.replace('X', str(i))) for s in ss]
            if ns[0] + ns[1] == ns[2]:
                print i
                break
        else:
            print 'NA'
    except EOFError:
        break

Submission status: Accepted

Solution code:
import sys
for line in sys.stdin.readlines():
    exp = line.strip()
    ans = "NA"
    # 1<=X<10
    for i in xrange(1,10):
        filled_exp = exp.replace("X",str(i))
        left,right = filled_exp.split("=")
        a,b = left.split("+")
        if int(a)+int(b) == int(right):
            ans = i
    # X==0
    filled_exp = exp.replace("X","0")
    left,right = filled_exp.split("=")
    a,b = left.split("+")
    if not any([(s[0]=="0" and len(s)>1)  for s in (a,b,right)]):
        if int(a)+int(b) == int(right):
            ans = 0
    print ans

Submission status: Accepted

Solution code:
while True:
    try:
        s=raw_input()
    except EOFError:
        break
    flag=False
    a1=s.split("+")[0]
    a2,a3=s.split("+")[1].split("=")
    for i in range(10):
        t1,t2,t3=a1,a2,a3
        if len(t1)>=2 and t1[0]=="X" and i==0:
            continue
        if len(t2)>=2 and t2[0]=="X" and i==0:
            continue
        if len(t3)>=2 and t3[0]=="X" and i==0:
            continue
        t1=int(t1.replace("X",str(i)))
        t2=int(t2.replace("X",str(i)))
        t3=int(t3.replace("X",str(i)))
        if t1+t2==t3:
            print i
            flag=True
            break
    if flag==False:
        print "NA"

Submission status: Accepted

Solution code:

import sys


def solv(s):
    for i in range(10):
        i = str(i)
        s2 = s.replace('X', i)
        left, right = s2.split('=')
        l1, l2 = left.split('+')
        if any([x[0] == '0' and len(x) > 1 for x in [l1, l2, right]]):
            continue
        if int(l1) + int(l2) == int(right):
            return i
    else:
        return 'NA'


for line in sys.stdin:
    print solv(line.rstrip('\n'))

Submission status: Accepted

Solution code:
import sys
def to_ints(a,b,c,n):
    d=list(a)
    e=list(b)
    f=list(c)
    for i in range(len(d)):
        if d[i]=='X': d[i]=n
    for i in range(len(e)):
        if e[i]=='X': e[i]=n
    for i in range(len(f)):
        if f[i]=='X': f[i]=n
    return map(int,("".join(d),"".join(e),"".join(f)))

for e in sys.stdin.readlines():
    e=e.rstrip()
    i=e.index('+')
    j=e.index('=')
    a=e[:i]
    b=e[i+1:j]
    c=e[j+1:]
    s = 0
    if len(a)>1 or len(b)>1 or len(c)>1:
        s=1 if a[0]=='X' or b[0]=='X' or c[0]=='X' else 0
    ans=-1
    for n in range(s,10):
        x,y,z=to_ints(a,b,c,str(n))
        if x+y==z: ans=n
    print ans if ans>=0 else 'NA'

Submission status: Accepted

Solution code:
f=lambda x,y: int(x.replace("X",str(y)))
q=lambda x:len(str(x))
while True:
	try:
		ex = raw_input()
		a,b,c = ex[0:ex.index("+")],ex[ex.index("+")+1:ex.index("=")],ex[ex.index("=")+1:]
		for i in range(10):
			fa,fb,fc=f(a,i),f(b,i),f(c,i)
			if q(fa)==q(a) and q(fb)==q(b) and q(fc)==q(c):
				ans = i if fa+fb==fc else -1
				if ans!=-1:
					print ans
					break
			else:
				continue
		else:
			print "NA"
	except EOFError:
		break