input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
n, x, m = list(map(int, input().split()))
arr = [x]
mod = x
def find_value(lst, value):
return [i for i, x in enumerate(lst) if x == value]
ans = 0
for i in range(n-1):
mod = mod ** 2 % m
res = find_value(arr, mod)
if res:
j = res[0]
a = i + 1 - j
q, mod = divm... | n, x, m = list(map(int, input().split()))
arr = [x]
mod = x
memo = [-1 for _ in range(m+1)]
ans = 0
for i in range(1, n):
mod = mod ** 2 % m
idx = memo[mod]
if idx != -1:
a = i - idx
q, _mod = divmod((n - idx), a)
ans = sum(arr[0:idx]) + q * sum(arr[idx:idx+a]) + sum... | p02550 |
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with... | # -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with... | p02550 |
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with... | # -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with... | p02550 |
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with... | # -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from fractions import Fraction
from functools import lru_cache, reduce
from itertools import combinations, combinations_with... | p02550 |
n,x,m = list(map(int, input().split()))
amari = [x]
cnt = 0
p = 0
for i in range(n-1):
a = amari[-1]**2 % m
if a in amari:
p = amari.index(a)
cnt = i+1-p
break
else:
amari.append(a)
if cnt == 0:
print((sum(amari)))
else:
n -= len(amari)
a = n // cnt
b = n % cnt
print((sum(amar... | n,x,m = list(map(int, input().split()))
amari = [x]
amariset = {x}
cnt = 0
p = 0
for i in range(n-1):
a = amari[-1]**2 % m
if a in amariset:
p = amari.index(a)
cnt = i+1-p
break
else:
amari.append(a)
amariset.add(a)
if cnt == 0:
print((sum(amari)))
else:
n -= len(amari)
a = n ... | p02550 |
N, X, M = list(map(int, input().split()))
used = [0] * M
a = []
A = X
for i in range(N):
if used[A] != 0:
break
used[A] = 1
a.append(A)
A = A * A % M
for i in range(len(a)):
if a[i] == A:
break
j = i
if a[-1] != 0:
result = sum(a[:j])
a = a[j:]
l ... | N, X, M = list(map(int, input().split()))
existence = [0] * M
a = []
A = X
for i in range(N):
if existence[A] != 0:
break
existence[A] = 1
a.append(A)
A = A * A % M
for i in range(len(a)):
if a[i] == A:
break
j = i
result = sum(a[:j])
a = a[j:]
N -= j
x = N // ... | p02550 |
N, X, M = list(map(int, input().split()))
existence = [False] * M
a = []
A = X
for i in range(N):
if existence[A]:
break
existence[A] = True
a.append(A)
A = A * A % M
for i in range(len(a)):
if a[i] == A:
break
loop_start = i
result = sum(a[:loop_start])
a = a[lo... | N, X, M = list(map(int, input().split()))
existence = [False] * M
a = []
A = X
for i in range(N):
if existence[A]:
break
existence[A] = True
a.append(A)
A = A * A % M
for i in range(len(a)):
if a[i] == A:
loop_start = i
break
else:
loop_start = len(a)
... | p02550 |
N, X, M = list(map(int, input().split()))
existence = [False] * M
a = []
A = X
for i in range(N):
if existence[A]:
break
existence[A] = True
a.append(A)
A = A * A % M
for i in range(len(a)):
if a[i] == A:
loop_start = i
break
else:
loop_start = len(a)
... | N, X, M = list(map(int, input().split()))
existence = [False] * M
a = []
A = X
for i in range(N):
if existence[A]:
break
existence[A] = True
a.append(A)
A = A * A % M
try:
loop_start = a.index(A)
except:
loop_start = len(a)
result = sum(a[:loop_start])
N -= loop_star... | p02550 |
n,x,m = list(map(int, input().split()))
ans = 0
x_list = []
for _ in range(n):
x_list.append(x)
ans += x
x = x**2 % m
if x == 0:
ans = sum(x_list)
break
if x == 1:
ans = sum(x_list) + (n-len(x_list))
break
if x_list.count(x) == 1:
after_li... | N, X, M = list(map(int, input().split()))
def next_f(a):
return a * a % M
visited = [None] * M
sum_at_visit = [None] * M
loop_done = False
time = 1
ans = 0
x = X
while time <= N:
if not loop_done and visited[x] is not None:
# found cycle ... | p02550 |
n,x,m = list(map(int, input().split()))
ans = 0
x_list = []
for _ in range(min(10**5,n)):
x_list.append(x)
ans += x
x = x**2 % m
if x_list.count(x) == 1:
after_list = x_list[x_list.index(x):(len(x_list))]
if x_list.index(x) == 0:
if (n // len(x_list)) == 0:
... | N,x,M=list(map(int,input().split()))
k=x
L=list()
c=dict()
for i in range(M):
if i==N:
print((sum(L)))
exit()
if x==0:
print((sum(L)))
exit()
L.append(x)
x=(x*x)%M
if x in c:
q=x
break
c[x]=1
moto=sum(L)
N-=len(L)
for i in range(len(L)):
if L[i]==q:
roop=li... | p02550 |
def f(x, m):
return (x ** 2) % m
def main():
n, x, m = list(map(int, input().split()))
lst = [[False, 0] for _ in range((m + 1))]
lst[x] = [True, 1]
res = [x]
loop = 0
for i in range(2, m + 1):
x = f(x, m)
if lst[x][0] == False:
lst[x][0] = True
... | def main():
n, x, m = list(map(int, input().split()))
idx = [-1] * (m + 1) # mで割った余り(0,1,...m-1)が何回目に出てきたか
res = [] # 数列A
for i in range(1, m + 3): # 余りはm+1種類なのでm+2回計算すれば絶対ループが発生する
if idx[x] == -1: # 初めて
idx[x] = i
res.append(x)
else: # 既出(ループ発見)
... | p02550 |
def cycle_getter(N, start):
"""
:param N: 移動回数
:param start: 初期条件
:return front: cycleまでの要素のリスト
cycle: cycle内の要素のリスト
end: cycle後の余った部分の要素のリスト
cnt: cycle回数
"""
p = start
front, cycle, end = [], [], []
cnt = 0
visit = {p:0}
L, R = N,... | def cycle_getter(N, start):
"""
:param N: 移動回数
:param start: 初期条件
:return front: cycleまでの要素のリスト
cycle: cycle内の要素のリスト
end: cycle後の余った部分の要素のリスト
cnt: cycle回数
"""
p = start
front, cycle, end = [], [], []
cnt = 0
visit = {p:0}
L, R = N,... | p02550 |
# -*- coding: utf-8 -*-
# 標準入力を取得
N, X, M = list(map(int, input().split()))
# 求解処理
def f(x: int, m: int) -> int:
return x**2 % m
A = [X]
loop_start_index = 0
while True:
A_next = f(A[-1], M)
if A_next in A:
loop_start_index = A.index(A_next)
break
else:
A.... | # -*- coding: utf-8 -*-
# 標準入力を取得
N, X, M = list(map(int, input().split()))
# 求解処理
def f(x: int, m: int) -> int:
return x**2 % m
A = [X]
s = {X}
i = 0
while True:
A_next = f(A[-1], M)
if A_next in s:
i = A.index(A_next)
break
else:
A.append(A_next)
... | p02550 |
n,x,m = list(map(int,input().split()))
#listを2つ使って初項からループ手前までのセット、そして二回目のループのセット。
k_1 = list()
k_2 = list()
#len(k_2)でも良かった
#loop_sumはk_2によって表されるループの和
loop_number = 0
loop_sum = 0
while x not in k_1:
k_1.append(x)
x = x*x%m
while x not in k_2:
k_2.append(x)
loop_number+=1
loop_sum+=x
x = x*x%m
an... | n,x,m = list(map(int,input().split()))
k_1 = set()
k_2 = set()
tmp = list()
loop_number = 0
loop_sum = 0
while x not in k_1:
k_1.add(x)
x = x*x%m
while x not in k_2:
k_2.add(x)
tmp.append(x)
loop_number+=1
loop_sum+=x
x = x*x%m
ans = 0
k_1 = list(k_1)
for i in range(len(k_1)):
ans += k_1[i]
... | p02550 |
n, x, m = list(map(int, input().split()))
ans = 0
if x == 0:
print((0))
exit()
elif x == 1:
print(n)
exit()
flag = [False]*m
a = []
while not flag[x]:
flag[x] = True
a.append(x)
x = pow(x, 2, m)
loop_start_idx = a.index(x)
loop_len = len(a) - loop_start_idx
loop_count = (n ... | n, x, m = list(map(int, input().split()))
flag = [False]*m
a = []
while not flag[x]:
flag[x] = True
a.append(x)
x = pow(x, 2, m)
loop_start_idx = a.index(x)
loop_len = len(a) - loop_start_idx
loop_count = (n - loop_start_idx)//loop_len
loop_amari = (n-loop_start_idx)%loop_len
ans = sum(a[:loop_... | p02550 |
n,x,m=list(map(int,input().split()))
A=[x]
ans=0
for i in range(min(n,m)):
a_i=(A[i]**2)%m
if i ==n-1:
print((sum(A)))
exit()
if a_i==0:
print((sum(A)))
exit()
if a_i in A:
k=A.index(a_i)+1
l=len(A)
roop=A[k-1:l]
many=(n-k+1)//(l-k+1)
ans+=sum(A[:k-1])
an... | N, X, M = list(map(int, input().split()))
flag = [False for _ in range(M)]
record = list()
record.append(X)
flag[X] = 1
An = X
for i in range(M + 1):
An = pow(An, 2, M)
if flag[An]:
start = flag[An]
cnt = i + 2 - start
cost = record[-1] - record[start - 2] if start > 1 els... | p02550 |
n,x,m= list(map(int, input().strip().split()))
a=[0]*(m)
for i in range(m):
a[i]=i*i%m
a=list(set(a))
t=0
b=[]
b.append(x)
for i in range(m+1):
b.append(b[-1]**2%m)
if len(b)>len(list(set(b))):
break
l=len(b)
for i in range(l-1):
if b[i]==b[l-1]:
q=i
w=l-2-q+1... | n,x,m= list(map(int, input().strip().split()))
t=0
b=[]
b.append(x)
for i in range(m+1):
if b[-1]**2%m in b:
b.append(b[-1]**2%m)
break
else:
b.append(b[-1]**2%m)
l=len(b)
for i in range(l-1):
if b[i]==b[l-1]:
q=i
w=l-2-q+1
s=0
f... | p02550 |
N, X, M = [int(x) for x in input().split()]
seen = set()
front = []
cycle = []
for i in range(N):
x = pow(X, 2 ** i, M)
if x in seen:
index = front.index(x)
front, cycle = front[:index], front[index:]
break
front.append(x)
seen.add(x)
if N <= len(front):
pr... | def solve(N, X, M):
seen = set()
front = [X]
cycle = []
for i in range(N - 1):
x = (front[-1] * front[-1]) % M
if x in seen: # seen can't be more than M since x is < M
index = front.index(x)
front, cycle = front[:index], front[index:]
break
... | p02550 |
N,X,M = list(map(int,input().split()))
data = [X for i in range(M+1)]
val = set([X])
start = -1
period = -1
for i in range(1,M+1):
data[i] = pow(data[i-1],2,M)
if data[i] in val:
for j in range(0,i):
if data[j]==data[i]:
start = j
period = i-j
... | N,X,M = list(map(int,input().split()))
data = [X for i in range(M+1)]
val = set([X])
start = -1
period = -1
for i in range(1,M+1):
data[i] = pow(data[i-1],2,M)
if data[i] in val:
for j in range(0,i):
if data[j]==data[i]:
start = j
period = i-j
... | p02550 |
N,X,M = list(map(int,input().split()))
count = 1
S = X
A = [X]
for i in range(2,N+1):
A_i = (A[-1]*A[-1])%M
A.append(A_i)
S += A_i
if len(A) != len(set(A)):
S -= A_i
del A[-1]
B = A[A.index(A_i):]
S += ((N-len(A))//len(B))*sum(B) + sum(B[:((N-len(A))%len(B))])
break
print(S... | N,X,M = list(map(int,input().split()))
S = X
A = [X]
exists = [0]*M
exists[X] = 1
for i in range(2,N+1):
A_i = (A[-1]*A[-1])%M
A.append(A_i)
S += A_i
if exists[A_i] == 0:
exists[A_i] = 1
else:
S -= A_i
del A[-1]
B = A[A.index(A_i):]
S += ((N-len(A))//len(B))*sum(B) + sum(B... | p02550 |
n, x, m = [int(_) for _ in input().split()]
d = {}
d_rev = {}
d[x] = 0
d_rev[0] = x
c = 0
while True:
c += 1
x = (x ** 2) % m
if x in d:
roop_idx = d[x]
break
else:
d[x] = c
d_rev[c] = x
# print(d)
# print(d_rev)
ans = 0
if roop_idx >= n:
for i in... | n, x, m = [int(_) for _ in input().split()]
d = {}
d_rev = {}
d[x] = 0
d_rev[0] = x
c = 0
while True:
c += 1
x = (x ** 2) % m
if x in d:
roop_idx = d[x]
break
else:
d[x] = c
d_rev[c] = x
ans = 0
if roop_idx >= n:
for i in range(n):
ans += d... | p02550 |
n, x, m = list(map(int, input().split()))
mn = min(n, m)
S = set()
A = []
a = x
ans = 0
for i in range(mn):
if a in S: break
S.add(a)
A.append(a)
ans += a
a = a*a%m
if a == 0:
print(ans)
exit()
if len(A) >= mn:
print(ans)
exit()
st_len = 0
while... | n, x, m = list(map(int, input().split()))
mn = min(n, m)
S = set()
A = []
a = x
sum_9 = 0 # sum of pre + cycle
for _ in range(mn):
if a in S: break
S.add(a)
A.append(a)
sum_9 += a
a = a*a % m
if a == 0:
print(sum_9)
exit()
if len(A) >= mn:
print(sum_9)
... | p02550 |
n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # value of pre & cycle
sum_p = 0 # sum of pre + cycle
X = [False] * m # for cycle check
for _ in range(mn):
if X[x]:
pre_len = P.index(x)
cyc_len = len(P) - pre_len
nxt_len = (n - pre_len) % cyc_len
cyc = sum(P... | n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # value of pre & cycle
sum_p = 0 # sum of pre + cycle
X = [False] * m # for cycle check
for _ in range(mn):
if X[x]:
pre_len = P.index(x)
cyc_len = len(P) - pre_len
nxt_len = (n - pre_len) % cyc_len
pre = sum(P... | p02550 |
n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # value of pre & cycle
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
nxt_len = (n - X[x]) % cyc_len
pre = sum(P[:X[x]])
cyc = (sum_p - p... | n, x, m = list(map(int, input().split()))
P = [] # value of pre & cycle
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check
for i in range(n):
if X[x] > -1:
cyc_len = len(P) - X[x]
nxt_len = (n - X[x]) % cyc_len
pre = sum(P[:X[x]])
cyc = (sum_p - pre) * ((n - X[x])... | p02550 |
n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # (x,pre_sum)
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
pre = P[X[x]][1]
cyc = (sum_p - pre) * ((n - X[x]) // cyc_len)
nxt = P[X[x] ... | n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # (x,pre_sum)
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
pre = P[X[x]]
cyc = (sum_p - pre) * ((n - X[x]) // cyc_len)
nxt = P[X[x] + (... | p02550 |
n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # pre_sum
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check & pre_len
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
remain = P[X[x]]
cyc = (sum_p - remain) * ((n - X[x]) // cyc_len)
rema... | n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # pre_sum
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check & pre_len
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
cyc = (sum_p - P[X[x]]) * ((n - X[x]) // cyc_len)
remain = P[X[x] + (n - X[x]) ... | p02550 |
n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # pre_sum
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check & pre_len
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
n -= X[x]
cyc = (sum_p - P[X[x]]) * (n // cyc_len)
remain = P[X[x] + n... | n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = [] # pre_sum
sum_p = 0 # sum of pre + cycle
X = [-1] * m # for cycle check & pre_len
for i in range(mn):
if X[x] > -1:
cyc_times, nxt_len = divmod(n-X[x], len(P) - X[x])
cyc = (sum_p - P[X[x]]) * cyc_times
remain = P[X[x] ... | p02550 |
N, X, M = list(map(int, input().split()))
candicate = [pow(i, 2, M) for i in range(M)]
ans = 0
now = X
A = [0]
X2 = pow(X, 2, M)
d = dict()
flag = True
for i in range(1, N+1):
if i == 1:
A.append(X)
else:
nxt = candicate[A[i-1]]
if d.get(nxt) == None:
... | N,X,M = list(map(int,input().split()))
ans = X
A = X
TF = True
srt = 1000000
retu = [X]
d = dict()
d[X] = 0
loop = X
flag = False
for i in range(N-1):
if TF:
A = A**2 % M
if d.get(A) != None:
srt = d[A]
goal = i
TF = False
if TF:
... | p02550 |
n,x,m=list(map(int,input().split()))
def f(ai,m):
return (ai*ai)%m
if x==0:
print((0))
elif x==1:
print(n)
elif m==1:
print((0))
elif n<=m*2:
asum=x
ai=x
for i in range(1,n):
ai=f(ai,m)
asum+=ai
print(asum)
else:
aa=[]
ai,icnt,asum=x,0,0
... | n,x,m=list(map(int,input().split()))
def f(ai,m):
return (ai*ai)%m
if x==0:
print((0))
elif x==1:
print(n)
elif m==1:
print((0))
elif n<=m*2:
asum=x
ai=x
for i in range(1,n):
ai=f(ai,m)
asum+=ai
print(asum)
else:
chk=[-1]*m
asum00=[0]*m
... | p02550 |
n,x,m=list(map(int,input().split()))
def f(ai,m):
return (ai*ai)%m
if x==0:
print((0))
elif x==1:
print(n)
elif m==1:
print((0))
elif n<=m*2:
asum=x
ai=x
for i in range(1,n):
ai=f(ai,m)
asum+=ai
print(asum)
else:
chk=[-1]*m
asum00=[0]*m
... | n,x,m=list(map(int,input().split()))
def f(ai,m):
return (ai*ai)%m
if x==0:
print((0))
elif x==1:
print(n)
elif m==1:
print((0))
#elif n<=m*2:
# asum=x
# ai=x
# for i in range(1,n):
# ai=f(ai,m)
# asum+=ai
# print(asum)
else:
chk=[-1]*m
asum00=[... | p02550 |
def binary_search(data, value):
left = 0 # 探索する範囲の左端を設定
right = len(data) - 1 # 探索する範囲の右端を設定
while left <= right:
mid = (left + right) // 2 # 探索する範囲の中央を計算
if data[mid] == value:
# 中央の値と一致した場合は位置を返す
return mid
elif data[... | def binary_search(data, value):
left = 0 # 探索する範囲の左端を設定
right = len(data) - 1 # 探索する範囲の右端を設定
while left <= right:
mid = (left + right) // 2 # 探索する範囲の中央を計算
if data[mid] == value:
# 中央の値と一致した場合は位置を返す
return mid
elif data[... | p02550 |
n,x,m=list(map(int, input().split()))
a=x
ans=a
flg=[0]*m
flg[a]=1
l=[a]
lp=-1
for i in range(1,m+1):
if n <= i:
break
tmp=(a*a)%m
a=tmp
if flg[a]==1:
lp = l.index(a)
break
else:
ans+=tmp
l.append(a)
flg[a]=1
if lp != -1:
... | n,x,m=list(map(int, input().split()))
a=x
ans=a
flg=[0]*m
flg[a]=1
l=[a]
lp=-1
for i in range(1,n):
tmp=(a*a)%m
a=tmp
if flg[a]==1:
lp = l.index(a)
break
else:
ans+=tmp
l.append(a)
flg[a]=1
if lp != -1:
l2 = l[lp:]
tmp = sum(l2)
... | p02550 |
# -*- coding: utf-8 -*-
# map(int, input().split())
N,X,M = list(map(int, input().split()))
first_A = [X]
mod_M = [False for i in range(M)]
is_break = False
for i in range(min(N-1,M+1)):
A_n1 = pow(first_A[-1],2)%M
if A_n1 in first_A+[1,0]:
is_break = True
last_num = A_n1
... | # -*- coding: utf-8 -*-
# map(int, input().split())
N,X,M = list(map(int, input().split()))
first_A = [X]
mod_M = [False for i in range(M+1)]
mod_M[0] = True
mod_M[1] = True
is_break = False
for i in range(min(N-1,M+1)):
A_n1 = pow(first_A[-1],2)%M
if mod_M[A_n1]:
is_break = True
... | p02550 |
N,X,M = list(map(int,input().split()))
dps = [X%M]
rt = 0
while True:
d = (dps[-1]**2)%M
if d not in dps:
dps.append(d)
if N == len(dps):
print((sum(dps)))
break
else:
ki = dps.index(d)
RP = (N-ki) // len(dps[ki:])
mod = (N-ki) % len(dps[ki:])
print((sum(dps[:ki])... | n, x, m = list(map(int, input().split()))
mn = min(n, m)
P = []
sum_p = 0
X = [-1] * m
for i in range(mn):
if X[x] > -1:
cyc_len = len(P) - X[x]
remain = P[X[x]]
cyc = (sum_p - remain) * ((n - X[x]) // cyc_len)
remain += P[X[x] + (n - X[x]) % cyc_len] - P[X[x]]
pri... | p02550 |
n, x, m = list(map(int, input().split()))
cnt = [x]
for i in range(m+1):
x = x*x % m
if x in cnt:
break
cnt.append(x)
start = cnt.index(x)
ans = sum(cnt[:start])
roop = len(cnt) - start
#print(roop)
n = n-start
ans = ans + (n//roop)*sum(cnt[start:]) + sum(list(cnt)[start:(start... | n, x, m = list(map(int, input().split()))
cnt = [x]
c = set()
c.add(x)
for i in range(m+1):
x = x*x % m
if x in c:
break
cnt.append(x)
c.add(x)
start = cnt.index(x)
ans = sum(cnt[:start])
roop = len(cnt) - start
n = n-start
ans = ans + (n//roop)*sum(cnt[start:]) + sum(lis... | p02550 |
n, x, m = list(map(int, input().split()))
a = x
mod = [a]
loop = []
cnt = 0
while cnt < n:
a = a**2 % m
if a in mod:
i = mod.index(a)
before = mod[:i]
loop = mod[i:]
break
mod.append(a)
cnt += 1
length = len(loop)
if length == 0:
print((sum(mod[:n]))... | n, x, m = list(map(int, input().split()))
a = x
dup = [0]*(10**5+10)
mod = [a]
loop = []
cnt = 0
while cnt < n:
a = a**2 % m
if dup[a]==1:
i = mod.index(a)
before = mod[:i]
loop = mod[i:]
break
mod.append(a)
dup[a] = 1
cnt += 1
length = len(loop)
if... | p02550 |
n, x, m = list(map(int, input().split()))
count = 0
# table2jou = []
# for mi in range(m + 1):
# table2jou.append(pow(mi, 2))
# tablef = []
# for mi in range(m + 1):
# # tablef.append(table2jou[mi] % m)
# tablef.append(pow(mi, 2, m))
count = x
pre_an = x
his = [x]
for ni in range(2, n ... | n, x, m = list(map(int, input().split()))
count = 0
# table2jou = []
# for mi in range(m + 1):
# table2jou.append(pow(mi, 2))
# tablef = []
# for mi in range(m + 1):
# # tablef.append(table2jou[mi] % m)
# tablef.append(pow(mi, 2, m))
is_used = [0] * (m + 1)
count = x
pre_an = x
his = [... | p02550 |
def calc_dynamic(n, x, m):
# 結果を保持する辞書
cal_result = {}
# 初期値の設定
cal_result[1] = x
if n == 1:
return x
for i in range(1, n):
cal_result[i+1] = cal_result[i] ** 2 % m
#return cal_result[n]
return sum(cal_result.values())
def calc_dynamic2(n, x, m):
... | def calc_dynamic(n, x, m):
# 結果を保持する辞書
cal_result = {}
# 初期値の設定
cal_result[1] = x
if n == 1:
return x
for i in range(1, n):
cal_result[i+1] = cal_result[i] ** 2 % m
#return cal_result[n]
return sum(cal_result.values())
def calc_dynamic2(n, x, m):
... | p02550 |
n, x, m = list(map(int, input().split()))
mod_list = [-1 for i in range(m)]
value = x
i = 0
head = 0
while i < n:
if value not in mod_list:
mod_list[i] = value
i += 1
value = (value**2)%m
else:
head = mod_list.index(value)
break
if i == n:
print((sum(mod... | n, x, m = list(map(int, input().split()))
mod_value = [-1 for i in range(2*m)] #余りの値を頭から格納
mod_check = [False for i in range(m)] #余りが現れたか否か
value = x
i = 0
head = 0
while i < n:
if not mod_check[value]:
mod_value[i] = value
mod_check[value] = True
i += 1
value = (value**2)... | p02550 |
from collections import defaultdict
it = lambda: list(map(int, input().strip().split()))
def solve():
N, X, M = it()
if N == 1:
return X % M
value = defaultdict(int)
history = defaultdict(int)
cur = 0
cnt = 0
for i in range(N):
if X in history:
bre... | from collections import defaultdict
it = lambda: list(map(int, input().strip().split()))
def solve():
N, X, M = it()
if N == 1: return X % M
cur = 0
cnt = 0
value = defaultdict(int)
history = defaultdict(int)
for i in range(N):
if X in history: break
value[X]... | p02550 |
# -*- coding: utf-8 -*-
N, X, M = list(map(int, input().split()))
mod_check_list = [False for _ in range(M)]
mod_list = [(X ** 2) % M]
counter = 1
mod_sum = (X ** 2) % M
last_mod = 0
for i in range(M):
now_mod = (mod_list[-1] ** 2) % M
if mod_check_list[now_mod]:
last_mod = now_mod
... | # -*- coding: utf-8 -*-
N, X, M = list(map(int, input().split()))
mod_check_list = [False for _ in range(M)]
mod_list = [(X ** 2) % M]
counter = 1
mod_sum = (X ** 2) % M
last_mod = 0
for i in range(M):
now_mod = (mod_list[-1] ** 2) % M
if mod_check_list[now_mod]:
last_mod = now_mod
... | p02550 |
N, X, M = list(map(int, input().split()))
A = [X]
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0
for i in range(M+1):
a = a ** 2 % M
if a in A:
r0 = A.index(a)
l = len(A[r0:])
r = (N - i - 1) // l
amari = (N - i - 1) % l
s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
break
A.appe... | N, X, M = list(map(int, input().split()))
A = [X]
A_dict = {X}
a = X
s = X
r = 0
r0 = 0
l = 0
amari = 0
for i in range(M+1):
a = a ** 2 % M
if a in A_dict:
r0 = A.index(a)
l = len(A[r0:])
r = (N - i - 1) // l
amari = (N - i - 1) % l
s = s + sum(A[r0:])*r + sum(A[r0:r0+amari])
... | p02550 |
# -*- using utf8 -*-
import sys
inputlinevec = sys.stdin.readline().strip().split(' ')
n = int(inputlinevec[0])
x = int(inputlinevec[1])
m = int(inputlinevec[2])
sum_res = x
crcle_list = list()
crcle_idx = 0
pre_a = x
for idx in range(1, n):
#a范围在0-m之间,不会无限递增,因此很大概率会重复
#a * a % m这种单变量的式子,一旦a出现了之... | # -*- using utf8 -*-
import sys
inputlinevec = sys.stdin.readline().strip().split(' ')
n = int(inputlinevec[0])
x = int(inputlinevec[1])
m = int(inputlinevec[2])
sum_res = x
crcle_dict = dict()
crcle_list = list()
crcle_idx = 0
pre_a = x
for idx in range(1, n):
#a范围在0-m之间,不会无限递增,因此很大概率会重复
#a * ... | p02550 |
N, X, M = list(map(int, input().split()))
if N == 1:
print(X)
exit()
A = [-1] * (10**5 + 1)
A[1] = X
# %M は答えをMOD Mにしなさいといってくれているだけ
# M はたかだか10^5なので10^5繰り返すとどこかで循環する。循環するまで繰り返す。
S = set([X])
i = 2
while True:
A[i] = ((A[i - 1])**2) % M
if A[i] in S:
break
S.add(A[i])
... | N, X, M = list(map(int, input().split()))
I = [-1] * M
A = []
total = 0
while (I[X] == -1):
A.append(X)
I[X] = len(A)
total += X
X = (X * X) % M
# print(f'{A=}')
# print(f'{I[:20]=}')
# print(f'{total=}')
# print(f'{X=}, {I[X]=}')
c = len(A) - I[X] + 1
s = sum(A[I[X] - 1:])
# print(... | p02550 |
""" E
https://atcoder.jp/contests/abc173/tasks/abc173_e
"""
import sys
import math
from functools import reduce
def readString():
return sys.stdin.readline()
def readInteger():
return int(readString())
def readStringSet(n):
return sys.stdin.readline().split(" ")[:n]
def readIntegerSe... | """ E
https://atcoder.jp/contests/abc173/tasks/abc173_e
"""
import sys
import math
from functools import reduce
def readString():
return sys.stdin.readline()
def readInteger():
return int(readString())
def readStringSet(n):
return sys.stdin.readline().split(" ")[:n]
def readIntegerSe... | p02550 |
import sys
N, X, M = list(map(int, input().split()))
if N == 1:
print(X)
sys.exit()
ans = 0
ans += X - (X%M)
X %= M
def fun(x):
return (x * x) % M
appear_list = [X]
appear_set = {X}
x = X
i_0 = 1
sum_0 = x
while fun(x) not in appear_set:
x = fun(x)
appear_list.append(x)
appear_se... | import sys
N, X, M = list(map(int, input().split()))
if N == 1: #例外
print(X)
sys.exit()
ans = 0
ans += X - (X%M) #XがMより大きい場合、超過分を先に計上する。
X %= M #これで 0 <= X <= M-1 が成り立つようにできた。
def fun(x): #問題の関数を用意(いちいち中身を書いていると面倒くさいため)
return (x * x) % M
appear_set = {X} #すでに現れた数を格納する
i_0 = 1 #今までに調べた個数。... | p02550 |
N, X, M = list(map(int, input().split()))
ind = 0
l = [X]
while True:
t = l[-1] ** 2 % M
if t not in l:
l.append(t)
else:
ind = l.index(t)
break
S = sum(l[ind:])
L = len(l)
ans = 0
if N <= L:
ans = sum(l[:N])
else:
ans = sum(l)
N -= L
ans += S * (N // (L - ind))
ans += sum... | N, X, M = list(map(int, input().split()))
checklist = [True] * 100000
ind = 0
l = [X]
while True:
t = l[-1] ** 2 % M
if checklist[t]:
l.append(t)
checklist[t] = False
else:
ind = l.index(t)
break
S = sum(l[ind:])
L = len(l)
ans = 0
if N <= L:
ans = sum(l[:N])
else:
ans = sum(... | p02550 |
N,X,M=list(map(int,input().split()))
seen=[-2]*M
seen[X]
A=[X]
i=1
while(i<N):
T=A[-1]**2%M
if seen[T]!=-2:
Roop=i-seen[T]
Left,Right=seen[T],i
break
A.append(T)
seen[T]=i
i+=1
if i==N:
print((sum(A)))
exit()
Roopsum=0
for i in range(Left,Right... | N,X,M=list(map(int,input().split()))
i=1
A=[X]
seen=[-1]*M
seen[X]=0
while(i<N):
T=A[-1]**2%M
#seen[T]~i-1までがループになっている
if seen[T]!=-1:
Roop=i-seen[T]
Left,Right=seen[T],i
break
seen[T]=i
A.append(T)
i+=1
if i==N:
print((sum(A)))
exit()
ans=... | p02550 |
#import sys
#print(sys.maxsize)
N,X,M=list(map(int,input().split()))
#table=list(range(M))
#for i in range(M):
# table[i]=((table[i]%M)**2)%M
#print(table[471])
start=X%M
rireki=list()
rireki.append(start)
ss=0
for j in range(1,N):
start=(start**2)%M
if start in rireki:
ss=rireki.index(... | #import sys
#print(sys.maxsize)
N,X,M=list(map(int,input().split()))
#table=list(range(M))
#for i in range(M):
# table[i]=((table[i]%M)**2)%M
#print(table[471])
start=X%M
rset=set()
rireki=list()
rset.add(start)
rireki.append(start)
ss=0
for j in range(1,M+1):
start=(start**2)%M
if start in rs... | p02550 |
N,X,M=list(map(int,input().split()))
table=[X]
ans=X
for i in range(N-1):
nx=table[i]**2
nx%=M
if nx in table:
first=table.index(nx)
oneloop=i+1-first
rest=N-i-1
loops=rest//oneloop
totalofoneloop=sum(table[first:])
ans+=totalofoneloop*loops
... | N,X,M=list(map(int,input().split()))
table=[X]
visited=[-1]*M
visited[X]=1
ans=X
for i in range(N-1):
nx=table[i]**2
nx%=M
if visited[nx]>0:
first=table.index(nx)
oneloop=i+1-first
rest=N-i-1
loops=rest//oneloop
totalofoneloop=sum(table[first:])
... | p02550 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, X, M = list(map(int, readline().split()))
n = 1
P = 1
while n < N:
n *= 2
P += 1
pos = [... | import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, X, M = list(map(int, readline().split()))
P = N.bit_length()
pos = [[0] * M for _ in range(P)]
value = [[0] * M f... | p02550 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, X, M = list(map(int, readline().split()))
P = N.bit_length()
pos = [[0] * M for _ in range(P)]
value = [[0] * M f... | import sys
readline = sys.stdin.readline
def main():
N, X, M = list(map(int, readline().split()))
P = N.bit_length()
pos = [[0] * M for _ in range(P)]
value = [[0] * M for _ in range(P)]
for r in range(M):
pos[0][r] = r * r % M
value[0][r] = r
for p in range... | p02550 |
n,x,m=list(map(int,input().split()))
ans=x
a=[x]
if n==1:
print(ans)
else:
for i in range(2,n+1):
tmp=a[-1]**2%m
if tmp not in a:
a.append(tmp)
ans+=tmp
else:
t=a.index(tmp)
if len(a)-t>=n-i+1:
ans+=sum(a[t:t+n-i+1])
break
else:
t=a.... | n,x,m=list(map(int,input().split()))
ans=x
a=[x]
d={}
d[x]=1
if n==1:
print(ans)
else:
for i in range(2,n+1):
tmp=a[-1]**2%m
if d.get(tmp,0)==0:
a.append(tmp)
d[tmp]=1
ans+=tmp
else:
t=a.index(tmp)
if len(a)-t>=n-i+1:
ans+=sum(a[t:t+n-i+1])
... | p02550 |
n, x, m = list(map(int, input().split()))
ans = []
flag = False
for i in range(n):
if x in ans:
v = x
flag = True
break
ans.append(x)
x = x**2 % m
if flag:
p = ans.index(v)
l = len(ans) - p
d, e = divmod(n-p, l)
print((sum(ans[:p]) + d*sum(ans[p:]) + s... | n, x, m = list(map(int, input().split()))
ans = []
c = [0]*m
flag = False
for i in range(n):
if c[x] == 1:
flag = True
break
ans.append(x)
c[x] = 1
x = x**2 % m
if flag:
p = ans.index(x)
l = len(ans) - p
d, e = divmod(n-p, l)
print((sum(ans[:p]) + d*sum(a... | p02550 |
n, x, m = list(map(int, input().split()))
l = [x]
loop = []
a = x
for i in range(n - 1):
a = a**2%m
if a not in l:
l.append(a)
else:
if a not in loop:
loop.append(a)
else:
break
if len(loop) > 0:
rest = (n - len(l))%len(loop)
times =... | n, x, m = list(map(int, input().split()))
l = [x]
a = x
loopsum = 0
loopnum = 0
for i in range(n - 1):
a = a**2%m
if a not in l:
l.append(a)
else:
pos = l.index(a)
loopnum = len(l[pos:])
loopsum = sum(l[pos:])
break
if loopnum > 0:
rest = (n -... | p02550 |
n, x, m = list(map(int, input().split()))
a = x
S = [0] * (m + 1)
F = [None] * m
F[x] = 0
i0, j0 = None, None
for i in range(1, m + 1):
S[i] = S[i - 1] + a
a = pow(a, 2, m)
if F[a] is not None:
i0, j0 = F[a], i
F[a] = i
q, r = (n - i0) // (j0 - i0), (n - i0) % (j0 - i0)
print(((S[j0... | n, x, m = list(map(int, input().split()))
S = [0]
F = [None] * m
for i in range(m + 1):
S.append(S[-1] + x)
if F[x] is not None:
j = F[x]
q, r = (n - j) // (i - j), (n - j) % (i - j)
print(((S[i] - S[j]) * q + S[j + r]))
exit()
F[x] = i
x = pow(x, 2, m)
print(... | p02550 |
n, x, m = list(map(int, input().split()))
S = [0]
F = [None] * m
for i in range(m + 1):
S.append(S[-1] + x)
if F[x] is not None:
j = F[x]
q, r = (n - j) // (i - j), (n - j) % (i - j)
print(((S[i] - S[j]) * q + S[j + r]))
exit()
F[x] = i
x = pow(x, 2, m)
print(... | n, x, m = list(map(int, input().split()))
S = [0]
F = [None] * m
for i in range(m + 1):
S.append(S[-1] + x)
if F[x] is not None:
j = F[x]
q, r = (n - j) // (i - j), (n - j) % (i - j)
print(((S[i] - S[j]) * q + S[j + r]))
exit()
F[x] = i
x = pow(x, 2, m)
| p02550 |
import sys
sys.setrecursionlimit(100000000)
while True:
w, h = list(map(int, input().split()))
if not w:
break
n = int(eval(input()))
xlst = [0, w - 1]
ylst = [0, h - 1]
plst = []
for i in range(n):
x1, y1, x2, y2 = list(map(int, input().split()))
plst.append([x1,y1,x2 - ... | def main():
while True:
w, h = list(map(int, input().split()))
if not w:
break
n = int(eval(input()))
xlst = [0, w - 1]
ylst = [0, h - 1]
plst = []
for i in range(n):
x1, y1, x2, y2 = list(map(int, input().split()))
plst.append([x1,y1,x2 - 1,y2 - 1]... | p00454 |
def main():
while True:
w, h = list(map(int, input().split()))
if not w:
break
n = int(eval(input()))
xlst = [0, w - 1]
ylst = [0, h - 1]
plst = []
for i in range(n):
x1, y1, x2, y2 = list(map(int, input().split()))
plst.append([x1,y1,x2 - 1,y2 - 1]... | def main():
while True:
w, h = list(map(int, input().split()))
if not w:
break
n = int(eval(input()))
xlst = [0, w - 1]
ylst = [0, h - 1]
plst = []
for i in range(n):
x1, y1, x2, y2 = list(map(int, input().split()))
plst.append([x1,y1,x2 - 1,y2 - 1]... | p00454 |
# tree diameter if (currDia <= k) true else shorten() while (dia >= k) O(n) dia readjust
# multiple pairs . centroid of tree --> on largest path find centroid --> maintain a heap ?
from queue import Queue
def main():
n, d = list(map(int, input().split()))
adj = [[] for i in range(n)]
edges ... | # tree diameter if (currDia <= k) true else shorten() while (dia >= k) O(n) dia readjust
# multiple pairs . centroid of tree --> on largest path find centroid --> maintain a heap ?
from collections import deque
def main():
n, d = list(map(int, input().split()))
adj = [[] for i in range(n)]
... | p04049 |
from collections import deque
N, K = list(map(int, input().split()))
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = list(map(int, input().split()))
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append((a, b))
def dfs(n):
visited = [False] * N
stack = [[n, 0... | from collections import deque
N, K = list(map(int, input().split()))
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = list(map(int, input().split()))
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append((a, b))
def bfs(n):
visited = [False] * N
dist = [0] * N... | p04049 |
from collections import deque
N, K = list(map(int, input().split()))
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = list(map(int, input().split()))
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append((a, b))
def bfs(n):
visited = [False] * N
dist = [0] * N... | from collections import deque
N, K = list(map(int, input().split()))
T = [[] for i in range(N)]
E = []
for i in range(N-1):
a, b = list(map(int, input().split()))
a, b = a-1, b-1
T[a].append(b)
T[b].append(a)
E.append([a, b])
def bfs(n):
visited = [False] * N
dist = [0] * N... | p04049 |
o=lambda:list(map(int,input().split()))
T,F=True,False
n,k=o()
e=[[] for _ in [0]*n]
for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1]
a=10**5
def dfs(x,f):
if f>=0:l[f]=T
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=T
for w in e[v]:
if ~l[w]:q+=[(w,c+1)]
return n-l.count(... | o=lambda:list(map(int,input().split()))
T,F=True,False
n,k=o()
e=[[] for _ in [0]*n]
for _ in [0]*(n-1):a,b=o();e[a-1]+=[b-1];e[b-1]+=[a-1]
a=10**5
def dfs(x,f):
if f>=0:l[f]=T
q=[(x,0)]
while len(q):
v,c=q.pop(0)
if c>k/2:break
l[v]=T
for w in e[v]:
if not l[w]:q+=[(w,c+1)]
return n-l.cou... | p04049 |
import sys
input = sys.stdin.readline
n,k = list(map(int,input().split()))
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
a,b = list(map(int,input().split()))
edge[a-1].append(b-1)
edge[b-1].append(a-1)
alledge.append((a-1,b-1))
def f(x,y,c):
global tmp
c += 1
... | import sys
#input = sys.stdin.readline
n,k = list(map(int,input().split()))
edge = [[] for i in range(n)]
alledge = []
for i in range(n-1):
a,b = list(map(int,input().split()))
edge[a-1].append(b-1)
edge[b-1].append(a-1)
alledge.append((a-1,b-1))
def f(x,y,c):
global tmp
c += 1
... | p04049 |
N,K=list(map(int,input().split()))
G=[[] for i in range(N)]
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
for i in range(N-1):
a[i],b[i]=list(map(int,input().split()))
a[i]-=1;b[i]-=1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
P=[-1 for i in range(N)]
rnk=[-1 for i in range(N)]
q=[0]
rn... | N,K=list(map(int,input().split()))
G=[[] for i in range(N)]
a=[0 for i in range(N-1)]
b=[0 for i in range(N-1)]
for i in range(N-1):
a[i],b[i]=list(map(int,input().split()))
a[i]-=1;b[i]-=1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
d=[[-1 for i in range(N)] for j in range(N)]
for i in range(N)... | p04049 |
import sys
stdin = sys.stdin
def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return... | import sys
stdin = sys.stdin
def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return... | p04049 |
from collections import deque
def rad_reach(bfr,h,rest,E):
cnt = 0
Q = deque([(bfr,h,rest)])
while Q:
cnt += 1
bfr,h,rest = Q.popleft()
if rest > 0:
for nxt in E[h]:
if nxt == bfr:
continue
Q.append((h,nxt,rest-1))
return cnt
def solve():
N,K = list(map(int,input().split()))
p... | from sys import setrecursionlimit
setrecursionlimit(100000)
def rad_reach(b,h,dist,E,rad):
cnt = 1
if dist < rad:
for nxt in E[h]:
if nxt == b:
continue
cnt += rad_reach(h,nxt,dist+1,E,rad)
return cnt
def solve():
N,K = list(map(int,input().split()))
pairs = []
E = [[] for i in range(N)]... | p04049 |
from collections import deque
n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans,c = [[] for i in range(n)],[[] for i in range(n)],n,0
for i,j in b:
x[i].append(j)
x[j].append(i)
for i in x: c = max(len(i),c)
if n-c+1<=k: ans = 0
elif k==1: ans = n-2... | from collections import deque
n,k = (int(i) for i in input().split())
b = [[int(i)-1 for i in input().split()] for i in range(n-1)]
x,d,ans,c = [[] for i in range(n)],[[] for i in range(n)],n,0
for i,j in b:
x[i].append(j)
x[j].append(i)
def f(s):
for i in range(n):
q,v = deque(),[1]*n
v[i] = 0
for ... | p04049 |
def main():
n, k = list(map(int, input().split()))
a = []
g = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = list(map(int, input().split()))
u -= 1
v -= 1
a.append((u, v))
g[u].append(v)
g[v].append(u)
st = []
pu = st.append
... | def main(le=len):
n, k = list(map(int, input().split()))
a = []
g = [[] for _ in range(n)]
for _ in range(n - 1):
u, v = list(map(int, input().split()))
u -= 1
v -= 1
a.append((u, v))
g[u].append(v)
g[v].append(u)
st = []
pu = st.append... | p04049 |
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
H = [0]+list(map(int, input().split()))
dp = [[[10**18]*(N+1) for _ in range(K+1)] for _ in range(2)]
dp[0][0][0] = 0
for i in range(N):
for j in range(K+1):
for k in range(N+1):
dp[(i+1)%2][j][k] = 10**18
... | import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
H = [0]+list(map(int, input().split()))
dp = [[10**18]*(N+1) for _ in range(N-K+1)]
dp[0][0] = 0
for i in range(N-K):
for j in range(N+1):
for k in range(j):
dp[i+1][j] = min(dp[i+1][j], dp[i][k]+max(0, H[j... | p02864 |
n,K=list(map(int,input().split()))
l=list(map(int,input().split()))
INF=float('inf')
dp=[[[] for j in range(K+1)] for i in range(n+1)]
dp[0][0]=[(0,0)]
for i,j in enumerate(l,1):
a1,b1=dp[i-1][0][0]
dp[i][0]=[(a1+max(0,j-b1),j)]
for i,j in enumerate(l,1):
for k in range(1,min(K+1,i+1)):
tl... | n,K=list(map(int,input().split()))
l=list(map(int,input().split()))
INF=float('inf')
dp=[[[] for j in range(K+1)] for i in range(n+1)]
dp[0][0]=[(0,0)]
for i,j in enumerate(l,1):
a1,b1=dp[i-1][0][0]
dp[i][0]=[(a1+max(0,j-b1),j)]
for i,j in enumerate(l,1):
for k in range(1,min(K+1,i+1)):
tl... | p02864 |
N,K= list(map(int,input().split()))
H= list(map(int,input().split()))
def dpinit(ps, val=0):
res = [val for i in [0]*ps[-1]]
for i in ps[:-1][::-1]:
res = [res.copy() for k in [0]*i]
return res
H = [0] + H + [0]
dp = dpinit((N+2,K+2))
for i in range(1,N+2):
for j in range(K+1):
... | N,K= list(map(int,input().split()))
H= list(map(int,input().split()))
def dpinit(ps, val=0):
res = [val for i in [0]*ps[-1]]
for i in ps[:-1][::-1]:
res = [res[:] for k in [0]*i]
return res
H = [0] + H + [0]
dp = dpinit((N+2,K+2))
for i in range(1,N+2):
for j in range(K+1):
... | p02864 |
def main():
N, K = (int(i) for i in input().split())
H = [0] + [int(i) for i in input().split()]
if N == K:
return print(0)
elif K == 0:
ans = 0
for i in range(N):
ans += max(H[i+1] - H[i], 0)
return print(ans)
dp = [[10**10]*(N-K+1) for i in r... | def main():
import sys
input = sys.stdin.buffer.readline
N, K = (int(i) for i in input().split())
H = [0] + [int(i) for i in input().split()]
if N == K:
return print(0)
elif K == 0:
ans = 0
for i in range(N):
ans += max(H[i+1] - H[i], 0)
ret... | p02864 |
def main():
import sys
input = sys.stdin.buffer.readline
N, K = (int(i) for i in input().split())
H = [0] + [int(i) for i in input().split()]
if N == K:
return print(0)
elif K == 0:
ans = 0
for i in range(N):
ans += max(H[i+1] - H[i], 0)
ret... | def main():
N, K = (int(i) for i in input().split())
H = [0] + [int(i) for i in input().split()]
# 1-indexed
if N == K:
return print(0)
elif K == 0:
ans = 0
for i in range(N):
ans += max(H[i+1] - H[i], 0)
return print(ans)
dp = [[10**12]*(N-... | p02864 |
from collections import defaultdict
n, k = list(map(int, input().split()))
hhh = list(map(int, input().split()))
INF = 10 ** 18
dp = defaultdict(lambda: INF)
dp[0, 0] = 0
for h in hhh:
ndp = defaultdict(lambda: INF)
for (changed, prev_h), operate in list(dp.items()):
if h == prev_h:
... | from collections import defaultdict
n, k = list(map(int, input().split()))
hhh = list(map(int, input().split()))
INF = 10 ** 18
dp = defaultdict(lambda: INF)
dp[0] = 0
mask1 = (1 << 10) - 1
mask2 = ((1 << 63) - 1) ^ mask1
for h in hhh:
ndp = defaultdict(lambda: INF)
h_key = h << 10
for change... | p02864 |
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(... | #!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(... | p02864 |
n,k = list(map(int, input().split( )))
h = list(map(int, input().split( )))
#h = [0]+h#h[0]=0追加
inf = 10**15
dp = [[inf]*n for _ in range(n+1)] #dp[i][j]右端がi、j+1本残す
mn = h[0]
for i in range(n):
mn = min(mn,h[i])
dp[i][0] = min(mn,h[i])
for i in range(n):##右端
for j in range(i+1):#本数
fo... | n,k = list(map(int, input().split( )))
if n==k:
print((0))
exit()
h = list(map(int, input().split( )))
#h = [0]+h#h[0]=0追加
inf = 10**15
dp = [[inf]*n for _ in range(n+1)] #dp[i][j]右端がi、j+1本残す
mn = h[0]
for i in range(n):
#mn = min(mn,h[i])
dp[i][0] = h[i]#min(mn,h[i])
for i in range(1,n)... | p02864 |
def main():
n, K = list(map(int, input().split()))
H = list(map(int, input().split()))
s = sorted(set(H+[0]))
d = {i: j for j, i in enumerate(s)}
l = len(s)
m = max(s)
a = [m-i for i in s]
dp = [[10**13]*l for _ in range(K+1)]
dp[0][0] = 0
for h in H:
dh = d[h]... | def main():
n, K = list(map(int, input().split()))
H = list(map(int, input().split()))
s = sorted(set(H+[0]))
d = {i: j for j, i in enumerate(s)}
l = len(s)
m = max(s)
a = [m-i for i in s]
dp = [[10**13]*l for _ in range(K+1)]
dp[0][0] = 0
for h in H:
dh = d[h]... | p02864 |
N, K = list(map(int, input().split()))
H = list(map(int, input().split())) + [0]
dp = [[10**12]*(K+2) for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
for j in range(min(i+1, K) + 1):
for k in range(j + 1):
dp[i+1][j] = min(dp[i+1][j], dp[i-k][j-k] + max(H[i] - H[i-k-1], 0))
ans = min(dp[-1])
f... | N, K = list(map(int, input().split()))
H = list(map(int, input().split())) + [0]
dp = [[10**12]*(K+2) for _ in range(N+1)]
dp[0][0] = 0
for i in range(N):
for j in range(min(i+1, K) + 1):
for k in range(j + 1):
dp[i+1][j] = min(dp[i+1][j], dp[i-k][j-k] + max(H[i] - H[i-k-1], 0))
ans = min(dp[-1])
f... | p02864 |
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
def main():
N,K=map(int,input().... | # -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1
def main():
N,K=map(int,input().... | p02864 |
N, K = list(map(int, input().split()))
H = [0] + [int(a) for a in input().split()]
X = [[1<<100] * (N-K+1) for _ in range(N+1)]
X[0][0] = 0
for i in range(1, N+1):
for j in range(1, min(i+1, N-K+1)):
X[i][j] = min([X[ii][j-1] + max(H[i] - H[ii], 0) for ii in range(i)])
print((min([x[-1] for x in X]... | N, K = list(map(int, input().split()))
H = [0] + list(map(int, input().split()))
dp = [[10 ** 100] * (N-K+1) for _ in range(N+1)]
dp[0][0] = 0
for x in range(1, N+1):
for y in range(1, min(x+1, N-K+1)):
dp[x][y] = min([dp[i][y-1] + max(H[x] - H[i], 0) for i in range(x)])
print((min([dp[i][N-K] for i ... | p02864 |
from functools import lru_cache
n, k = list(map(int, input().split()))
h = [0] + list(map(int, input().split())) + [0]
INF = 10 ** 18
@lru_cache(maxsize=10**9)
def solve(i, nokori):
if i == 0:
return 0
res = INF
for j in range(nokori + 1):
if i - 1 - j < 0:
break
... | from functools import lru_cache
n, k = list(map(int, input().split()))
h = [0] + list(map(int, input().split())) + [0]
INF = 10 ** 18
@lru_cache(maxsize=None)
def solve(i, nokori):
if i == 0:
return 0
res = INF
for j in range(nokori + 1):
if i - 1 - j < 0:
break
... | p02864 |
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n,k = [int(i) for i in readline().split()]
a = [0]+[int(i) for i in readline().split()] + [0]
n += 2
INF = 10**18
diff = [0]
for i in range(n-1):
diff.append(max(a[i+1]-a[i],0))
from itertools import accumulate
acc ... |
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n,k = [int(i) for i in readline().split()]
a = [0]+[int(i) for i in readline().split()]
n += 1
INF = 10**18
K = n-k-1
dp = [[INF]*n for _ in range((K+1))]
dp[0][0] = 0
for i in range(1,n):
ndp = [c[:] for c in ... | p02864 |
N, K = list(map(int, input().split()))
D = [Di for Di in input().split()]
for n in range(N, 1000000000):
n = str(n)
flag = True
for i in range(len(str(n))):
if n[i] in D:
flag = False
break
if flag is True:
print(n)
exit() | N, K = list(map(int, input().split()))
D = [Di for Di in input().split()]
for n in range(N, 1000000000):
n = str(n)
flag = True
for ni in n:
if ni in D:
flag = False
break
if flag:
print(n)
exit() | p04045 |
n, k = list(map(int, input().split()))
list_D = set(map(str, input().split()))
num = n
ans = -1
while True:
S = set(list(str(num)))
cnt = 0
for s in S:
if s in list_D:
cnt += 1
if cnt == 0:
ans = num
break
num += 1
print(ans) | n, k = list(map(int, input().split()))
list_D = list(map(str, input().split()))
num = n
ans = -1
while True:
S = list(str(num))
cnt = 0
for s in S:
if s in list_D:
cnt += 1
if cnt == 0:
ans = num
break
num += 1
print(ans) | p04045 |
#!/usr/bin/env python3
import sys
def solve(N: int, K: int, D: "List[int]"):
while True:
for d in D:
strn=str(N)
if str(d) in strn:
break
else:
print(N)
return
N+=1
return
def main():
def ite... | #!/usr/bin/env python3
import sys
def solve(N: int, K: int, D: "List[int]"):
D = list(map(str,D))
while True:
for d in D:
if d in str(N):
N += 1
break
else:
print(N)
break
return
def main():
d... | p04045 |
total, k = list(map(int, input().split()))
ds = list(input().split())
ans = total
while True:
flag = False
for d in ds:
if d in list(str(ans)):
flag = True
if flag:
ans += 1
else:
print(ans)
exit(0) | total, k = list(map(int, input().split()))
ds = list(input().split())
for i in range(total,total*10+2):
for d in ds:
if d in list(str(i)):
break
else:
print(i)
exit(0)
| p04045 |
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# map(int, ... | N, K = list(map(int, input().split()))
D = set(input().split())
for n in range(N, N * 10 + 1):
ok = True
for c in str(n):
if c in D:
ok = False
if ok:
print(n)
break
| p04045 |
n, k = list(map(int, input().split()))
d = list(map(int, input().split()))
n_list = list(str(n))
n_list = [int(num) for num in n_list]
num = {1,2,3,4,5,6,7,8,9,0}
d_set = set(d)
empty = set()
#print(set(n_list))
#print(d_set)
#print(set(n_list) & d_set)
while(1):
n_list = list(str(n))
n_list = [... | import sys
N, K = list(map(int, input().split()))
d = list(map(int, input().split()))
d_set = set(d)
num_set = set([1,2,3,4,5,6,7,8,9,0])
enable = num_set - d_set
#print(type(enable))
#print(enable)
def dfs(s):
#print(s)
global ans
# すでにN以上の場合はこの先を探索しない
if s != "":
if int(s) >= N:
... | p04045 |
kingaku, n = input().split()
no_num = input().split()
result = []
ok_num = []
kuriage_num = [-1, 0]
def kuriage(l):
for i in ok_num:
if result[l] < i:
kuriage_num = [l, i]
return
if l <= 0:
return
return kuriage(l-1)
#使える数字のリストを作成
for i in range(10):... | kingaku, n = input().split()
no_num = input().split()
ok_num = []
result = []
for i in range(10):
if not str(i) in no_num:
ok_num.append(str(i))
set_flg = False
for i in kingaku:
set_flg = True
for j in ok_num:
if i == j:
set_flg = False
result.append... | p04045 |
N, L = list(map(int, input().split()))
S = {int(x) for x in input().split()}
flag = False
while flag == False:
if N%10 in S:
N += 1
continue
if int((N%100-N%10)/10) in S and N >= 10:
N += 1
continue
if int((N%1000-N%100)/100) in S and N >= 100:
N += 1
... | N, L = list(map(int, input().split()))
S = {int(x) for x in input().split()}
flag = False
while 1:
if N%10 in S:
N += 1
continue
if int((N%100-N%10)/10) in S and N >= 10:
N += 1
continue
if int((N%1000-N%100)/100) in S and N >= 100:
N += 1
continue... | p04045 |
N, K = list(map(int, input().split()))
D = set(map(str, input().split()))
for i in range(N, 100000):
a = list(str(i))
a = set(a)
if a & D == set():
print(i)
break
| N, K = list(map(int, input().split()))
D = set(list(input().split()))
while True:
if set(list(str(N))) & D:
N += 1
else:
print(N)
exit()
| p04045 |
Num = [int(n) for n in input().rstrip().split()]
D = [int(n) for n in input().rstrip().split()]
for i in range(Num[0],100000):
if len(set([str(n) for n in D]) & set(str(i))) == 0:
print((int(i)))
break
| Num = [int(n) for n in input().rstrip().split()]
D = [int(n) for n in input().rstrip().split()]
# numbers = [ n for n in range(10) if not n in D]
flag = 0
for i in range(Num[0],10*Num[0]+1):
for j in str(i):
if int(j) in D:
flag = 1
break
if flag == 0:
... | p04045 |
Num = [int(n) for n in input().rstrip().split()]
D = [int(n) for n in input().rstrip().split()]
# numbers = [ n for n in range(10) if not n in D]
flag = 0
for i in range(Num[0],10*Num[0]+1):
for j in str(i):
if int(j) in D:
flag = 1
break
if flag == 0:
... | N ,K = list(map(int,input().split()))
D = [int(n) for n in input().split()]
flag = False
for i in range(N,100000):
N = i
while i > 0:
flag = True
if i%10 in D:
flag = False
break
else:
i //=10
if flag == True:
print(N)
... | p04045 |
import sys
from bisect import bisect_left
def input(): return sys.stdin.readline().strip()
def main():
N, K = list(map(int, input().split()))
D = set(map(int, input().split()))
while True:
check = True
for c in str(N):
if int(c) in D:
check = False
... | import sys
from itertools import product
def input(): return sys.stdin.readline().strip()
def main():
"""
itertools.productを使うと高速なようなので写経
参考:https://atcoder.jp/contests/abc042/submissions/16188969
"""
N, K = list(map(int, input().split()))
D = set(map(int, input().split()))
ok = ... | p04045 |
def main():
N, K = list(map(int, input().split()))
D = set(map(int, input().split()))
numset = set(range(0,10))
d = numset.difference(D)
for n in range(N, pow(10, 6) + 10):
check = set(list(str(n)))
ngflag = False
for i in check:
if int(i) in D:
... | def minN(N:int, usable:list, restrict=True):
usable.sort()
keta = False
if restrict:
for i in usable:
if i >= N:
return str(i)
# 桁が増える
return '1'+str(usable[0])
else:
return str(usable[0])
def rote(N:list, D:set, d:set):
ans =... | p04045 |
n, k = list(map(int, input().split()))
d_array = [int(x) for x in input().split()]
for i in range(n, 100000):
flag = True
price = str(i)
for d in d_array:
str_d = str(d)
if str_d in price:
flag = False
else:
if flag:
print(i)
exit... | n, k = list(map(int, input().split()))
d_array = [str(x) for x in input().split()]
price = 0
while True:
flag = True
for s in str(price):
if s in d_array:
flag = False
if price >= n and flag:
print(price)
break
price+=1 | p04045 |
def main():
n, k = list(map(int, input().split()))
D = list(map(int, input().split()))
while 1:
f = 0
hoge = str(n)
for i in range(len(hoge)):
for j in range(k):
if(int(hoge[i]) == D[j]):
f = 1
break
... | def main():
N, K = list(map(int, input().split()))
D = list(input().split())
i = 0
while True:
S = str(N)
f = True
for s in S:
if s in D:
f = False
break
if f:
break
N += 1
print(N)
... | p04045 |
def getN():
return int(eval(input()))
def getNM():
return list(map(int, input().split()))
def getList():
return list(map(int, input().split()))
def getArray(intn):
return [int(eval(input())) for i in range(intn)]
from collections import defaultdict, deque
from sys import exit
import heapq
i... | def getN():
return int(eval(input()))
def getNM():
return list(map(int, input().split()))
def getList():
return list(map(int, input().split()))
def getArray(intn):
return [int(eval(input())) for i in range(intn)]
from collections import defaultdict, deque
from sys import exit
import heapq
i... | p04045 |
from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))
nums = []
for i in range(10):
if i not in d: nums.append(i)
for i in range(len(str(n)), 6):
for j in product(nums, repeat=i):
t = ""
if j[0] != 0:
for k in j:
t += str(k)
... | from itertools import product
n,k = list(map(int, input().split()))
d = list(map(int, input().split()))
nums = []
for i in range(10):
if i not in d: nums.append(i)
m = str(n)
for i in range(len(m), len(m)+2):
for j in product(nums, repeat=i):
ans = ""
for k in j: ans += str(k)
if int(ans)... | p04045 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.