input
stringlengths 20
127k
| target
stringlengths 20
119k
| problem_id
stringlengths 6
6
|
---|---|---|
n = int(eval(input()))
a = list(map(int, input().split()))
a.sort()
for i in range(1, len(a)):
while 0 != a[i - 1]:
r = a[i] % a[i - 1]
a[i] = a[i - 1]
a[i - 1] = r
print((a[-1]))
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(a, b):
r = a % b
if 0 == r:
return b
else:
return gcd(b, r)
def gcd_for_arr(l):
tmp = gcd(l[0], l[1])
for i in range(2, len(l)):
tmp = gcd(tmp, l[i])
return tmp
print((gcd_for_arr(a)))
|
p03127
|
def gcd(m, n):
while n: # nが0になるまで繰り返す
m, n = n, m % n
return m
n = int(eval(input())) # n体のモンスター
a = list(map(int, input().split())) # それぞれの体力
g = gcd(a[0], a[1]) # 最大公約数を求める
for i in range(2, n):
g = gcd(g, a[i]) # 最初のgは1つ目と2つ目の要素の最大公約数がすでに入っている
print(g)
|
def gcd(m, n):
while n: # nが0になるまで続ける
m, n = n, m % n
return m
N = int(eval(input())) # モンスターの数
A = list(map(int, input().split())) # モンスターのそれぞれの体力のリスト
g = gcd(A[0], A[1])
for i in range(2, N):
g = gcd(g, A[i])
print(g)
|
p03127
|
import sys
stdin = sys.stdin
def li(): return list(map(int, stdin.readline().split()))
N = li()
A = list(li())
def gcd(A, B):
# fast return
if B == 1 or A == 1:
return 1
# normal gcd logic
while B not in (1, 0):
A, B = min(A, B), abs(A - B)
if B == 0:
return A
elif B == 1:
return 1
A = list([x for x in A if x>0])
A.sort()
if len(A) == 0:
print((0))
else:
res = A[0]
for a in A:
res = gcd(a, res)
if res == 1:
break
print(res)
|
import sys
stdin = sys.stdin
def li(): return list(map(int, stdin.readline().split()))
N = li()
A = list(li())
def gcd(A, B):
# normal gcd logic
while B not in (1, 0):
A, B = (A, B) if A > B else (B, A)
_, mod = divmod(A, B)
A, B = B, mod
if B == 0:
return A
elif B == 1:
return 1
A = list([x for x in A if x>0])
A.sort()
if len(A) == 0:
print((0))
else:
res = A[0]
for a in A:
res = gcd(a, res)
if res == 1:
break
print(res)
|
p03127
|
N = int(eval(input()))
A = [int(i) for i in input().split()]
A.sort()
def calc(a, b):
while(a != b):
largeNum = max(a, b)
smallNum = min(a, b)
largeNum -= smallNum
a = largeNum
b = smallNum
return a
ans = 1
for i in range(0, N - 1):
a = calc(A[i], A[i+1])
A[i], A[i+1] = a, a
if a == 1:
break
else:
ans = min(A)
print(ans)
|
N = int(eval(input()))
A = [int(i) for i in input().split()]
A.sort(reverse = True)
def calc(a, b):
largeNum = max(a, b)
smallNum = min(a, b)
while(largeNum != 0 and smallNum != 0):
largeNum %= smallNum
x = largeNum
largeNum = smallNum
smallNum = x
return max(smallNum, largeNum)
ans = 1
for i in range(0, N - 1):
a = calc(A[i], A[i+1])
A[i], A[i+1] = a, a
if a == 1:
break
else:
ans = min(A)
print(ans)
|
p03127
|
n=int(eval(input()))
a=list(map(int,input().split()))
def search_common_divisor(x,y):
if x<y:
x,y=y,x
while y!=0:
x,y=y,x%y
return x
def search_list_common_divisor(*numbers):
c=[numbers[0][i] for i in range(n)]
b=[search_common_divisor(c[0],c[1])]
for i in c:
b.append(search_common_divisor(b[-1],i))
return b[-1]
print((search_list_common_divisor(a)))
|
n=int(eval(input()))
a=list(map(int,input().split()))
def search_common_divisor(x,y):
if x<y:
x,y=y,x
while y!=0:
x,y=y,x%y
return x
def search_list_common_divisor(*numbers):
b=[search_common_divisor(numbers[0],numbers[1])]
for i in numbers:
b.append(search_common_divisor(b[-1],i))
return b[-1]
print((search_list_common_divisor(*a)))
|
p03127
|
n = int(eval(input()))
monster= list(map(int,input().split()))
while True:
index_list = [i for i, v in enumerate(monster) if v >0]
mini = monster[index_list[0]]
for i in range(1, len(index_list)):
if mini > monster[index_list[i]]:
mini = monster[index_list[i]]
miniindex_list = [i for i, v in enumerate(monster) if v ==mini]
min_index = miniindex_list[0]
for i in range(len(monster)):
if not i == min_index:
monster[i] = monster[i] % monster[min_index]
non_zeroindex_list = [i for i, v in enumerate(monster) if v >0]
non_zeroindex = non_zeroindex_list[0]
if len(non_zeroindex_list) == 1:
break
print((monster[non_zeroindex]))
|
n = int(eval(input()))
monsters = list(map(int, input().split()))
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
gcd(4,2)
def gcd_more(a):
ans = a[0]
for i in range(1,len(a)):
ans = gcd(ans, a[i])
return ans
print((gcd_more(monsters)))
|
p03127
|
from functools import reduce
def gcd(a, b):
if b > a:
a, b = b, a
dp = [a, b]
for i, v in enumerate(dp, start=2):
dp.append(dp[i - 2] % dp[i - 1])
if dp[i] == 0:
break
return dp[i - 1]
N = int(eval(input()))
a = list(map(int, input().split()))
print((reduce(gcd, a)))
|
import math
n = int(eval(input()))
a = list(map(int, input().split()))
ans = 0
for i in a:
ans = math.gcd(ans, i)
print(ans)
|
p03127
|
from heapq import heapify, heappush, heappop
N = int(eval(input()))
Alist = list(map(int, input().split()))
heapify(Alist)
while len(Alist)>1:
Alist = [a for a in Alist if a > 0]
min_num = heappop(Alist)
for i in range(len(Alist)):
Alist[i] %= min_num
heappush(Alist, min_num)
print((Alist[0]))
|
from heapq import heapify, heappop, heappush
N = int(eval(input()))
Alist = list(map(int, input().split()))
heapify(Alist)
while len(Alist) > 1:
atk = heappop(Alist)
Alist = [A % atk for A in Alist if (A % atk != 0)]
heapify(Alist)
heappush(Alist, atk)
print((Alist[0]))
|
p03127
|
n=int(eval(input()))
hoge=list(map(int,input().split()))
###
def huga(a,b):
x=max(a,b)
y=min(a,b)
z=1
for i in range(y):
wa=y-i
if(x%wa==0 and y%wa==0):
z=wa
break
return z
###
hoge.sort(reverse=True)
ans=hoge[0]
for i in range(n-1):
ans=huga(ans,hoge[i+1])
print(ans)
|
def gcd(a,b):
if(a<b):
a,b=b,a
if(b==0):
return a
c= a%b
return gcd(b,c)
n=int(eval(input()))
hoge=list(map(int,input().split()))
hoge.sort()
for i in range(n-1):
hoge[i+1]=gcd(hoge[i],hoge[i+1])
print((hoge[-1]))
|
p03127
|
n = int(eval(input()))
a = list(map(int, input().split()))
a = list(set(a))
while len(a) > 1:
mina = min(a)
for i in a:
if i == mina:
continue
elif i % mina == 0:
a.remove(i)
else:
a[a.index(i)] = i % mina
print((a[0]))
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
g = a[0]
for i in a:
g = gcd(g, i)
print(g)
|
p03127
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a%b)
g = a[0]
for i in a:
g = gcd(g, i)
print(g)
|
n = int(eval(input()))
a = list(map(int, input().split()))
min_ind = a.index(min(a))
while True:
min_num = min(a)
min_ind = a.index(min_num)
a = [n % min_num for n in (a[:min_ind] + a[min_ind+1:]) if n % min_num != 0]
if a == []:
print(min_num)
exit()
a.append(min_num)
|
p03127
|
N=int(eval(input()))
A=list(map(int,input().split()))
while len(A)>=2:
A=sorted(A,reverse=True)
while 1:
if not A[-1]:
A.pop()
else:
for i in range(len(A)-1):
A[i]%=A[-1]
break
print((A[0]))
|
def gcd(a, b):
while b:
a, b = b, a % b
return a
N=int(eval(input()))
A=list(map(int,input().split()))
num=A[0]
for i in range(1,N):
num=gcd(num,A[i])
print(num)
|
p03127
|
N = int(eval(input()))
lista = sorted(list(map(int, input().split())))
length = len(lista)
while length>1:
#print(length)
#print(lista)
for n in range(1, length):
lista[n] = lista[n]%lista[0]
#print(lista)
lista.sort()
#print(lista)
lista = list(set(lista))
if lista[0] == 0:
lista.remove(0)
#print(lista)
length = len(lista)
print((lista[0]))
|
N = int(eval(input()))
lista = sorted(list(map(int, input().split())))
length = 2
while length>1:
#print(length)
#print(lista)
for n in range(1, length):
lista[n] = lista[n]%lista[0]
#print(lista)
lista = list(set(lista))
lista.sort()
if lista[0] == 0:
lista.remove(0)
#print(lista)
length = len(lista)
print((lista[0]))
|
p03127
|
from functools import reduce
def gcd(a, b):
while b:
a, b = b, a % b
return a
def gcd_n(numbers):
return reduce(gcd, numbers)
def main():
N, *A = list(map(int, open(0).read().split()))
print((gcd_n(A)))
return
main()
|
def main():
N, *A = list(map(int, open(0).read().split()))
b = None
for a in A:
if b is None:
b = a
continue
while b:
a, b = b, a % b
b = a
print(a)
return
main()
|
p03127
|
# coding: utf-8
# Your code here!
N=int(eval(input()))
A=list(map(int,input().split()))
a=min(A)
ans=0
for i in range(1,int(a**0.5)//1+1):
judge=True
for item in A:
if item%i!=0:
judge=False
if judge:
ans=max(ans,i)
judge=True
for item in A:
if item%(a//i)!=0:
judge=False
if judge:
ans=max(ans,(a//i))
print(ans)
|
# coding: utf-8
# Your code here!
N=int(eval(input()))
A=list(map(int,input().split()))
a=min(A)
ans=0
l=[]
def gcd(lis,num):
temp=[]
for item in lis:
if num%item!=0:
pass
else:
temp.append(item)
return temp
for i in range(1,int(a**0.5)//1+1):
if a%i==0:
l.append(i)
l.append(a//i)
for item in A:
l=gcd(l,item)
print((max(l)))
|
p03127
|
###C - Monsters Battle Royale
N = int(eval(input()))
A = list(map(int, input().split()))
while(len(set(A)) != 1):
for i in range(N):
minA = min(A)
if A[i] > minA:
time = A[i] // minA
if time == 1:
A[i] =A[i] - time * minA
else:
A[i] =A[i] - (time - 1) * minA
print((min(A)))
|
###C - Monsters Battle Royale
N = int(eval(input()))
A = list(map(int, input().split()))
while(len(set(A)) != 1):
minA = min(A)
for i in range(N):
if A[i] > minA:
time = A[i] // minA
if time == 1:
A[i] =A[i] - time * minA
else:
A[i] =A[i] - (time - 1) * minA
print((min(A)))
|
p03127
|
n = int(eval(input()))
a = list(map(int,input().split()))
a = sorted(list(set(a)))
while len(a) > 1:
b = a.pop()
d = []
for i in a:
c = b%i
if c != 0:
d.append(c)
a = sorted(list(set(d+a)))
print((a[0]))
|
n = int(eval(input()))
a = list(map(int,input().split()))
a = sorted(list(set(a)))
while len(a) > 1:
b = a.pop(0)
d = []
for i in a:
c = i%b
if c != 0:
d.append(c)
a = sorted(list(set(d+[b])))
print((a[0]))
|
p03127
|
N = int(eval(input()))
Flag = [0 for i in range(N)]
A = list(map(int,input().split()))
while len(A) != 1:
A.sort()
mi = min(A)
for _ in range(1,len(A)):
if A.count(1) > 1:
del A[1:]
break
try:
A[_] -= mi
if A[_] <= 0:
A.pop(_)
except:
pass
print((A[0]))
|
N = int(eval(input()))
A = list(map(int,input().split()))
while len(A) > 1:
a_ = A[0]
for x in range(1,len(A)):
A[x] %= a_
A = [ans for ans in A if ans > 0]
A.sort()
print((A[0]))
|
p03127
|
def uauau():
N = int(eval(input()))
aA=sorted(set(int(_) for _ in input().split()))
N = len(aA)
def ggg(i,j):
i %= j
if i == 0:
return j
else:
return ggg(j,i)
if len(aA) == 1:
print((aA[0]))
else:
iMin = aA[0]
iMax = aA[-1]
if iMin == 1:
print((1))
else:
iMt = max(i % iMin for i in aA)
if iMt == 0:
print(iMin)
exit()
elif iMt == 1:
print((1))
else:
iM = iMax
for i in range(N):
iA = aA[i]
for j in range(i+1,N):
if aA[j] % iA != 0:
iMt = ggg(aA[j],iA)
if iMt < iM:
iM = iMt
if iM == 1:
print((1))
exit()
print(iM)
uauau()
|
#gcdか....
def fgcd(a,b):
while b:
a,b = b,a%b
return a
N = int(eval(input()))
aA = sorted(set(int(_) for _ in input().split()))
b = aA[0]
for a in aA[1:]:
b = fgcd(a,b)
print(b)
|
p03127
|
#gcdか....
def fgcd(a,b):
while b:
a,b = b,a%b
return a
N = int(eval(input()))
aA = sorted(set(int(_) for _ in input().split()))
b = aA[0]
for a in aA[1:]:
b = fgcd(a,b)
print(b)
|
N = int(eval(input()))
aA=[int(_) for _ in input().split()]
iMin = min(aA)
iMax = max(aA)
def fgcd(a,b):
while b:
a,b = b,a%b
return a
if iMin == 1:
print((1))
else:
iDm = [ _ % iMin for _ in aA]
if max(iDm) == 0:
print(iMin)
else:
b = aA[0]
for _ in aA[1:]:
b = fgcd(_,b)
if b == 1:
print((1))
exit()
print(b)
|
p03127
|
N = int(eval(input()))
data = [int(i) for i in input().split()]
i = 0
def euq(x,y):
a = max(x,y)
b = min(x,y)
if a % b == 0:
return b
else:
return euq(b, a % b)
while i < N-1:
com = euq(data[i],data[i + 1])
data[i+1] = com
i += 1
print(com)
|
n = int(eval(input()))
data = [int(s) for s in input().split()]
def gcd(a,b):
if b == 0:
return a
else:
return gcd(b,a%b)
x = gcd(data[0], data[1])
for i in range(2,n-1):
x = gcd(x,data[i])
print(x)
|
p03127
|
N = int(eval(input()))
A = list(map(int, input().split()))
# 体力最小のモンスターが他全員に攻撃することを繰り返す。ただし体力0にはしない
# 全員同じ体力になったら終了
while len(set(A)) >= 2:
A = list(set(A))
A = [x%min(A) if x%min(A) != 0 else min(A) for x in A]
print((A[0]))
|
N = int(eval(input()))
A = list(map(int, input().split()))
# 体力最小のモンスターが他全員に攻撃することを繰り返す。ただし体力0にはしない
# 全員同じ体力になったら終了
while len(set(A)) >= 2:
A = list(set(A))
min_A = min(A)
A = [x%min_A if x%min_A != 0 else min_A for x in A]
print((A[0]))
|
p03127
|
N = int(eval(input()))
A = list(map(int,input().split()))
def gcd(m, n):
while n:
m, n = n, m%n
return m
ans = A[0]
tmp = []
for i in range(1,N):
tmp.append(gcd(ans,A[i]))
print((min(tmp)))
|
def gcd(m, n):
while n:
m, n = n, m%n
return m
def ans():
N = int(eval(input()))
A = list(map(int,input().split()))
tmp = A[0]
for i in range(1,N):
tmp = gcd(A[i], tmp)
print(tmp)
ans()
|
p03127
|
N = int(eval(input()))
A = list(map(int,input().split()))
def gcd(m, n):
while n:
m, n = n, m%n
return m
def lcm(m, n):
return (m*n)//gcd(m, n)
ans = A[0]
for i in range(1, N):
ans = gcd(ans, A[i])
print(ans)
|
N = int(eval(input()))
A = list(map(int,input().split()))
def gcd(m, n):
while n:
m, n = n, m%n
return m
ans = A[0]
for i in range(1,N):
ans = gcd(ans, A[i])
print(ans)
|
p03127
|
n = int(eval(input()))
a = sorted(list(map(int, input().split())))
while True:
a = sorted(list(set(a)))
tmp = sorted(a[:20])
for i in tmp:
if i == a[0]:
continue
elif i % a[0] == 0:
a.remove(i)
else:
a.append(i % a[0])
a.remove(i)
if len(a) == 1:
break
print((a[0]))
|
n = int(eval(input()))
a = sorted(list(map(int, input().split())))
while True:
a = sorted(list(set(a)))
tmp = sorted(a[:])
for i in tmp:
if i == a[0]:
continue
elif i % a[0] == 0:
a.remove(i)
else:
a.append(i % a[0])
if i % a[0] == 1:
print((1))
exit()
a.remove(i)
if len(a) == 1:
break
print((a[0]))
|
p03127
|
N = int(eval(input()))
L = list(map(int,input().split()))
L = sorted(L)
while True:
for i in range(1,len(L)):
L[i] %= L[0]
L= [x for x in L if x!=0]
L= sorted(L)
if len(L) == 1:
break
print((L[0]))
|
def gcd(x , y):
if x < y:
x , y = y , x
if y == 0:
return x
r = x % y
return gcd(y , r)
N = int(eval(input()))
L = list(map(int,input().split()))
ans = L[0]
for i in range(len(L)):
ans = gcd( ans ,L[i] )
print(ans)
|
p03127
|
N = int(eval(input()))
L = list(map(int,input().split()))
L.sort()
def gcd(x,y):
while y != 0:
k = x
x = y
y = k%y
return x
G = gcd(L[0],L[1])
for i in range(1,N-1):
if G >= gcd(L[i],L[i+1]):
G = gcd(L[i],L[i+1])
print(G)
|
N = int(eval(input()))
L = list(map(int,input().split()))
L.sort()
def gcd(x,y):
while y != 0:
k = x
x = y
y = k%y
return x
cur = L[0]
for i in range(N):
cur = gcd(cur,L[i])
print(cur)
|
p03127
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(x, y):
if x < y:
x, y = y, x
while y > 0:
x, y = y, x % y
return x
g = a[0]
for i in range(n-1):
g = gcd(g, a[i+1])
print(g)
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(x, y):
while y > 0:
x, y = y, x % y
return x
g = a[0]
for _a in a[1:]:
g = gcd(g, _a)
print(g)
|
p03127
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(x, y):
while y > 0:
x, y = y, x % y
return x
g = a[0]
for i in range(n-1):
g = gcd(g, a[i+1])
print(g)
|
n = int(eval(input()))
a = list(map(int, input().split()))
def gcd(x, y):
if x < y:
x, y = y, x
while y > 0:
x, y = y, x % y
return x
g = a[0]
for _a in a[1:]:
g = gcd(g, _a)
print(g)
|
p03127
|
n = int(eval(input()))
aa = list(map(int,input().split()))
# print(aa)
def mod(xx):
bb=[]
minX = min(xx)
for i in range(len(xx)):
b = xx[i] % minX if xx[i]>min(xx) else xx[i]
if b>0:
bb.append(b)
return list(set(bb))
while True:
bb = mod(aa)
# print(bb)
if min(aa)==min(bb) or min(bb)==1:
break
aa=bb
print((min(bb)))
|
n = int(eval(input()))
aa = list(map(int,input().split()))
def gcd(a, b):
while b:
a, b = b, a % b
return a
ans=aa[0]
for i in range(1,n):
ans = gcd(ans, aa[i])
print(ans)
|
p03127
|
import heapq
N = int(eval(input()))
A = list(map(int, input().split()))
heapq.heapify(A)
flag = True
while flag:
minA = heapq.heappop(A) # 最小値を取り出す
if len(A) == 0: # 要素がなかったら終わり
break
if minA == 0: # 0だったら除外したまま続ける
continue
A = list([x % minA for x in A])
heapq.heappush(A, minA)
print(minA)
|
N = int(eval(input()))
A = list(map(int, input().split()))
def gcd(a, b):
if b == 0:
return a
else:
return(gcd(b, a % b))
ans = A[0]
for i in range(1, N):
ans = gcd(ans, A[i])
print(ans)
|
p03127
|
N = int(eval(input()))
A = [int(i) for i in input().split()]
def attack(A):
A = list(set(A))
A.sort()
if A[0] ==0:
A.pop(0)
attack_mons = A[0]
new_A = [A[0]]
for i in range(1,len(A)):
new_A.append(A[i]%min(A))
return new_A
new_A = attack(A)
while A!=new_A:
A = new_A
new_A = attack(new_A)
print((min(new_A)))
|
N = int(eval(input()))
A = [int(i) for i in input().split()]
def gcd(a,b):
while b!=0:
a,b=b,a%b
return a
ans = A[0]
for a in A:
ans = gcd(ans,a)
print(ans)
|
p03127
|
a = eval(input())
list = input().split()
b = len(list)
for j in range(b):
list[j] = int(list[j])
for k in range(b-1):
l = k + 1
while l < b:
if list[k] > list[l]:
temp = list[k]
list[k] = list[l]
list[l] = temp
l += 1
gcd = int(list[0])
for i in range(b):
m = list[i]
n = gcd
while n != 0:
m,n = n,m%n
if(m<gcd):
gcd = m
print(gcd)
|
a = eval(input())
list = list(map(int,input().split()))
b = len(list)
gcd = int(list[0])
for i in range(b):
m = list[i]
n = gcd
while n != 0:
m,n = n,m%n
if(m<gcd):
gcd = m
print(gcd)
|
p03127
|
from heapq import heapify, heappush, heappop
def main():
N = int(eval(input()))
A = list(map(int, input().split()))
heapify(A)
while len(A) > 1:
m = heappop(A)
A = [a % m for a in A if a % m != 0]
A.append((m))
print(m)
if __name__ == "__main__":
main()
|
def main():
N = int(eval(input()))
A = list(map(int, input().split()))
while len(A) > 1:
m = min(A)
A = [a % m for a in A if a % m != 0]
A.append((m))
print(m)
if __name__ == "__main__":
main()
|
p03127
|
n = int(eval(input()))
A = [int(i) for i in input().split()]
while True:
if len(A) == 1:
print((A[0]))
break
A[A.index(max(A))] %= min(A)
if 0 in A:
A.remove(0)
|
n = int(eval(input()))
A = [int(i) for i in input().split()]
def gcd(x, y):
while y > 0:
x, y = y, x % y
return x
for i in range(n - 1):
A[i + 1] = gcd(A[i], A[i + 1])
print((A[-1]))
|
p03127
|
N = int(eval(input()))
A = list(map(int, input().split()))
def gcd(x, y):
while y:
x, y = y, x % y
return x
gcd_A = A[0]
for i in range(1, N):
gcd_A = gcd(gcd_A, A[i])
print(gcd_A)
|
N = int(eval(input()))
A = list(map(int, input().split()))
#最大公約数
def gcd(x, y):
while y:
x, y = y, x % y
return x
gcd_calc = A[0]
for a in A[1:]:
gcd_calc = gcd(gcd_calc, a)
print(gcd_calc)
|
p03127
|
#priority_queueで管理
#普通にソートすればいいじゃん
n=int(eval(input()))
a=sorted([int(i) for i in input().split()])
#print(a)
while len(a)!=1:
b=a[0]
for i in range(1,n):
a[i]-=b*(a[i]//b)
a=sorted([i for i in a if i!=0])
n=len(a)
#print(a)
print((a[0]))
|
n=int(eval(input()))
a=list(map(int,input().split()))
while len(a)>1:
l=len(a)
a.sort()
b=[a[0] if i==0 else a[i]%a[0] for i in range(l)]
a=[b[i] for i in range(l) if b[i]!=0]
print((a[0]))
|
p03127
|
import sys
sys.setrecursionlimit(10**9)
def mi(): return list(map(int,input().split()))
def ii(): return int(eval(input()))
def isp(): return input().split()
def deb(text): print(("-------\n{}\n-------".format(text)))
INF=10**20
def main():
N = ii()
A = list(mi())
A.sort()
if A[0] == 1:
print((1))
exit()
ini = A[0]
while len(A) > 1:
x = A[0]
B = set([x])
for a in A:
b = a%x
if b > 0:
B.add(b)
A = list(B)
#print(A)
if len(A) > 0:
print((A[0]))
else:
print(ini)
if __name__ == "__main__":
main()
|
import sys
sys.setrecursionlimit(10**9)
def mi(): return list(map(int,input().split()))
def ii(): return int(eval(input()))
def isp(): return input().split()
def deb(text): print(("-------\n{}\n-------".format(text)))
INF=10**20
def main():
N = ii()
A = list(mi())
A.sort()
if A[0] == 1:
print((1))
exit()
ini = A[0]
while len(A) > 1:
x = A[0]
B = set([x])
for a in A:
b = a%x
if b > 0:
B.add(b)
A = list(B)
A.sort()
if len(A) > 0:
print((A[0]))
else:
print(ini)
if __name__ == "__main__":
main()
|
p03127
|
N,K,X,Y=int(eval(input())),int(eval(input())),int(eval(input())),int(eval(input()))
print((K*X+(N-K)*Y if N > K else N*X))
|
n,k,x,y=[int(eval(input())) for _ in range(4)];print(([n*x,k*x+(n-k)*y][n-k>0]))
|
p04011
|
#coding: utf-8
N, K, X, Y = [int(eval(input())) for _ in range(4)]
print((X * K + (N-K) * Y if N >= K else X * N))
|
#coding: utf-8
N, K, X, Y = [int(eval(input())) for _ in range(4)]
print((X * min(K,N) + max((N-K), 0) * Y))
|
p04011
|
n = int(eval(input()))
k = int(eval(input()))
x = int(eval(input()))
y = int(eval(input()))
if n <= k:
print((n*x))
else:
print((k*x + (n-k)*y))
|
n,k,x,y = [int(eval(input())) for i in range(4)]
cost = (min(n,k)*x+max(0,n-k)*y)
print(cost)
|
p04011
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N-K > 0:
print((X*K+(N-K)*Y))
else:
print((X*N))
|
n, k, x, y = (int(eval(input())) for i in range(4))
print((n*x-(x-y)*max((n-k), 0)))
|
p04011
|
N,K,X,Y = [int(eval(input())) for _ in range(4)]
cnt = 0
for i in range(N):
if i<K:
cnt += X
else:
cnt += Y
print(cnt)
|
n,k,x,y = [int(eval(input())) for _ in range(4)]
if n>k:
print((k*x+(n-k)*y))
else:
print((n*x))
|
p04011
|
n = int(eval(input()))
k = int(eval(input()))
x = int(eval(input()))
y = int(eval(input()))
ans = 0
for i in range(n):
if i + 1 <= k:
ans += x
else:
ans += y
print(ans)
|
n, k, x, y = [int(eval(input())) for i in range(4)]
print((k*x + (n-k)*y if n >= k else n*x))
|
p04011
|
n, k, x, y = [int(eval(input())) for i in range(4)]
sum = 0
for t in range(1, n+1):
if t <= k:
sum += x
else:
sum += y
print(sum)
|
n = int(eval(input()))
k = int(eval(input()))
x = int(eval(input()))
y = int(eval(input()))
if n <= k:
ans = n * x
else:
ans = k * x + (n-k) * y
print(ans)
|
p04011
|
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list([int(x)-1 for x in stdin.readline().split()])
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
n, k, x, y = IR(4)
ans = 0
for i in range(n):
if i >= k:
ans += y
else:
ans += x
print(ans)
return
#B
def B():
return
#C
def C():
return
#D
def D():
return
#E
def E():
return
#F
def F():
return
#G
def G():
return
#H
def H():
return
#Solve
if __name__ == '__main__':
A()
|
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list([int(x)-1 for x in stdin.readline().split()])
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')
#A
def A():
n, k, x, y = II(),II(),II(),II()
ans = 0
for i in range(n):
if i >= k:
ans += y
else:
ans += x
print(ans)
return
#B
def B():
return
#C
def C():
return
#D
def D():
return
#E
def E():
return
#F
def F():
return
#G
def G():
return
#H
def H():
return
#Solve
if __name__ == '__main__':
A()
|
p04011
|
a = [int(eval(input())) for i in range(4)]
if a[0] <= a[1]:
print((a[0]*a[2]))
else:
print((a[1]*a[2] + (a[0]-a[1])*a[3]))
|
a = int(eval(input()))
b = int(eval(input()))
c = int(eval(input()))
d = int(eval(input()))
if a > b:
print((b*c+(a-b)*d))
else:
print((a*c))
|
p04011
|
n=int(eval(input()))
k=int(eval(input()))
x=int(eval(input()))
y=int(eval(input()))
if k<n:
print((x*k+y*(n-k)))
else:
print((x*n))
|
n,k,x,y=[int(eval(input())) for _ in range(4)]
print((x*min(k,n)+y*max(n-k,0)))
|
p04011
|
l = [int(eval(input())) for _ in range(4)]
if l[1] >= l[0]:
print((l[0]*l[2]))
else:
print((l[1]*l[2] + (l[0]-l[1])*l[3]))
|
n,k,x,y = (int(eval(input())) for _ in range(4))
print((min(n,k)*x + y*max(n-k,0)))
|
p04011
|
N, K, X, Y = (int(eval(input())) for i in range(4))
print((K * X + (N - K) * Y if N > K else N * X))
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N > K:
print((K * X + (N - K) * Y))
else:
print((N * X))
|
p04011
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
P = 0
for i in range(1,N+1):
if i <= K:
P = P + X
else:
P = P + Y
print(P)
|
N, K, X, Y = (int(eval(input())) for i in range(4))
P = 0
for i in range(1,N+1):
if i <= K:
P = P + X
else:
P = P + Y
print(P)
|
p04011
|
total_stay = int(eval(input()))
normal_stay = int(eval(input()))
normal_cost = int(eval(input()))
discounted_cost = int(eval(input()))
if total_stay <= normal_stay:
total_cost = total_stay * normal_cost
else:
total_cost = normal_stay * normal_cost + (total_stay - normal_stay) * discounted_cost
print(total_cost)
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N <= K:
print((N * X))
else:
print((K * X + (N - K) * Y))
|
p04011
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N <= K:
print((N*X))
else:
print((K*X + (N-K)*Y))
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N <= K:
print((N*X))
else:
print((K*X+Y*(N-K)))
|
p04011
|
N,K,X,Y=[int(eval(input())) for i in range(4)]
print((N*X-(X-Y)*max(0,N-K)))
|
N,K,X,Y=[int(eval(input())) for i in range(4)]
print((min(N,K)*X+max(0,N-K)*Y))
|
p04011
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N<=K:
c = X*N
else:
c = X*K+Y*(N-K)
print(c)
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
if N<=K:
ans = X*N
else:
ans = X*K
ans += Y*(N-K)
print(ans)
|
p04011
|
n,k,x,y=eval('int(input()),'*4);print((n*x-(x-y)*max(n-k,0)))
|
n,k,x,y=list(map(int,open(0)));print((n*x-(x-y)*max(n-k,0)))
|
p04011
|
n=int(eval(input()))
k=int(eval(input()))
x=int(eval(input()))
y=int(eval(input()))
yen = 0
for i in range(n):
if i < k:
yen += x
else:
yen += y
print(yen)
|
n=int(eval(input()))
k=int(eval(input()))
x=int(eval(input()))
y=int(eval(input()))
if n <= k:
ans = n*x
else:
ans = k*x + (n-k)*y
print(ans)
|
p04011
|
# -*- coding: utf-8 -*-
# template: v1.2
import io
import sys
import math
def solve(n,k,x,y):
# implement process
higher = n-k
if higher < 1:
return n*x
else:
return k*x + higher*y
def main():
# input
n,k,x,y = list(map(int,sys.stdin.read().split()))
# process
ans = str( solve(n,k,x,y) )
# output
print(ans)
return ans
### DEBUG I/O ###
_DEB = 0 # 1:ON / 0:OFF
_INPUT = """\
2
3
10000
9000
"""
_EXPECTED = """\
20000
"""
def logd(str):
if _DEB: print(f"[deb] {str}")
### MAIN ###
if __name__ == "__main__":
if _DEB:
sys.stdin = io.StringIO(_INPUT)
print("!! Debug Mode !!")
ans = main()
if _DEB:
print()
if _EXPECTED.strip() == ans.strip(): print("!! Success !!")
else: print(f"!! Failed... !!\nANSWER: {ans}\nExpected: {_EXPECTED}")
|
import sys
n,k,x,y = list(map(int,sys.stdin.read().split()))
high=n-k
ans=0
if high > 0:
ans=x*k+high*y
else:
ans=n*x
print(ans)
|
p04011
|
a = [int(eval(input())) for i in range(4)]
if a[0] <= a[1]:
print((a[0] * a[2]))
else:
print((a[1] * a[2] + (a[0] - a[1]) * a[3]))
|
n,k,x,y= [int(eval(input())) for i in range(4)]
print((n * x + (y-x) * max(n-k,0)))
|
p04011
|
n = int(eval(input()))
k = int(eval(input()))
x = int(eval(input()))
y = int(eval(input()))
price = 0
for i in range(1, n + 1):
price += x if i <= k else y
print(price)
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
val = 0
for i in range(N):
val += X if i < K else Y
print(val)
|
p04011
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
cnt = 0
for i in range(N):
if(i < K):
cnt += X
else:
cnt += Y
print(cnt)
|
N = int(eval(input()))
K = int(eval(input()))
X = int(eval(input()))
Y = int(eval(input()))
print((min(K, N)*X + max(0,(N-K))*Y))
|
p04011
|
n = int(eval(input()))
k = int(eval(input()))
x = int(eval(input()))
y = int(eval(input()))
price = n*x if (n < k) else (k*x + (n-k)*y)
print(price)
|
n = int(eval(input()))
k = int(eval(input()))
x = int(eval(input()))
y = int(eval(input()))
v = n*x if (n <= k) else k*x + (n-k)*y
print(v)
|
p04011
|
a, b, c, d = [int(eval(input())) for i in range(4)]
if a-b > 0:
print((b*c + (a-b)*d))
else:
print((a*c))
|
n,k,x,y=(int(eval(input())) for i in range(4))
print((n*x-(x-y)*max(n-k, 0)))
|
p04011
|
import itertools
from copy import deepcopy
def turnOverRow(now_state,combs,c):
for i in combs:
for j in range(c):
now_state[i][j]=0 if now_state[i][j]==1 else 1
def turnOverCol(now_state,r,c):
h=r/2
for i in range(c):
cnt=0
for j in range(r):
if now_state[j][i]==1:
cnt+=1
if cnt>h:
for j in range(r):
now_state[j][i]=1 if now_state[j][i]==0 else 0
def countMax(now_state,r):
return sum(sum(now_state[i]) for i in range(r))
while True:
r,c=list(map(int,input().split()))
if r==c==0:break
init_state=[list(map(int,input().split())) for i in range(r)]
now_state=deepcopy(init_state)
ans=10000000000
for i in range(r+1):
for combs in itertools.combinations(list(range(r)),i):
turnOverRow(now_state,combs,c)
turnOverCol(now_state,r,c)
ans=min(ans,countMax(now_state,r))
now_state=deepcopy(init_state) #initialization
print(r*c-ans)
|
while True:
r,c=list(map(int,input().split()))
if r==c==0:break
state=[input().split() for i in range(r)]
state=list(map(list,list(zip(*state))))
new_state=[int("".join(ls),2) for ls in state]
ans=0
h=r/2
for i in range(pow(2,r)):
cnt=0
for j in new_state:
b=format(i^j,"b")
n=b.count("1")
cnt+=n if n>h else r-n
ans=max(ans,cnt)
print(ans)
|
p00448
|
while True:
r,c=list(map(int,input().split()))
if r==c==0:break
state=[input().split() for i in range(r)]
state=list(map(list,list(zip(*state))))
new_state=[int("".join(ls),2) for ls in state]
ans=0
h=r/2
for i in range(pow(2,r)):
cnt=0
for j in new_state:
b=format(i^j,"b")
n=b.count("1")
cnt+=n if n>h else r-n
ans=max(ans,cnt)
print(ans)
|
while True:
r,c=list(map(int,input().split()))
if r==c==0:break
state=[input().split() for i in range(r)]
state=list(map(list,list(zip(*state))))
new_state=[int("".join(ls),2) for ls in state]
ans=0
h=r/2
for i in range(pow(2,r)):
cnt=0
for j in new_state:
n=bin(i^j).count("1")
cnt+=n if n>h else r-n
ans=max(ans,cnt)
print(ans)
|
p00448
|
for e in iter(input,'0 0'):
r,_=list(map(int,e.split()))
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for i in range(1<<r):
b=0
for j in d:
c=bin(i^j).count('1')
b+=max(c,r-c)
a=max(a,b)
print(a)
|
for e in iter(input,'0 0'):
r,_=list(map(int,e.split()))
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for i in range(1<<r):
b=0
for j in d:
c=bin(i^j).count('1')
b+=[r-c,c][c>r//2]
if b>a:a=b
print(a)
|
p00448
|
for e in iter(input,'0 0'):
r,_=list(map(int,e.split()))
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for i in range(1<<r):
b=0
for j in d:
c=bin(i^j).count('1')
b+=[r-c,c][c>r//2]
if b>a:a=b
print(a)
|
for e in iter(input,'0 0'):
r,_=list(map(int,e.split()))
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for i in range(1<<r):
b=0
for j in d:
c=bin(i^j).count('1')
b+=c if c>r//2else r-c
a=max(a,b)
print(a)
|
p00448
|
import sys
e = sys.stdin.readlines()
while 1:
r=int(e[0].split()[0])
if r<1:break
d=[int(''.join(x),2)for x in zip(*[x.split() for x in e[1:r+1]])]
a=[]
for i in range(1<<r):
b=0
for j in d:
c=bin(i^j).count('1')
b+=c if c>r//2 else r-c
a+=[b]
print((max(a)))
del e[:r+1]
|
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
b=1<<r
f=[1]*b
for m in range(b):
if f[m]:
f[~m]=0
t=0
for s in d:
c=bin(m^s).count('1')
t+=c if c>r//2 else r-c
if a<t:a=t
print(a)
|
p00448
|
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
b=1<<r
f=[1]*b
for m in range(b):
if f[m]:
f[~m]=0
t=0
for s in d:
c=bin(m^s).count('1')
t+=c if c>r//2 else r-c
if a<t:a=t
print(a)
|
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for m in range(1<<~-r):
t=0
for s in d:
c=bin(m^s).count('1')
t+=c if c>r//2 else r-c
if a<t:a=t
print(a)
|
p00448
|
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for m in range(1<<~-r):
t=0
for s in d:
c=bin(m^s).count('1')
t+=c if c>r//2 else r-c
if a<t:a=t
print(a)
|
def v():
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for m in range(1<<~-r):
t=0
for s in d:
c=bin(m^s).count('1')
t+=c if c>r//2 else r-c
if a<t:a=t
print(a)
if'__main__'==__name__:v()
|
p00448
|
def v():
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for m in range(1<<~-r):
t=0
for s in d:
c=sum(map(int,bin(m^s)[2:]))
t+=c if c>r//2 else r-c
if a<t:a=t
print(a)
if'__main__'==__name__:v()
|
def v():
for e in iter(input,'0 0'):
r=int(e.split()[0])
d=[int(''.join(x),2)for x in zip(*[input().split()for _ in[0]*r])]
a=0
for m in range(1<<~-r):
t=0
for s in d:
c=bin(m^s).count('1')
t+=max(c,r-c)
if a<t:a=t
print(a)
if'__main__'==__name__:v()
|
p00448
|
def hantei(field,r,c):
count=0
for i in range(c):
countc=0
for j in range(r):
countc=countc+field[j][i]
count=max(countc,r-countc)+count
return count
def turnr(field,rn,r,c):
if rn==r:
return hantei(field,r,c)
field1=field
field2=field
field2[rn]=[not x for x in field[rn]]
return max(turnr(field1,rn+1,r,c),turnr(field2,rn+1,r,c))
while(1):
[r,c]=[int(x) for x in input().split()]
if r==0:
break
else:
field=[]
for i in range(r):
field.append([int(x) for x in input().split()])
print(turnr(field,0,r,c))
|
def hanteib(bfield,r,c):
count=0
for b in bfield:
ccount=bin(b).count("1")
count=count+max(ccount,r-ccount)
return count
def turnrb(bfield,rn,r,c):
if rn==r:
return hanteib(bfield,r,c)
bfield1=bfield
bfield2=[b ^ 2**rn for b in bfield]
return max(turnrb(bfield1,rn+1,r,c),turnrb(bfield2,rn+1,r,c))
while(1):
[r,c]=[int(x) for x in input().split()]
if r==0:
break
else:
field=[]
bfield=[]
for i in range(r):
field.append([int(x) for x in input().split()])
for j in range(c):
bb=0
for i in range(r):
bb=bb+2**i*field[i][j]
bfield.append(bb)
print(turnrb(bfield,0,r,c))
|
p00448
|
def main():
while True:
r,c = list(map(int,input().split()))
if not r and not c:
break
lst = []
for i in range(r):
lst.append(list(map(int,input().split())))
def dfs(l,n):
if n == r:
ans = 0
for i in range(c):
count = 0
for j in range(r):
if l[j][i]:
count += 1
ans += max(count, r - count)
return ans
else:
return max(dfs(l + [[0 if i else 1 for i in lst[n]]],n + 1),
dfs(l + [[1 if i else 0 for i in lst[n]]],n + 1))
print((dfs([],0)))
main()
|
while True:
r,c = list(map(int,input().split()))
if not r:
break
lst = [list(map(int,input().split())) for i in range(r)]
dic = [0 for i in range(2 ** r)]
for i in range(c):
num = 0
for j in range(r):
num *= 2
num += lst[j][i]
dic[num] += 1
ans = 0
for i in range(2 ** r):
ret = 0
for j in range(2 ** r):
num = (i ^ j)
cnt = 0
for k in range(r):
cnt += num % 2
num //= 2
ret += max(cnt, r - cnt) * dic[j]
ans = max(ans, ret)
print(ans)
|
p00448
|
while True:
r,c = list(map(int,input().split()))
if not r:
break
lst = [list(map(int,input().split())) for i in range(r)]
dic = [0 for i in range(2 ** r)]
for i in range(c):
num = 0
for j in range(r):
num *= 2
num += lst[j][i]
dic[num] += 1
ans = 0
for i in range(2 ** r):
ret = 0
for j in range(2 ** r):
num = (i ^ j)
cnt = 0
for k in range(r):
cnt += num % 2
num //= 2
ret += max(cnt, r - cnt) * dic[j]
ans = max(ans, ret)
print(ans)
|
def f():
while True:
r,c = list(map(int,input().split()))
if not r:
break
lst = [list(map(int,input().split())) for i in range(r)]
dic = [0 for i in range(2 ** r)]
for i in range(c):
num = 0
for j in range(r):
num *= 2
num += lst[j][i]
dic[num] += 1
ans = 0
for i in range(2 ** r):
ret = 0
for j in range(2 ** r):
num = (i ^ j)
cnt = 0
for k in range(r):
cnt += num % 2
num //= 2
ret += max(cnt, r - cnt) * dic[j]
ans = max(ans, ret)
print(ans)
f()
|
p00448
|
import itertools
while 1:
R,C = list(map(int,input().split()))
if R == 0: break
A = [list(map(int,input().split())) for i in range(R)]
ans = 0
for n in range(1,R+1):
for flip in itertools.combinations(list(range(R)),n):
cur = 0
for ci in range(C):
s = sum((A[ri][ci] if ri not in flip else 1 - A[ri][ci]) for ri in range(R))
cur += max(s,R-s)
ans = max(ans,cur)
print(ans)
|
def solve(A,flip):
res = 0
if len(flip) != H:
res = max(res,solve(A,flip+[1]),solve(A,flip+[0]))
else:
B = [A[i][:] for i in range(H)]
for h in range(H):
if flip[h]:
B[h] = [1-val for val in B[h]]
for w in range(W):
good = sum(1-B[h][w] for h in range(H))
res += max(good,H-good)
return res
while 1:
H,W = list(map(int,input().split()))
if H == W == 0:
break
A = [list(map(int,input().split())) for h in range(H)]
print(solve(A,[]))
|
p00448
|
while 1:
r, c = list(map(int, input().split()))
if r == 0: break
old = [None for i in range(r)]
new = [[True]*c for i in range(r)]
for i in range(r):
old[i] = "".join(map(str, input().split()))
ans = 0
n = 1<<r
for i in range(n):
for j in range(r):
if i & (1<<j):
for k in range(c):
new[j][k] = bool(int(old[j][k])) ^ True
else:
for k in range(c):
new[j][k] = bool(int(old[j][k]))
ret = 0
for j in range(c):
cnt = 0
for k in range(r):
if not new[k][j]: cnt+=1
ret += cnt if cnt > r/2 else r-cnt
ans = max(ans, ret)
print(ans)
|
while 1:
r, c = list(map(int, input().split()))
if r == 0: break
old = [input().split() for i in range(r)]
old = list(map(list, list(zip(*old))))
new = [int("".join(lst),2) for lst in old]
ans = 0
for i in range(1<<r):
ret = 0
for j in new:
cnt = bin(i^j).count('1')
ret += cnt if cnt > r/2 else r-cnt
ans = max(ans, ret)
print(ans)
|
p00448
|
import copy
def culc(k):
cnt_0=0
cnt_1=0
for i in range(r):
if osenbei1[i][k]==1:
cnt_1+=1
else:
cnt_0+=1
return max(cnt_0,cnt_1)
def rev(j):
for i in range(c):
if osenbei1[j][i]==1:
osenbei1[j][i]=0
else:
osenbei1[j][i]=1
while 1:
r,c=list(map(int,input().split()))
if r==c==0:break
osenbei=[list(map(int,input().split())) for _ in range(r)]
r_cnt=[[0]*2 for _ in range(r)]
c_cnt=[[0]*2 for _ in range(c)]
dp=[[0]*(c+1) for _ in range(1<<r)]
for i in range(1<<r):
osenbei1=copy.deepcopy(osenbei)
for j in range(r):
if i&(1<<j):
rev(j)
for k in range(c):
dp[i][k+1]=dp[i][k]+culc(k)
ans=-1
for i in range(1<<r):
ans=max(ans,max(dp[i]))
print(ans)
|
while 1:
r,c=list(map(int,input().split()))
if r==c==0:break
osenbei=[list(map(int,input().split())) for _ in range(r)]
r_osenbei=[0]*c
for i in range(c):
st=""
for j in range(r):
st+=str(osenbei[j][i])
r_osenbei[i]=int(st,2)
ans=-1
for i in range(1<<r):
cnt=0
for j in range(c):
n=bin(i^r_osenbei[j]).count('1')
cnt+=max(n,r-n)
ans=max(ans,cnt)
print(ans)
|
p00448
|
while 1:
n=eval(input())
if n==0:break
t=[list(map(int,input().split())) for i in [1]*n]
t=sorted(sorted(sorted(t),key=lambda x:x[3])[::-1],key=lambda x:x[2])[::-1]
u=[0 for i in range(1001)]
s=0
for i in t:
if (s<10 and u[i[1]]<3) or (s<20 and u[i[1]]<2) or (s<26 and u[i[1]]<1):
print(i[0])
u[i[1]]+=1
s+=1
|
while 1:
n=eval(input())
if n==0:break
t=[list(map(int,input().split())) for i in [1]*n]
t=sorted(sorted(sorted(t),key=lambda x:x[3])[::-1],key=lambda x:x[2])[::-1]
u=[0]*1001
s=0
for i in t:
if (s<10 and u[i[1]]<3) or (s<20 and u[i[1]]<2) or (s<26 and u[i[1]]<1):
print(i[0])
u[i[1]]+=1
s+=1
|
p00629
|
# AOJ 1043: Selecting Teams Advanced to Regional
# Python3 2018.7.6 bal4u
def pr(i, u):
global total
print((team[i][0]))
total, affi[u] = total+1, affi[u]+1
while True:
n = int(eval(input()))
if n == 0: break
team = []
for i in range(n):
id, u, a, p = list(map(int, input().split()))
team.append((id, u, a, p))
team.sort(key=lambda x:(-x[2],x[3],x[0]))
total, affi = 0, [0]*1002
for i in range(n):
u = team[i][1]
if total < 10 and affi[u] < 3: pr(i, u)
elif total < 20 and affi[u] < 2: pr(i, u)
elif total < 26 and affi[u] < 1: pr(i, u)
|
# AOJ 1043: Selecting Teams Advanced to Regional
# Python3 2018.7.6 bal4u
def pr(i, u):
global total
print((team[i][0]))
total, affi[u] = total + 1, affi[u] + 1
while True:
n = int(eval(input()))
if n == 0: break
team = [list(map(int, input().split())) for i in range(n)]
team.sort(key=lambda x: (-x[2], x[3], x[0]))
total, affi = 0, [0] * 1002
for i in range(n):
u = team[i][1]
if total < 10 and affi[u] < 3: pr(i, u)
elif total < 20 and affi[u] < 2: pr(i, u)
elif total < 26 and affi[u] < 1: pr(i, u)
|
p00629
|
D = {"A":"T","T":"A","C":"G","G":"C"}
b = input().strip()
x = ""
for i in range(len(b)):
x += D[b[i]]
print(x)
|
b = input().strip()
if b=="A":
print("T")
elif b=="T":
print("A")
elif b=="C":
print("G")
else:
print("C")
|
p03085
|
'''
Doubling
nxt[i][r] = the value of r mod M after 2 ** i squaring
dp[i][r] = the sum of sequence of lenght 2 ** i starting from r
We split sequence A into multiple disjoint parts,
where the lengths of each parts are the binary decomposition of N.
e.g.
N = 11 = 1 + 2 + 8
A = A[0:1], A[1:3], A[3:11]
Sum of each parts are:
sum(A[0:1]) = dp[0][A[0] % M]
sum(A[1:3]) = dp[1][A[1] % M]
sum(A[3:11]) = dp[3][A[3] % M]
'''
import math
N, X, M = list(map(int, input().split()))
H = math.ceil(math.log2(N)) + 1
nxt = [[0] * M for _ in range(H)]
dp = [[0] * M for _ in range(H)]
nxt[0] = [r ** 2 % M for r in range(M)]
dp[0] = list(range(M))
for i in range(1, H):
for r in range(M):
nxt[i][r] = nxt[i - 1][nxt[i - 1][r]]
dp[i][r] = dp[i - 1][r] + dp[i - 1][nxt[i - 1][r]]
ans = 0
r = X % M
for i in range(H):
if N & (1 << i):
ans += dp[i][r]
r = nxt[i][r]
print(ans)
|
import math
def solve():
N, X, M = list(map(int, input().split()))
if N == 1:
return X % M
A = [X]
vis = [-1] * M
vis[X] = 0
for i in range(1, M + 1):
X = X ** 2 % M
if vis[X] != -1:
pref = A[:vis[X]]
cycle = A[vis[X]:]
break
A.append(X)
vis[X] = i
n_full_period = (N - len(pref)) // len(cycle)
suffix_length = N - len(pref) - n_full_period * len(cycle)
ans = 0
ans += sum(pref)
ans += sum(cycle) * n_full_period
ans += sum(cycle[:suffix_length])
return ans
print((solve()))
|
p02550
|
N, X, M = list(map(int, input().split()))
Visited = [0]*(M+1)
Mods = [0]*(M+1)
count = 0
for i in range (0, M):
if (i**2)%M == i:
Mods[i]=1
latest = 0
for i in range (0, N):
if X in Mods:
count+=((N-i)*X)
for j in range (0, M+1):
count+=(j*Visited[j])
print(count)
exit()
if Visited[X] > 0:
latest = X
break
Visited[X]+= 1
X = (X**2)%M
P = sum(Visited)
for j in range (0, M+1):
count+=(j*Visited[j])
Visited = [0]*(M+1)
if P == N:
print(count)
exit()
crouch = [0]
looper = 0
debu = 0
for i in range (0, N-P):
if Visited[X] > 0:
looper = i
debu = 1
break
else:
crouch.append(X+crouch[-1])
Visited[X]+= 1
X = (X**2)%M
if debu == 0:
for j in range (0, M+1):
count+=(j*Visited[j])
print(count)
exit()
else:
loops = 0
for j in range (0, M+1):
loops+=(j*Visited[j])
count+=(loops*((N-P)//looper))
count+=crouch[(N-P)%looper]
print(count)
|
N, X, M = list(map(int, input().split()))
Visited = [0]*(M+1)
latestvisit = [0]*(M+1)
count = 0
latest = 0
proper = 0
for i in range (0, N):
if Visited[X] > 0:
latest = X
latestvisit[X] = i
proper = 1
break
latestvisit[X] = i
Visited[X]+= 1
X = (X**2)%M
for j in range (0, M+1):
count+=(j*Visited[j])
if proper == 0:
print(count)
exit()
P = sum(Visited)
A = latestvisit[X]
B = latestvisit[(X**2)%M]
slouch = 0
for i in range (0, M):
if latestvisit[i] >=B and latestvisit[i] <= A:
slouch+=i
count+=((N-P)//(A-B+1))*slouch
for i in range (0, (N-P)%(A-B+1)):
count+=X
X = (X**2)%M
print(count)
|
p02550
|
N, X, M = list(map(int, input().split()))
ans = X
ALL = []
rou = False
for i in range(N-1):
X = pow(X, 2, M)
if X in ALL:
num = ALL.index(X)
now = i
rou = True
break
ALL.append(X)
ans += X
if rou :
roupe = now - num
nokori = N - now -2
print((sum(ALL[num:])*(nokori//roupe) + ans + sum(ALL[num:num + nokori%roupe + 1])))
else:
print(ans)
|
N, X, M = list(map(int, input().split()))
ans = X
ALL_cal = [False] * M
ALL = []
rou = False
for i in range(N-1):
X = pow(X, 2, M)
if ALL_cal[X]:
num = ALL_cal[X]
now = i
rou = True
break
ALL.append(X)
ALL_cal[X] = i
ans += X
if rou :
roupe = now - num
nokori = N - now - 1
print((sum(ALL[num:])*(nokori//roupe) + ans + sum(ALL[num:num + nokori%roupe])))
else:
print(ans)
|
p02550
|
N, X, M = list(map(int,input().split()))
ans = 0
ls = [X%M] + [-1]*(M+1)
for i in range(N):
a = ls[i]
ls[i+1] = pow(a,2,M)
if i+1 < N and ls[i+1] in ls[:i]:
b = ls[i+1]
L = ls.index(b)
R = i
len_roop = R - L + 1
p = (N - L) // len_roop
q = (N - L) % len_roop
break
else:
for i in range(N):
ans += ls[i]
print(ans)
exit()
sum_roop = 0
for i in range(L, R+1):
sum_roop += ls[i]
sum_edge = 0
for i in range(L, L+q):
sum_edge += ls[i]
for i in range(L):
ans += ls[i]
else:
ans += sum_roop * p + sum_edge
print(ans)
|
N, X, M = list(map(int,input().split()))
ans = 0
ls = [X%M] + [-1]*(M+1)
ls_app = [0]*(M+1)
for i in range(N):
a = ls[i]
ls_app[a] = i
ls[i+1] = pow(a,2,M)
if i+1 < N and ls_app[ls[i+1]] != 0:
b = ls[i+1]
L = ls.index(b)
R = i
len_roop = R - L + 1
p = (N - L) // len_roop
q = (N - L) % len_roop
break
else:
for i in range(N):
ans += ls[i]
print(ans)
exit()
sum_roop = 0
for i in range(L, R+1):
sum_roop += ls[i]
sum_edge = 0
for i in range(L, L+q):
sum_edge += ls[i]
for i in range(L):
ans += ls[i]
else:
ans += sum_roop * p + sum_edge
print(ans)
|
p02550
|
n,x,m=list(map(int,input().split()))
f=1
w=[]
d=[0]*(m+2)
t=[0]
chk=n
for i in range(m+1):
f=pow(2,i)
p=pow(x,f,m)
if d[p]: break
d[p]=1
w.append(p)
t.append(t[-1]+p)
#f*=2
chk-=1
if chk==0:
print((t[-1]))
exit()
ans=0
a=w.index(p)
ans+=t[a]
n-=a
loo=len(w)-a
if n%loo==0:
ans+=(t[-1]-t[a])*(n//loo)
else:
ans+=(t[-1]-t[a])*(n//loo)
ans+=t[a+n%loo]-t[a]
print(ans)
|
n,x,m=list(map(int,input().split()))
f=1
w=[]
d=[0]*(m+2)
t=[0]
chk=n
p=x
for i in range(m+1):
if i!=0: p=p*p%m
if d[p]: break
d[p]=1
w.append(p)
t.append(t[-1]+p)
#f*=2
chk-=1
if chk==0:
print((t[-1]))
exit()
ans=0
a=w.index(p)
ans+=t[a]
n-=a
loo=len(w)-a
if n%loo==0:
ans+=(t[-1]-t[a])*(n//loo)
else:
ans+=(t[-1]-t[a])*(n//loo)
ans+=t[a+n%loo]-t[a]
print(ans)
|
p02550
|
n,x,m=list(map(int,input().split()))
l=[-1]*m
s=[0]*m
t=p=0
while l[x]<0:
t+=1
l[x]=t
s[x]=s[p]+x
p=x
x=pow(p,2,m)
T=t+1-l[x]
S=s[p]+x-s[x]
if n<l[x]:
print((s[l.index(n)]))
else:
print((S*((n-l[x])//T)+s[l.index(l[x]+(n-l[x])%T)]))
|
n,x,m=list(map(int,input().split()))
l=[0]*m
s=[0]*m
t=p=0
while l[x]<1:
t+=1
l[x]=t
s[x]=s[p]+x
p=x
x=pow(p,2,m)
T=t+1-l[x]
S=s[p]+x-s[x]
print((S*((n-l[x])//T)+s[l.index(l[x]+(n-l[x])%T)]))
|
p02550
|
n,x,m=list(map(int,input().split()))
l=[0]*m
s=[0]*m
t=p=0
while l[x]<1:
t+=1
l[x]=t
s[x]=s[p]+x
p=x
x=pow(p,2,m)
T=t+1-l[x]
S=s[p]+x-s[x]
d,m=divmod(n-l[x],T)
print((S*d+s[l.index(l[x]+m)]))
|
n,x,m=list(map(int,input().split()))
l,s=[0]*m,[0]*m
t=p=0
while l[x]<1:
t+=1
l[x]=t
s[x]=s[p]+x
p=x
x=p*p%m
k=l[x]
d,m=divmod(n-k,t+1-k)
print(((s[p]+x-s[x])*d+s[l.index(k+m)]))
|
p02550
|
n, x, m = list(map(int, input().split()))
A = [x]
ans = x
for i in range(1,n):
buf = A[i-1]*A[i-1] % m
if buf in A:
idx = A.index(buf)
back = len(A) - idx
nokori = n-len(A)
sumKurikaeshi = sum(A[idx:])
ans += int(nokori/back)*sumKurikaeshi
last = nokori%back
ans += sum(A[idx:idx+last])
break
else:
A.append(buf)
ans += buf
print(ans)
|
n, x, m = list(map(int, input().split()))
A = [x]
checkA = {x}
ans = x
for i in range(1,n):
buf = A[i-1]*A[i-1] % m
if buf in checkA:
idx = A.index(buf)
back = len(A) - idx
nokori = n-len(A)
sumKurikaeshi = sum(A[idx:])
ans += int(nokori/back)*sumKurikaeshi
last = nokori%back
ans += sum(A[idx:idx+last])
break
else:
A.append(buf)
checkA.add(buf)
ans += buf
print(ans)
|
p02550
|
N, X, M = list(map(int,input().split()))
A = [X]
Lstart = 0
for i in range(N):
An = A[-1]**2 % M
if An in A:
Lstart = A.index(An)
break
else:
A.append(An)
LenBefL = Lstart
LenLoop = len(A) - Lstart
if LenLoop > 0:
ans = sum(A[:Lstart])+sum(A[Lstart:])*((N-LenBefL)//LenLoop) + sum(A[Lstart:Lstart + (N-LenBefL)%LenLoop])
else:
ans = sum(A)
print(ans)
|
N, X, M = list(map(int,input().split()))
A = [X]
As = set(A)
Lstart = 0
for i in range(N):
An = A[-1]**2 % M
if An in As:
Lstart = A.index(An)
break
else:
A.append(An)
As.add(An)
LenBefL = Lstart
LenLoop = len(A) - Lstart
if LenLoop > 0:
ans = sum(A[:Lstart])+sum(A[Lstart:])*((N-LenBefL)//LenLoop) + sum(A[Lstart:Lstart + (N-LenBefL)%LenLoop])
else:
ans = sum(A)
print(ans)
|
p02550
|
# -*- coding: utf-8 -*-
N,X,M=list(map(int,input().split()))
A=[X]
s=N
for i in range(1,N):
a=A[i-1]**2%M
if a in A:
s=A.index(a)
break
else:
A.append(a)
if len(A)==N:
ans=sum(A)
else:
Z=[0]*(len(A)+1)
for i in range(1,len(A)+1):
Z[i]=Z[i-1]+A[i-1]
ans=0
l=len(A)-s
#ans+=sum(A[:s])
ans+=Z[s]
#S=sum(A[s:])
S=Z[len(A)]-Z[s]
T=(N-s)//l
ans+=T*S
K=N-s-l*T
#ans+=sum(A[s:(s+K)])
ans+=Z[s+K]-Z[s]
print(ans)
|
# -*- coding: utf-8 -*-
N,X,M=list(map(int,input().split()))
A=[0]*10**5
A[0]=X
D=dict()
#A=[X]
s=N
for i in range(1,N):
a=A[i-1]**2%M
if a in D:
s=A.index(a)
break
else:
#A.append(a)
A[i]=a
D[a]=0
if s==N:
ans=sum(A)
else:
A=A[:i]
Z=[0]*(len(A)+1)
for i in range(1,len(A)+1):
Z[i]=Z[i-1]+A[i-1]
ans=0
l=len(A)-s
#ans+=sum(A[:s])
ans+=Z[s]
#S=sum(A[s:])
S=Z[len(A)]-Z[s]
T=(N-s)//l
ans+=T*S
K=N-s-l*T
#ans+=sum(A[s:(s+K)])
ans+=Z[s+K]-Z[s]
print(ans)
|
p02550
|
# -*- coding: utf-8 -*-
N,X,M=list(map(int,input().split()))
A=[0]*10**5
A[0]=X
D=dict()
#A=[X]
s=N
for i in range(1,N):
a=A[i-1]**2%M
if a in D:
s=A.index(a)
break
else:
#A.append(a)
A[i]=a
D[a]=0
if s==N:
ans=sum(A)
else:
A=A[:i]
Z=[0]*(len(A)+1)
for i in range(1,len(A)+1):
Z[i]=Z[i-1]+A[i-1]
ans=0
l=len(A)-s
#ans+=sum(A[:s])
ans+=Z[s]
#S=sum(A[s:])
S=Z[len(A)]-Z[s]
T=(N-s)//l
ans+=T*S
K=N-s-l*T
#ans+=sum(A[s:(s+K)])
ans+=Z[s+K]-Z[s]
print(ans)
|
# -*- coding: utf-8 -*-
N,X,M=list(map(int,input().split()))
A=[0]*(M+1)
A[0]=X
D=[0]*(M+1)
s=N
for i in range(1,N):
a=A[i-1]**2%M
if D[a] == 1:
s=A.index(a)
break
else:
A[i]=a
D[a]=1
if s==N:
ans=sum(A)
else:
A=A[:i]
ans=0
l=len(A)-s
ans+=sum(A[:s])
S=sum(A[s:])
T=(N-s)//l
ans+=T*S
K=N-s-l*T
ans+=sum(A[s:(s+K)])
print(ans)
|
p02550
|
# coding: utf-8
def solve(*args: str) -> str:
n, x, m = list(map(int, args[0].split()))
a = x
ret = 0
D = [0]*(m+1)
for i in range(n):
if 0 < D[a]:
j = a
L = []
while D[j] != a:
L.append(j)
j = D[j]
L.append(j)
d, r = divmod(n-i-1, len(L))
ret += d*sum(L)+sum(L[:r+1])
break
ret += a
p = a
a = pow(a, 2, m)
D[p] = a
return str(ret)
if __name__ == "__main__":
print((solve(*(open(0).read().splitlines()))))
|
# coding: utf-8
def solve(*args: str) -> str:
n, x, m = list(map(int, args[0].split()))
a = x
ret = 0
D = [-1]*(m+1)
for i in range(n):
if 0 <= D[a]:
j = a
L = []
while D[j] != a:
L.append(j)
j = D[j]
L.append(j)
d, r = divmod(n-i-1, len(L))
ret += d*sum(L)+sum(L[:r+1])
break
ret += a
p = a
a = pow(a, 2, m)
D[p] = a
return str(ret)
if __name__ == "__main__":
print((solve(*(open(0).read().splitlines()))))
|
p02550
|
n, x, m = list(map(int, input().split()))
amr = []
for i in range(n):
if i == 0:
x %= m
amr.append(x)
continue
x = pow(x, 2, m)
if x in amr:
break
if x == 0:
break
amr.append(x)
if x == 0:
print((sum(amr[:n])))
else:
idx = amr.index(x)
loop = sum(amr[idx:])
l = len(amr[idx:])
if n >= idx:
cnt = (n-idx)//l
print((cnt*loop + sum(amr[:idx]) + sum(amr[idx:idx+n-idx-cnt*l:])))
else:
print((sum(amr[:n])))
|
n, x, m = list(map(int, input().split()))
A = []
al = [-1]*(m+1)
cnt = 0
for i in range(n):
if al[x%m] >= 0:
break
al[x%m] = cnt
A.append(x)
cnt += 1
x = x**2 % m
if n <= al[x]:
print((sum(A[:al[x]])))
exit()
lena = len(A)
loop = A[al[x]:]
lenl = cnt-al[x]
n -= al[x]
cnt = n//lenl
print((sum(A[:al[x]]) + sum(loop)*cnt + sum(loop[:n%lenl])))
|
p02550
|
n, x, m = list(map(int, input().split()))
g = x * 1
ans = x
arr = []
res = set([])
roop = (0, 0)
k = 0
for i in range(m + 1):
tmp = x ** 2 % m
if tmp in res:
roop = (i, tmp)
break
res.add(tmp)
arr.append(tmp)
ans += tmp
x = tmp
for i, y in enumerate(arr):
if y == roop[1]:
k = i
break
ini = sum(arr[:k])
mul = ans - ini - g
t, v = divmod(n - k - 1, roop[0] - k)
print((g + ini + mul * t + sum(arr[k:k + v])))
|
n, x, m = list(map(int, input().split()))
g = x * 1
ans = x
arr = []
res = set([])
loop = (0, 0)
k = 0
for i in range(m + 1):
tmp = x ** 2 % m
if tmp in res:
loop = (i, tmp)
break
res.add(tmp)
arr.append(tmp)
ans += tmp
x = tmp
for i, y in enumerate(arr):
if y == loop[1]:
k = i
break
ini = g + sum(arr[:k])
mul = ans - ini
t, v = divmod(n - k - 1, loop[0] - k)
print((ini + mul * t + sum(arr[k:k + v])))
|
p02550
|
n, x, m = list(map(int, input().split()))
lst = set()
num = []
for i in range(1, n + 1):
lst.add(x), num.append(x)
x = x ** 2 % m
if x in lst:
cnt, idx = i, num.index(x)
div, mod = divmod(n - cnt, len(num) - idx)
ans = sum(num[:idx + mod])
ans += sum(num[idx:]) * (div + 1)
break
else:
ans = sum(num)
print(ans)
|
n, x, m = list(map(int, input().split()))
lst, num = set(), []
for i in range(1, n + 1):
lst.add(x), num.append(x)
x = x ** 2 % m
if x in lst:
break
if x in lst:
cnt, idx = i, num.index(x)
div, mod = divmod(n - cnt, len(num) - idx)
ans = sum(num[:idx + mod])
ans += sum(num[idx:]) * (div + 1)
else:
ans = sum(num)
print(ans)
|
p02550
|
n, x, m = list(map(int, input().split()))
lst, num = set(), []
for i in range(1, n + 1):
lst.add(x), num.append(x)
x = x ** 2 % m
if x in lst:
break
ans = sum(num)
if x in lst:
cnt, idx = i, num.index(x)
div, mod = divmod(n - cnt, len(num) - idx)
ans += sum(num[idx:idx + mod])
ans += sum(num[idx:]) * div
print(ans)
|
n, x, m = list(map(int, input().split()))
lst, num = set(), []
for i in range(n):
lst.add(x), num.append(x)
x = x ** 2 % m
if x in lst:
cnt, idx = i + 1, num.index(x)
break
ans = sum(num)
if x in lst:
div, mod = divmod(n - cnt, len(num) - idx)
ans += sum(num[idx:]) * div
ans += sum(num[idx:idx + mod])
print(ans)
|
p02550
|
n, x, m = list(map(int, input().split()))
num, lst = set(), []
for i in range(n):
num.add(x), lst.append(x)
x = x ** 2 % m
if x in lst:
cnt, idx = i + 1, lst.index(x)
break
ans = sum(num)
if x in lst:
div, mod = divmod(n - cnt, len(lst) - idx)
ans += sum(lst[idx:]) * div
ans += sum(lst[idx:idx + mod])
print(ans)
|
n, x, m = list(map(int, input().split()))
num, lst = set(), []
for i in range(n):
num.add(x), lst.append(x)
x = x ** 2 % m
if x in num:
cnt, idx = i + 1, lst.index(x)
break
ans = sum(num)
if x in num:
div, mod = divmod(n - cnt, len(lst) - idx)
ans += sum(lst[idx:]) * div
ans += sum(lst[idx:idx + mod])
print(ans)
|
p02550
|
from collections import defaultdict
N, X, M = list(map(int, input().split()))
A = [X]
visited = set()
visited.add(X)
idx = defaultdict()
idx[X] = 0
iii = -1
for i in range(1, M):
tmp = (A[-1]**2) % M
if tmp not in visited:
A.append(tmp)
visited.add(tmp)
idx[tmp] = i
else:
iii = idx[tmp]
ans = 0
# ループの頭の直前まで
ans += sum(A[:iii])
N -= iii
if N > 0:
# ループの長さ
l = len(A) - iii
ans += (N // l) * sum(A[iii:])
N -= N // l * l
if N > 0:
# ループに満たないN
ans += sum(A[iii:iii + N])
print(ans)
exit()
|
from collections import defaultdict
N, X, M = list(map(int, input().split()))
A = [X]
visited = set()
visited.add(X)
idx = defaultdict()
idx[X] = 0
iii = -1
for i in range(1, M):
tmp = (A[-1]**2) % M
if tmp not in visited:
A.append(tmp)
visited.add(tmp)
idx[tmp] = i
else:
iii = idx[tmp]
ans = 0
if iii == -1:
ans = sum(A[:N])
print(ans)
exit()
else:
# ループの頭の直前まで
ans += sum(A[:iii])
N -= iii
if N > 0:
# ループの長さ
l = len(A) - iii
ans += (N // l) * sum(A[iii:])
N -= N // l * l
if N > 0:
# ループに満たないN
ans += sum(A[iii:iii + N])
print(ans)
|
p02550
|
#ダブリング解
n, x, m = list(map(int, input().split()))
tmp = [None]*m
for i in range(m):
tmp[i] = i*i%m
pos = [tmp]
accu = [tmp]
for i in range(40):
tmp1 = [None]*m
tmp2 = [None]*m
for j in range(m):
tmp1[j] = pos[-1][pos[-1][j]]
tmp2[j] = accu[-1][pos[-1][j]] + accu[-1][j]
pos.append(tmp1)
accu.append(tmp2)
now = x
ans = x
for i in range(40):
if (n-1)>>i&1:
ans += accu[i][now]
now = pos[i][now]
print(ans)
|
n, x, m = list(map(int, input().split()))
li = [x]
seen = [False]*m
seen[x] = True
for i in range(n-1):
x = x*x%m
if seen[x]:
idx = li.index(x)
break
seen[x] = True
li.append(x)
else:
print((sum(li)))
exit(0)
ans = sum(li) + sum(li[idx:])*((n-len(li)) // (len(li)-idx)) + sum(li[idx:idx+(n-len(li)) % (len(li)-idx)])
print(ans)
|
p02550
|
N, X, M = list(map(int, input().split()))
check = [0]*(M+1)
check[X] += 1
A = X
last_count = 0
while True:
A = (A**2)%M
if check[A] != 0:
last_count = sum(check)
target_value = A
break
check[A] += 1
A2 = X
first_count = 0
while True:
first_count += 1
A2 = (A2**2)%M
if A2 == target_value:
break
roop_count = last_count-first_count
A3 = target_value
mass = A3
for i in range(roop_count-1):
A3 = (A3**2)%M
mass += A3
if roop_count == 0:
A = X
ans = X
for i in range(N-1):
A = (A**2)%M
ans += A
print(ans)
exit()
ans = X
A = X
for i in range(min(N,first_count)-1):
A = (A**2)%M
ans += A
if N > first_count:
ans += ((N-first_count)//roop_count)*mass
A4 = target_value
if (N-first_count)%roop_count >= 1:
ans += A4
for i in range(((N-first_count)%roop_count)-1):
A4 = (A4**2)%M
ans += A4
print(ans)
|
N, X, M = list(map(int, input().split()))
if X <= 1:
print((X*N))
exit()
check = [0]*(M+1)
check[X] += 1
A = X
last_count = 0
while True:
A = (A**2)%M
if check[A] != 0:
last_count = sum(check)
target_value = A
break
check[A] += 1
A2 = X
first_count = 0
while A2 != target_value:
first_count += 1
A2 = (A2**2)%M
roop_count = last_count-first_count
A3 = target_value
mass = A3
for i in range(roop_count-1):
A3 = (A3**2)%M
mass += A3
if roop_count == 0:
print((N*X))
exit()
if first_count != 0:
ans = X
A = X
for i in range(min(N,first_count)-1):
A = (A**2)%M
ans += A
else:
ans = 0
if N > first_count:
ans += ((N-first_count)//roop_count)*mass
A4 = target_value
if (N-first_count)%roop_count >= 1:
ans += A4
for i in range(((N-first_count)%roop_count)-1):
A4 = (A4**2)%M
ans += A4
print(ans)
|
p02550
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.