input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
class uf_tree:
def __init__(self, n):
self.sizes = [0] * n
self.par = list(range(n))
def find(self, x):
if x == self.par[x]:
return x
self.par[x] = self.find(self.par[x])
return self.par[x]
def unite(self, x, y):
x, y = self.find(x), self.find(y)
if x == y:
return
if self.sizes[x] < self.sizes[y]:
x, y = y, x
self.par[y] = x
self.sizes[x] += self.sizes[y]
def same(self, x, y):
return self.find(x) == self.find(y)
N, M = list(map(int, input().split()))
a, b = [0] * M, [0] * M
for i in range(M):
a[i], b[i] = list(map(int, input().split()))
a[i] -= 1; b[i] -= 1
ans = 0
for i in range(M):
uf = uf_tree(N+1)
for j in range(M):
if i == j:
continue
uf.unite(a[j], b[j])
bridge = False
for i in range(N):
if not uf.same(0, i):
bridge = True
if bridge:
ans += 1
print(ans)
| #!/usr/bin/env python3
n, m = list(map(int, input().split()))
edges = []
graph = [ [] for _ in range(n) ]
for i in range(m):
a, b = list(map(int, input().split()))
a -= 1; b -= 1;
edges.append( (a, b) )
graph[a].append(b)
graph[b].append(a)
def dfs(u, v):
is_visited = [ False ]*n
st = []
st.append(0)
while st:
cur = st.pop()
is_visited[cur] = True
for adj in graph[cur]:
if is_visited[adj]: continue
if cur == u and adj == v: continue
if cur == v and adj == u: continue
st.append(adj)
return all(is_visited)
ans = 0
for u, v in edges:
if not dfs(u, v):
ans += 1
print(ans)
| p03575 |
import heapq
N, M = list(map(int, input().split()))
ab = [list([int(x)-1 for x in input().split()]) for _ in range(M)]
ans = 0
def dijkstra(s, graph, n):
dist = [float('inf')]*n
dist[s] = 0
num = [0]*n
num[s] = 1
q = [[0, s]]
heapq.heapify(q)
while q:
c, u = heapq.heappop(q)
if dist[u] < c:
continue
for v, weight in graph[u]:
if dist[v] > dist[u] + weight:
dist[v] = dist[u] + weight
num[v] = num[u]
heapq.heappush(q, [dist[v], v])
elif dist[v] == dist[u] + weight:
num[v] += num[u]
return dist, num
for i in range(M):
graph = [[] for _ in range(N)]
for j in range(M):
if i != j:
a, b = ab[j]
graph[a] += [[b, 1]]
graph[b] += [[a, 1]]
for j in range(N):
d, _ = dijkstra(j, graph, N)
if max(d) == float('inf'):
ans += 1
break
print(ans)
| import sys
sys.setrecursionlimit(1000000000)
N, M = list(map(int, input().split()))
graph = [[0]*N for _ in range(N)]
visited = [0]*N
ab = []
ans = 0
def dfs(u):
visited[u] = 1
for v in range(N):
if graph[u][v] == 1 and visited[v] == 0:
dfs(v)
for _ in range(M):
a, b = [int(x)-1 for x in input().split()]
graph[a][b] = 1
graph[b][a] = 1
ab += [[a, b]]
for _ in range(M):
a, b = ab.pop(0)
graph[a][b] = 0
graph[b][a] = 0
for i in range(N):
visited[i] = 0
dfs(0)
if sum(visited) != N:
ans += 1
graph[a][b] = 1
graph[b][a] = 1
print(ans) | p03575 |
import copy
N, M = [int(_) for _ in input().split()]
AB = [[int(_) - 1 for _ in input().split()] for _ in range(M)]
G = [[0] * N for _ in range(N)]
for ab in AB:
G[ab[0]][ab[1]] = 1
G[ab[1]][ab[0]] = 1
def dfs(Gn, a, x):
a[x] = 1
for i in range(N):
if Gn[x][i] == 1 and a[i] == 0:
a[i] = 1
dfs(Gn, a, i)
count = 0
for i in range(N - 1):
for j in range(i + 1, N):
if G[i][j] == 1:
Gn = copy.deepcopy(G)
Gn[i][j] = 0
Gn[j][i] = 0
flag = False
for k in range(N):
a = [0] * N
dfs(Gn, a, k)
if all(a):
flag = True
break
if not flag:
count += 1
print(count)
| import copy
N, M = [int(_) for _ in input().split()]
AB = [[int(_) - 1 for _ in input().split()] for _ in range(M)]
G = [set() for _ in range(N)]
a = set()
for ab in AB:
G[ab[0]].add(ab[1])
def find(a, x):
if a[x] != x:
a[x] = find(a, a[x])
return a[x]
def unite(a, x, y):
x = find(a, x)
y = find(a, y)
if x != y:
a[x] = min(x, y)
a[y] = a[x]
ans = 0
for i in range(N - 1):
for j in range(i + 1, N):
if j in G[i]:
Gn = copy.deepcopy(G)
Gn[i].discard(j)
a = [i for i in range(N)]
for k in range(N - 1):
for l in Gn[k]:
unite(a, k, l)
par = set()
for k in range(N):
par.add(find(a, k))
if len(par) > 1:
ans += 1
print(ans)
| p03575 |
N, M = [int(a) for a in input().split()]
adj = [[False] * N for _ in range(N)]
edgelist = []
for i in range(M):
a, b = [int(a) for a in input().split()]
a-=1
b-=1
adj[a][b] = True
adj[b][a] = True
edgelist.append((a,b))
bridges = 0
for a,b in edgelist:
adj[a][b] = False
adj[b][a] = False
marked = [False] * N
stk = [a]
while stk:
x = stk.pop()
marked[x] = True
for i in range(N):
if adj[x][i] and not marked[i]:
stk.append(i)
if not marked[b]:
bridges += 1
adj[a][b] = True
adj[b][a] = True
print(bridges) | def f(a, b, nodes, visited):
if (a in visited):
return False
if (a == b):
return True
visited.append(a)
for e in nodes[a]:
if (f(e, b, nodes, visited)):
return True
return False
def main():
n, m = [int(x) for x in input().split(' ')]
nodes = {}
edges = []
for _ in range(m):
a, b = [int(x) for x in input().split(' ')]
if (a in nodes):
nodes[a].append(b)
else:
nodes[a] = [b]
if (b in nodes):
nodes[b].append(a)
else:
nodes[b] = [a]
edges.append((a, b))
ans = 0
for a, b in edges:
nodes[a] = [x for x in nodes[a] if x != b]
nodes[b] = [x for x in nodes[b] if x != a]
if (not f(a, b, nodes, [])):
ans += 1
nodes[a].append(b)
nodes[b].append(a)
print(ans)
main()
| p03575 |
from sys import setrecursionlimit
#setrecursionlimit(10**5)
class UnionFind:
def __init__(self, n):
# 親要素のノード番号を格納
# par[x]=xの時そのノードは根
self.par = [i for i in range(n + 1)]
# rank
self.rank = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
# 根の番号を返す(所属する集合を確認する)
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def check(self, x, y):
# x,yが同じ集合に属するかどうか調べる
return self.find(x) == self.find(y)
def union(self, x, y):
# 根を探す
x = self.find(x)
y = self.find(y)
if x == y:return
# rankの小さいものを大きいほうにくっつける
if self.rank[x] < self.rank[y]:
self.par[x] = y
self.size[y] += self.size[x]
else:
self.par[y] = x
self.size[x] += self.size[y]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def count_group(self):
return len({self.find(x) for x in range(1, n + 1)})
n, m = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(m)]
cnt = 0
for i in range(m):
uf = UnionFind(n)
for j in range(m):
if i == j:continue
a, b = ab[j]
uf.union(a, b)
if uf.count_group() > 1:cnt += 1
print(cnt) | from sys import setrecursionlimit
#setrecursionlimit(10**5)
class UnionFind:
def __init__(self, n):
# 親要素のノード番号を格納
# par[x]=xの時そのノードは根
self.par = [i for i in range(n + 1)]
# rank
self.rank = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
# 根の番号を返す(所属する集合を確認する)
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def check(self, x, y):
# x,yが同じ集合に属するかどうか調べる
return self.find(x) == self.find(y)
def union(self, x, y):
# 根を探す
x = self.find(x)
y = self.find(y)
if x == y:return
# rankの小さいものを大きいほうにくっつける
if self.rank[x] < self.rank[y]:
self.par[x] = y
self.size[y] += self.size[x]
else:
self.par[y] = x
self.size[x] += self.size[y]
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
def count_group(self, n):
return len({self.find(x) for x in range(1, n + 1)})
n, m = list(map(int,input().split()))
ab = [list(map(int,input().split())) for i in range(m)]
cnt = 0
for i in range(m):
uf = UnionFind(n)
for j in range(m):
if i == j:continue
a, b = ab[j]
uf.union(a, b)
if uf.count_group(n) != 1:cnt += 1
print(cnt) | p03575 |
from collections import deque
from copy import deepcopy
N,M = list(map(int,input().split()))
Matrix = [[False for x in range(N)] for y in range(N)]
Path = []
for i in range(M) :
a,b = list(map(int,input().split()))
a -= 1
b -= 1
Path.append((a,b))
Matrix[a][b] = True
Matrix[b][a] = True
ans = 0
for i in range(M) :
Matrix_duplicated = deepcopy(Matrix)
a,b = Path[i]
Matrix_duplicated[a][b] = False
Matrix_duplicated[b][a] = False
Visited = []
Queue = deque([0])
while Queue :
now = Queue.popleft()
Visited.append(now)
for i in range(N) :
if Matrix_duplicated[now][i] :
Matrix_duplicated[now][i] = False
Matrix_duplicated[i][now] = False
Queue.append(i)
if len(list(set(Visited))) != N :
ans += 1
print(ans)
| class UnionFind:
def __init__(self, num):
self.rank = [0] * num
self.par = [i for i in range(num)]
self.n = num
def find_root(self, node):
if self.par[node] == node:
return node
else:
self.par[node] = self.find_root(self.par[node])
return self.par[node]
def same_root(self, x, y):
return self.find_root(x) == self.find_root(y)
def union(self, x, y):
x = self.find_root(x)
y = self.find_root(y)
if x == y:
return
if self.rank[x] > self.rank[y]:
self.par[y] = x
else:
self.par[x] = y
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
def main():
N, M = list(map(int, input().split()))
L = [list(map(int,input().split())) for i in range(M)]
for i in range(M) :
L[i][0] -= 1
L[i][1] -= 1
ans = 0
for i in range(M) :
UF = UnionFind(N)
for j in range(M) :
if j == i :
continue
UF.union(L[j][0], L[j][1])
ans += (UF.same_root(L[i][0], L[i][1]) == False)
print(ans)
import sys
input = sys.stdin.readline
if __name__ == "__main__":
main()
| p03575 |
N, M = list(map(int,input().split()))
v = [i for i in range(1,N+1)]
e = [[]]*(N+1)
for i in range(M):
a, b = list(map(int,input().split()))
e[a] = e[a] + [b]
e[b] = e[b] + [a]
def dfs(v, e):
for num in v:
if(len(e[num]) == 1):
tmp_num = e[num][0]
v.remove(num)
e[num] = []
e[tmp_num].remove(num)
#print(e,v)
return dfs(v, e)+1
return 0
print((dfs(v, e))) | N, M = list(map(int,input().split()))
v = list(range(1,N+1))
edges = [[] for _ in range(N+1)]
for _ in range(M):
a, b = list(map(int,input().split()))
edges[a] += [b]
edges[b] += [a]
def dfs(v, e):
for x in v:
if(len(e[x]) == 1):
t = e[x][0]
e[x] = []
e[t].remove(x)
v.remove(x)
return dfs(v, e)+1
return 0
print((dfs(v, edges))) | p03575 |
from collections import defaultdict
from copy import deepcopy
def dfs(graph, v, visited):
visited.add(v)
for nv in graph[v]:
if nv not in visited:
dfs(graph, nv, visited)
return visited
def is_connected(graph, nodes_cnt):
visited = dfs(graph, 1, set())
if len(visited) == nodes_cnt:
return True
else:
return False
N, M = list(map(int, input().split()))
edges = [list(map(int, input().split())) for _ in range(M)]
G = defaultdict(set)
for u, v in edges:
G[u].add(v)
G[v].add(u)
ans = 0
for u, v in edges:
H = deepcopy(G)
H[u] -= {v}
H[v] -= {u}
if not is_connected(H, N):
ans += 1
print(ans)
| class DisjointSet:
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -self.parents[self.find(x)]
N, M, *ab = list(map(int, open(0).read().split()))
edges = [(a - 1, b - 1) for a, b in zip(*[iter(ab)] * 2)]
ans = 0
for i in range(M):
ds = DisjointSet(N)
for j in range(M):
if i == j:
continue
ds.union(*edges[j])
if len([-p for p in ds.parents if p < 0]) > 1:
ans += 1
print(ans)
| p03575 |
n,m=[int(i) for i in input().split()]
pp=[[0 for i in range(2)] for j in range(m)]
ans=0
#深さ優先探索---------------------------------
#参照元:Python3でグラフの実装&深さ優先探索・幅優先探索の実装
#http://muromura.hatenablog.com/entry/2017/07/16/025414
def dfs(Graph, Start, Vn, Visited=None):
##追加(空から読んでも動くように修正)-----------------------
if Graph[Start]==[] and Visited==None:
Next=Start+1
dfs(Graph, Next, Vn, Visited) #再帰
else:
##--------------------------------------------------------
if (Visited==None): Visited = [] #開始点のときに探索済みリストをつくる
Visited.append(Start) #現在の頂点を探索済みリストに加える
# print(Start, "\n", Visited) #現在の頂点と探索済みリストを表示
# print(len(Visited))
if len(Visited)==Vn:
global ans
ans=ans+1
for Next in Graph[Start]: #次に探索する頂点の候補
if ( Next in Visited ): continue #訪問済みリストにある頂点は飛ばす
dfs(Graph, Next, Vn, Visited) #再帰
#--------------------------------------------------------
for i in range(m) :
pp[i] =[int(i) for i in input().split()]
for i in range(m) : #辺を1つ削除してその辺が橋かどうかを深さ優先探索で調べる
p=pp[:]
del p[i]
#隣接リスト
l = [[] for i in range(n+1)] #頂点の数+1
for pi in p: #0-originedに注意
l[pi[0]].append(pi[1])
l[pi[1]].append(pi[0])
# print(l)
#頂点0(1)から探索
dfs(l, 0, n)
# print(ans)
print((m-ans)) | n,m=[int(i) for i in input().split()]
pp=[[0 for i in range(2)] for j in range(m)]
not_bridge=0
#深さ優先探索-------------------------------------------------------------
#参照元:Python3でグラフの実装&深さ優先探索・幅優先探索の実装
#http://muromura.hatenablog.com/entry/2017/07/16/025414
def dfs(Graph, Start, Vn, Visited=None):
##追加(空から読んでも動くように修正)-----------------------
if Graph[Start]==[] and Visited==None:
Next=Start+1
dfs(Graph, Next, Vn, Visited) #再帰
else:
##--------------------------------------------------------
if (Visited==None): Visited = [] #開始点のときに探索済みリストをつくる
Visited.append(Start) #現在の頂点を探索済みリストに加える
# print(Start, "\n", Visited) #現在の頂点と探索済みリストを表示
if len(Visited)==Vn: #全点訪問済みの場合、橋ではない
global not_bridge
not_bridge=not_bridge+1
for Next in Graph[Start]: #次に探索する頂点の候補
if ( Next in Visited ): continue #訪問済みリストにある頂点は飛ばす
dfs(Graph, Next, Vn, Visited) #再帰
#-----------------------------------------------------------------------
for i in range(m) :
pp[i] =[int(i) for i in input().split()]
for i in range(m) : #辺を1つ削除してその辺が橋かどうかを深さ優先探索で調べる
p=pp[:]
del p[i]
#隣接リスト
l = [[] for i in range(n+1)] #頂点の数+1
for pi in p: #0-originedに注意、l[0]は空
l[pi[0]].append(pi[1])
l[pi[1]].append(pi[0])
#頂点0(1)から探索
dfs(l, 0, n)
print((m-not_bridge)) | p03575 |
class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0: return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y: return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
n, m = list(map(int, input().split()))
edge = []
for i in range(m):
a, b = list(map(int, input().split()))
edge.append([a - 1, b - 1])
ans = 0
for i in range(m):
uf = UnionFind(n)
for j in range(m):
if i == j: continue
uf.unite(edge[j][0], edge[j][1])
ans += (len(uf.roots()) != 1)
print(ans)
| class UnionFind():
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def unite(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
n, m = list(map(int, input().split()))
edge = []
for i in range(m):
a, b = list(map(int, input().split()))
edge.append([a - 1, b - 1])
ans = 0
for i in range(m):
uf = UnionFind(n)
for j in range(m):
if i != j: uf.unite(edge[j][0], edge[j][1])
ans += not uf.same(edge[i][0], edge[i][1])
print(ans)
| p03575 |
import copy
def j(n):
if n:print("Yes")
else:print("No")
exit(0)
rem = 10 ** 9 + 7
"""
def ct(x,y):
if (x>y):print("+")
elif (x<y): print("-")
else: print("?")
"""
def ip():
return int(eval(input()))
def iprow():
return [int(i) for i in input().split()]
def ips():
return (int(i) for i in input().split())
def ipmultiplerow(n):
a = []
for i in range(n):
a.append(ip())
return a
def printrow(a):
for i in a:
print(i)
n,m = ips()
a = []
s = 0
for i in range(m):
x,y = ips()
a.append([x,y])
connection = [[] for i in range(n+1)]
for i in range(m):
[x,y] = a[i]
connection[x].append(y)
connection[y].append(x)
for loop in range(100):
for i in range(1,n+1):
if len(connection[i]) == 1:
temp = connection[i][0]
d = connection[temp].index(i)
connection[temp].pop(d)
s+=1
connection[i].pop(0)
print(s) | import copy
def j(n):
if n:print("Yes")
else:print("No")
exit(0)
rem = 10 ** 9 + 7
"""
def ct(x,y):
if (x>y):print("+")
elif (x<y): print("-")
else: print("?")
"""
def ip():
return int(eval(input()))
def iprow():
return [int(i) for i in input().split()]
def ips():
return (int(i) for i in input().split())
def ipmultiplerow(n):
a = []
for i in range(n):
a.append(ip())
return a
def printrow(a):
for i in a:
print(i)
n,m = ips()
a = []
s = 0
for i in range(m):
x,y = ips()
a.append([x,y])
connection = [[] for i in range(n+1)]
for i in range(m):
[x,y] = a[i]
connection[x].append(y)
connection[y].append(x)
for loop in range(50):
for i in range(1,n+1):
if len(connection[i]) == 1:
temp = connection[i][0]
d = connection[temp].index(i)
connection[temp].pop(d)
s+=1
connection[i].pop(0)
print(s) | p03575 |
import sys
from collections import deque
input = sys.stdin.readline
def dfs(G, v, visited):
visited[v] = True
stack = deque([v])
while stack:
v = stack.pop()
for u in G[v]:
if visited[u]:
continue
visited[u] = True
stack.append(u)
def main():
N, M = list(map(int, input().split()))
G = [[] for _ in range(N)]
a = [0] * M
b = [0] * M
for i in range(M):
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])
ans = 0
for i in range(M):
G[a[i]].remove(b[i])
G[b[i]].remove(a[i])
visited = [False] * N
dfs(G, 0, visited)
if not all(visited):
ans += 1
G[a[i]].append(b[i])
G[b[i]].append(a[i])
print(ans)
if __name__ == "__main__":
main()
| import sys
input = sys.stdin.readline # NOQA
sys.setrecursionlimit(10 ** 7) # NOQA
def dfs(G, v, visited):
"""G: graph, v: vertex"""
visited[v] = True
for c in G[v]:
if visited[c]:
continue
dfs(G, c, visited)
def main():
N, M = list(map(int, input().split()))
a = [None] * M
b = [None] * M
G = [[] for _ in range(N)]
for i in range(M):
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])
ans = 0
for i in range(M):
# remove an adge
G[a[i]].remove(b[i])
G[b[i]].remove(a[i])
visited = [False] * N
visited[0] = True
dfs(G, 0, visited)
if visited.count(True) != N:
ans += 1
# restore an edge
G[a[i]].append(b[i])
G[b[i]].append(a[i])
print(ans)
if __name__ == "__main__":
main()
| p03575 |
n, m = list(map(int, input().split()))
ab = [list(map(int, input().split())) for _ in range(m)]
def bfs(i, edges, r):
if all(r):
return True
if r[i]:
return False
r[i] = True
if any([bfs(e, edges, r) for e in edges[i]]):
return True
return False
ans = 0
for i in range(m):
edges = [[] for _ in range(n)]
for j in range(m):
if i != j:
edges[ab[j][0]-1].append(ab[j][1]-1)
edges[ab[j][1]-1].append(ab[j][0]-1)
reached = [False for _ in range(n)]
if not bfs(0, edges, reached):
ans += 1
print(ans) | def dfs(i,edge):
if vis[i]==True:
return
vis[i]=True
for j in edge[i]:
dfs(j,edge)
N,M=list(map(int,input().split()))
ab=[[0,0] for _ in range(M)]
for i in range(M):
a,b=list(map(int,input().split()))
ab[i][0],ab[i][1]=a,b
ans=0
for i in range(M):
edge=[[] for _ in range(N+1)]
for j in range(M):
if j!=i:
edge[ab[j][0]].append(ab[j][1])
edge[ab[j][1]].append(ab[j][0])
vis=[False]*(N+1)
vis[0]=True
dfs(1,edge)
if False in vis:
ans+=1
print(ans) | p03575 |
n, m = list(map(int, input().split()))
brides = []
for _ in range(m):
a, b = list(map(int, input().split()))
brides.append((a, b))
class Union:
def __init__(self, val):
self.val = val
self.parent = self
self.rank = 0
def get_root(self):
n = self
while n.parent != n:
n = n.parent
return n
def find_set(self):
return self.get_root().val
ans = 0
for i in range(m):
unions = []
for val in range(n):
unions.append(Union(val))
for index, (a, b) in enumerate(brides):
if i == index:
continue
x, y = unions[a - 1], unions[b - 1]
x_root = x.get_root()
y_root = y.get_root()
if x_root.rank == y_root.rank:
x_root.rank += 1
y_root.parent = x_root
elif x_root.rank > y_root.rank:
y_root.parent = x_root
else:
x_root.parent = y_root
groups = [None] * n
for j, u in enumerate(unions):
root = u.find_set()
groups[j] = root
for g in range(1, n):
if groups[g - 1] != groups[g]:
ans += 1
break
print(ans) | # https://atcoder.jp/contests/abc075/tasks/abc075_c
# 各頂点を結ぶクエリー(a, bの情報)ごとに、
# それを実行しなくてもたどり着ける頂点が総頂点数(n)に一致するかを判定する。
n, m = list(map(int, input().split()))
queries = []
for _ in range(m):
a, b = list(map(int, input().split()))
queries.append((a, b))
def dfs(graph, n):
visited = [False] * (n + 1)
visited[0] = True # 1-indexedなので、0番目は関係ない
stack = [1]
num = 0
while stack:
t = stack.pop()
for node in graph[t]:
if visited[node]:
continue
visited[node] = True
stack.append(node)
num += 1
if num == n:
return True
return False
ans = 0
for i in range(m):
graph = [[] for _ in range(n + 1)]
for j in range(m):
if i == j:
continue
a, b = queries[j]
graph[a].append(b)
graph[b].append(a)
if not dfs(graph, n):
ans += 1
print(ans) | p03575 |
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())
nmax=50
graph=[[False]*nmax for _ in range(nmax)]
visited=[False]*nmax
def dfs(n,v):
visited[v]=True
for v2 in range(n):
if graph[v][v2]==False:
continue
if visited[v2]:
continue
dfs(n,v2)
def main():
n,m=LI()
a=[]
b=[]
for _ in range(m):
_a,_b=LI()
a.append(_a-1)
b.append(_b-1)
graph[_a-1][_b-1]=True
graph[_b-1][_a-1]=True
ans=0
for i in range(m):
graph[a[i]][b[i]]=False
graph[b[i]][a[i]]=False
for j in range(n):
visited[j]=False
dfs(n,j)
bridge=False
for j in range(n):
if visited[j]==False:
bridge=True
if bridge:
ans+=1
graph[a[i]][b[i]]=True
graph[b[i]][a[i]]=True
return ans
# main()
print((main()))
| import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())
limit=50
graph=[[False]*limit for _ in range(limit)]
visited=[False]*limit
def dfs(n,v):
visited[v]=True
for v2 in range(n):
if graph[v][v2]==False:
continue
if visited[v2]:
continue
dfs(n,v2)
def main():
n,m=LI()
a=[[] for _ in range(n*n)]
b=[[] for _ in range(n*n)]
for i in range(m):
_a,_b=LI()
_a-=1
_b-=1
a[i]=_a
b[i]=_b
graph[_a][_b]=True
graph[_b][_a]=True
ans=0
for i in range(m):
graph[a[i]][b[i]]=False
graph[b[i]][a[i]]=False
for j in range(n):
visited[j]=False
dfs(n,0)
no_visited=False
for j in range(n):
if visited[j]==False:
no_visited=True
if no_visited:
ans+=1
graph[a[i]][b[i]]=True
graph[b[i]][a[i]]=True
return ans
# main()
print((main()))
| p03575 |
n,m=list(map(int, input().split()))
ab=[list(map(int,input().split())) for i in range(m)]
from pprint import pprint
l=[[False for _ in range(n)] for _ in range(n)]
v=[True for _ in range(n)]
for a,b in ab:
l[b-1][a-1], l[a-1][b-1]=True,True
def dfs(x,t,v,j,m,n):
j+=1
if j<=m:
v[x]=False
for i in range(1,n):
if t[x][i]:
dfs(i,t,v,j,m,n)
import copy
ans=0
for i in range(m):
j=0
t=copy.deepcopy(l)
t[ab[i][1]-1][ab[i][0]-1],t[ab[i][0]-1][ab[i][1]-1]=False,False
v=[True for _ in range(n)]
if i!=0:
dfs(ab[0][0]-1, t, v, j, m,n)
else:
dfs(ab[1][0]-1, t, v, j, m,n)
z=False
for x in v:
if x:
z=True
if z:
ans+=1
print(ans) | n,m=list(map(int, input().split()))
ab=[list(map(int,input().split())) for i in range(m)]
from pprint import pprint
l=[[False for _ in range(n)] for _ in range(n)]
v=[True for _ in range(n)]
for a,b in ab:
l[b-1][a-1], l[a-1][b-1]=True,True
def dfs(x,t,v,j,m,n):
j+=1
if j<n+m:
v[x]=False
for i in range(1,n):
if v[i]:
if t[x][i]:
dfs(i,t,v,j,m,n)
import copy
ans=0
for i in range(m):
j=0
t=copy.deepcopy(l)
t[ab[i][1]-1][ab[i][0]-1],t[ab[i][0]-1][ab[i][1]-1]=False,False
v=[True for _ in range(n)]
dfs(0, t, v, j, m,n)
z=False
for x in v:
if x:
z=True
if z:
ans+=1
print(ans) | p03575 |
def C_Bridge(N, M, E):
"""
N:頂点の数
M:辺の数
E:辺で結ばれる頂点の組
"""
def dfs(v):
# 現在の頂点を訪問済にする
visited[v] = True
for v2 in range(N):
# 現在の頂点と次の頂点に辺がない。スキップ
if adjacent[v][v2] == False:
continue
# 次の頂点が訪問済。スキップ
if visited[v2] == True:
continue
dfs(v2)
# 隣接行列の構成
adjacent = [[0 for _ in range(N)] for _ in range(N)]
a = [0 for _ in range(M)]
b = [0 for _ in range(M)] # a[i],b[i]がi番目の辺を表す
for i in range(M):
a[i] = E[i][0] - 1
b[i] = E[i][1] - 1
adjacent[a[i]][b[i]] = True
adjacent[b[i]][a[i]] = True
# 各辺が橋であるか?
# グラフから辺を取り除いて、グラフが連結かを判定
ans = 0
visited = [False for _ in range(N)]
for i in range(M):
# 辺を取り除く
adjacent[a[i]][b[i]] = False
adjacent[b[i]][a[i]] = False
# すべての辺を未訪問にする
for j in range(N):
visited[j] = False
# 頂点0からDFS
dfs(0)
bridge = False
for j in range(N):
# すべての頂点が訪問済(訪問できた)なら連結、そうでなければ非連結
if visited[j] == False:
bridge = True
if bridge:
ans += 1
adjacent[a[i]][b[i]] = True
adjacent[b[i]][a[i]] = True
return ans
N,M = [int(i) for i in input().split()]
E = [[int(i) for i in input().split()] for j in range(M)]
print((C_Bridge(N, M, E))) | def c_bridge(N, M, E):
"""
N:頂点の数
M:辺の数
E:辺で結ばれる頂点の組
"""
def dfs(v):
# 現在の頂点を訪問済にする
visited[v] = True
for v2 in range(N):
# 現在の頂点と次の頂点に辺がない。スキップ
if adjacent[v][v2] == False:
continue
# 次の頂点が訪問済。スキップ
if visited[v2] == True:
continue
dfs(v2)
# 隣接行列の構成
adjacent = [[0 for _ in range(N)] for _ in range(N)]
for a, b in E:
a, b = a - 1, b - 1
adjacent[a][b] = True
adjacent[b][a] = True
# 各辺が橋であるか?
# グラフから辺を取り除いて、グラフが連結かを判定
ans = 0
for a, b in E:
a, b = a - 1, b - 1
# 辺を取り除く
adjacent[a][b] = False
adjacent[b][a] = False
# すべての辺を未訪問として初期化
visited = [False for _ in range(N)]
# 頂点0からDFS
dfs(0)
# すべての頂点が訪問済なら連結、そうでなければ非連結
if not all(visited):
ans += 1
# 辺を復元
adjacent[a][b] = True
adjacent[b][a] = True
return ans
N,M = [int(i) for i in input().split()]
E = [[int(i) for i in input().split()] for j in range(M)]
print((c_bridge(N, M, E))) | p03575 |
from collections import deque
def BFS(N,M,List):
Edge = [[] for TN in range(N+1)]
for TM in range(0,M):
Edge[List[TM][0]].append(List[TM][1])
Edge[List[TM][1]].append(List[TM][0])
Distance = [-1]*(N+1)
Distance[0] = 0
Distance[1] = 0
From = [0]*(N+1)
From[1] = 1
Deque = deque()
Deque.append(1)
while Deque:
Now = Deque.popleft()
for Con in Edge[Now]:
if Distance[Con]==-1:
Distance[Con] = Distance[Now]+1
Deque.append(Con)
From[Con] = Now
return Distance[1:],From[1:]
import copy
N,M = (int(T) for T in input().split())
List = [[] for TM in range(0,M)]
for TM in range(0,M):
List[TM] = [int(T) for T in input().split()]
Count = 0
for TM in range(0,M):
ListCopy = copy.deepcopy(List)
del ListCopy[TM]
Distance,From = BFS(N,M-1,ListCopy)
if -1 in Distance:
Count += 1
print(Count) | def FindParent(X):
if Parent[X]==X:
return X
else:
Parent[X] = FindParent(Parent[X])
return Parent[X]
def UniteParent(X,Y):
X = FindParent(X)
Y = FindParent(Y)
if X==Y:
return 0
if Rank[X]<Rank[Y]:
Parent[X] = Y
else:
Parent[Y] = X
if Rank[X]==Rank[Y]:
Rank[X] += 1
import copy
N,M = (int(T) for T in input().split())
List = [[] for TM in range(0,M)]
for TM in range(0,M):
List[TM] = [int(T) for T in input().split()]
Count = 0
for TM in range(0,M):
ListCopy = copy.deepcopy(List)
del ListCopy[TM]
Parent = [I for I in range(N+1)]
Rank = [0]*(N+1)
for TTM in range(0,M-1):
UniteParent(ListCopy[TTM][0],ListCopy[TTM][1])
for TN in range(1,N+1):
FindParent(TN)
if len(set(Parent[1:]))>1:
Count += 1
print(Count) | p03575 |
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
class UnionFind:
# Reference: https://note.nkmk.me/python-union-find/
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def all_group_members(self):
return {r: self.members(r) for r in self.roots()}
def __str__(self):
return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
N, M, *AB = list(map(int, read().split()))
A = AB[::2]
B = AB[1::2]
ans = 0
for i in range(M):
uf = UnionFind(N)
for j, (a, b) in enumerate(zip(A, B)):
if i != j:
uf.union(a - 1, b - 1)
if uf.group_count() > 1:
ans += 1
print(ans)
| import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
class UnionFind:
# Reference: https://note.nkmk.me/python-union-find/
def __init__(self, n):
self.n = n
self.parents = [-1] * n
def find(self, x):
if self.parents[x] < 0:
return x
else:
self.parents[x] = self.find(self.parents[x])
return self.parents[x]
def union(self, x, y):
x = self.find(x)
y = self.find(y)
if x == y:
return
if self.parents[x] > self.parents[y]:
x, y = y, x
self.parents[x] += self.parents[y]
self.parents[y] = x
def size(self, x):
return -self.parents[self.find(x)]
def same(self, x, y):
return self.find(x) == self.find(y)
def members(self, x):
root = self.find(x)
return [i for i in range(self.n) if self.find(i) == root]
def roots(self):
return [i for i, x in enumerate(self.parents) if x < 0]
def group_count(self):
return len(self.roots())
def main():
N, M = list(map(int, readline().split()))
AB = [[int(s) - 1 for s in line.split()] for line in readlines()]
ans = 0
for i in range(M):
uf = UnionFind(N)
for j, (a, b) in enumerate(AB):
if i == j:
continue
uf.union(a, b)
if uf.group_count() > 1:
ans += 1
print(ans)
return
if __name__ == '__main__':
main()
| p03575 |
# https://atcoder.jp/contests/abc075/tasks/abc075_c
class UnionFind():
def __init__(self, N):
self._root = [-1 for _ in range(N)]
def find(self, x):
if self._root[x] < 0: return x
self._root[x] = self.find(self._root[x])
return self._root[x]
def unite(self, x, y):
gx = self.find(x)
gy = self.find(y)
if gx == gy: return False
if self._root[gx] > self._root[gy]:
gx, gy = gy, gx
self._root[gx] += self._root[gy]
self._root[gy] = gx
return True
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -(self._root[self.find(x)])
def roots(self):
return [i for i in self._root if i < 0]
n, m = list(map(int, input().split()))
nodes = []
for _ in range(m):
a, b = list(map(int, input().split()))
nodes.append((a, b))
cnt = 0
for i in range(m):
uf = UnionFind(n)
for j in range(m):
if i != j:
uf.unite(nodes[j][0]-1, nodes[j][1]-1)
cnt += (len(uf.roots()) != 1)
print(cnt) | # I used the below code as a reference.
# https://atcoder.jp/contests/abc075/submissions/10369947
class UnionFind():
def __init__(self, N):
self._root = [-1 for _ in range(N)]
def find(self, x):
if self._root[x] < 0: return x
self._root[x] = self.find(self._root[x])
return self._root[x]
def unite(self, x, y):
gx = self.find(x)
gy = self.find(y)
if gx == gy: return False
if self._root[gx] > self._root[gy]:
gx, gy = gy, gx
self._root[gx] += self._root[gy]
self._root[gy] = gx
return True
def same(self, x, y):
return self.find(x) == self.find(y)
def size(self, x):
return -(self._root[self.find(x)])
def roots(self):
return [i for i in self._root if i < 0]
n, m = list(map(int, input().split()))
nodes = []
for _ in range(m):
a, b = list(map(int, input().split()))
nodes.append((a, b))
cnt = 0
for i in range(m):
uf = UnionFind(n)
for j in range(m):
if i != j:
uf.unite(nodes[j][0]-1, nodes[j][1]-1)
cnt += (len(uf.roots()) != 1)
print(cnt) | p03575 |
# ABC075 C - Bridge
def dfs(v):
visited[v]=True
for nv in range(n):
if G[v][nv]==False:
continue
if visited[nv]:
continue
dfs(nv)
n,m = list(map(int,input().split()))
G = [[False]*n for _ in range(n)]
ans = 0
visited = [False for _ in range(n)]
A,B = [],[]
for i in range(m):
a,b = list(map(int,input().split()))
A.append(b-1)
B.append(a-1)
G[a-1][b-1]=G[b-1][a-1]=True
for i in range(m):
G[A[i]][B[i]]=G[B[i]][A[i]]=False
visited = [False for _ in range(n)]
dfs(0)
if all(visited):
ans += 1
G[A[i]][B[i]]=G[B[i]][A[i]]=True
print((m-ans))
| class UnionFind:
def __init__(self, n):
self.p = [-1]*n
# union by rank
self.r = [1]*n
def find(self, x):
if self.p[x] < 0:
return x
else:
self.p[x] = self.find(self.p[x])
return self.p[x]
def union(self, x, y):
rx, ry = self.find(x), self.find(y)
if rx != ry:
if self.r[rx] > self.r[ry]:
rx, ry = ry, rx
if self.r[rx] == self.r[ry]:
self.r[ry] += 1
self.p[ry] += self.p[rx]
self.p[rx] = ry
def same(self, x, y):
return self.find(x) == self.find(y)
def count_member(self, x):
return -self.p[self.find(x)]
n,m=list(map(int,input().split()))
e=[]
for i in range(m):
a,b=list(map(int,input().split()))
e.append((a-1,b-1))
ans=0
for i in range(m):
uf=UnionFind(n)
ok=False
for j in range(m):
if i!=j:
a,b=e[j]
uf.union(a,b)
for k in range(n):
t=uf.count_member(k)
if t!=n:
ok=True
break
if ok:ans+=1
print(ans)
| p03575 |
def multival(): return list(map(int,input().split()))
def data(N=1): return [list(map(int,input().split())) for _ in range(N)]
mod = 10**9 + 7
inf = float("inf")
N,M = multival()
nemat = [[0]*N for _ in range(N)]
ans = M
info = []
for _ in range(M):
a,b = multival()
a -= 1; b-= 1
nemat[a][b] = 1
nemat[b][a] = 1
info.append([a,b])
def dfs(now,visited):
visited[now] = True
if all(visited):
global ans
ans -= 1
return
for ne in range(N):
if nemat[now][ne] == 1 and visited[ne] is False:
dfs(ne,visited)
for i,j in info:
nemat[i][j] = 0
nemat[j][i] = 0
dfs(0,[False]*N)
nemat[i][j] = 1
nemat[j][i] = 1
print(ans) | class Unionfind:
__slots__ = ['nodes']
def __init__(self, n):
self.nodes = [-1]*n
def root(self, x):
if self.nodes[x] < 0:
return x
else:
root_x = self.root(self.nodes[x])
self.nodes[x] = root_x
return root_x
def unite(self, x, y):
x = self.root(x); y = self.root(y)
if x == y:
return
rank_x = -self.nodes[x]; rank_y = -self.nodes[y]
if rank_x < rank_y:
x, y = y, x
if rank_x == rank_y:
self.nodes[x] -= 1
self.nodes[y] = x
def same(self, x, y):
return self.root(x) == self.root(y)
def rank(self, x):
return -self.nodes[self.root(x)]
N,M = list(map(int,input().split()))
info = [list(map(int,input().split())) for i in range(M)]
def bridge(k):
uf = Unionfind(N)
for i,pair in enumerate(info):
if i == k: continue
a = pair[0] - 1; b = pair[1] - 1
uf.unite(a,b)
s = {uf.root(i) for i in range(N)}
if len(s) != 1:
return True
return False
cnt = 0
for k in range(M):
if bridge(k):
cnt += 1
print(cnt) | p03575 |
import sys
sys.setrecursionlimit(10**5)
a, b, c = list(map(int, input().split()))
M = 100
memo = {}
def dfs(a, b, c):
if (a, b, c) in memo:
return memo[a,b,c]
if a == b == c:
return 0
res = 10**9+7
if a < M:
res = min(res, dfs(a+2, b, c) + 1)
if b < M:
res = min(res, dfs(a, b+2, c) + 1)
if c < M:
res = min(res, dfs(a, b, c+2) + 1)
if a < M and b < M:
res = min(res, dfs(a+1, b+1, c) + 1)
if b < M and c < M:
res = min(res, dfs(a, b+1, c+1) + 1)
if a < M and c < M:
res = min(res, dfs(a+1, b, c+1) + 1)
memo[a, b, c] = res
return res
print((dfs(a, b, c))) | a, b, c= list(map(int, input().split()))
ans = 0
def calc(a, b):
if a < b:
r = b-a
if r % 2:
return r//2 + 2
return r//2
else:
return a-b
def solve(a, b, c):
return calc(a, c) + calc(b, c)
print((min(solve(a, b, c), solve(b, c, a), solve(c, a, b))))
| p03389 |
a, b, c= list(map(int, input().split()))
ans = 0
def calc(a, b):
if a < b:
r = b-a
if r % 2:
return r//2 + 2
return r//2
else:
return a-b
def solve(a, b, c):
return calc(a, c) + calc(b, c)
print((min(solve(a, b, c), solve(b, c, a), solve(c, a, b))))
| a, b, c = sorted(map(int, input().split()))
a = c-a; b = c-b
p = a%2; q = b%2
if p and q:
print((a//2+b//2+1))
elif p or q:
print((a//2+b//2+2))
else:
print((a//2+b//2))
| p03389 |
a=list(map(int,input().split()))
m=max(a)
t=0
if (m*3-sum(a))%2==1:t=3
print(((m*3-sum(a)+t)//2))
| a=list(map(int,input().split()))
m=max(a)
t=m*3-sum(a)
if t%2==1:t+=3
print((t//2))
| p03389 |
def noofrupees(x,y,z):
if(x==y and x==z):
return 0
elif(x==y):
if(x<z):
return z-x
else:
if(x%2==0 and z%2==0):
return int((x-z)/2)
elif(x%2!=0 and z%2!=0):
return int((x-z)/2)
else:
z = z-1
return int((x-z)/2)+1
elif(x==z):
if(x<y):
return y-x
else:
if(x%2==0 and y%2==0):
return int((x-y)/2)
elif(x%2!=0 and y%2!=0):
return int((x-y)/2)
else:
y = y-1
return int((x-y)/2)+1
elif(y==z):
if(y<x):
return x-y
else:
if(y%2==0 and x%2==0):
return int((y-x)/2)
elif(y%2!=0 and x%2!=0):
return int((y-x)/2)
else:
x = x-1
return int((y-x)/2)+1
#t= int(input())
t=1;
for v in range(t):
x,y,z = list(map(int,input().split()))
if(x!=y and x!=z and y!=z):
list1 = [x,y,z]
list1.sort()
s = list1[2] - list1[1]
list1[0]+=s
list1[1]+=s
print((s+noofrupees(list1[0],list1[1],list1[2])))
else:
print((noofrupees(x,y,z)))
| #t=int(input())
t=1
for i in range(t):
x,y,z=list(map(int,input().split()))
ma=max(x,y,z)
mi=min(x,y,z)
if(x==ma):
if(y<z):
m=x-y
n=x-z
else:
m=x-z
n=x-y
elif(y==ma):
if(x<z):
m=y-x
n=y-z
else:
m=y-z
n=y-x
else:
if(x<y):
m=z-x
n=z-y
else:
m=z-y
n=z-x
ans=n+(m-n)//2
if((m-n)%2==1):
ans+=2
print(ans)
| p03389 |
N = int(eval(input()))
Ss = input().replace(" ", '')
part = ""
for n, i in enumerate(Ss):
if n != 0:
if i == part[0]:
Ss_cp = Ss[::1]
Ss_cp = Ss_cp.replace(part, '')
if Ss_cp == '':
print((N//len(part)))
exit()
part += Ss[n]
else:
print((1))
| N = int(eval(input()))
Ss = input().split()
res = 1
for t in range(1, N+1):
if (N%t != 0): continue
f = False
for i in range(N-t):
if (Ss[i] == Ss[i + t]): continue
f = True
break
if (f): continue
res = N // t
break
print(res)
| p01873 |
int_1, int_2 = list(map(int, input().split()))
answer = int_1 + int_2
if 10 <= answer:
print('error')
else:
print(answer) | a, b = list(map(int, input().split()))
answer = a + b
print(('error' if 10 <= answer else answer)) | p03697 |
a,b=list(map(int,input().split()))
print((a+b if a+b<10 else "error")) | A,B = list(map(int,input().split()))
print((A+B if A+B < 10 else "error")) | p03697 |
a,b=list(map(int,input().split()))
if a+b<10:
print((a+b))
else:
print("error") | a,b=list(map(int,input().split()))
print((a+b if a+b<10 else"error")) | p03697 |
a,b = list(map(int,input().split()))
if (a + b) < 10:
print(a+b)
else:
print("error") | a,b = list(map(int,input().split()))
sum = a+b
if sum >= 10:
print("error")
else:
print(sum) | p03697 |
N, K = list(map(int, input().split()))
count = 0
for a in range(1, N+1):
b = K - a % K
while b <= N:
if (a % K) == (b % K):
c = K - a % K
count += (N - c) // K + 1
b += K
print(count)
| N, K = list(map(int, input().split()))
count = 0
for a in range(1, N+1):
b = K - a % K
if (a % K) == (b % K):
count += ((N - b) // K + 1) ** 2
print(count)
| p03266 |
n,k=list(map(int,input().split()))
d=0
for a in range(1,n+1):
for b in range(k-a%k,n+1,k):
for c in range(k-a%k,n+1,k):
if (a+b)%k==0 and (b+c)%k==0 and (c+a)%k==0:d+=1
print(d) | n,k=list(map(int,input().split()))
if k%2==0:
if n%k<k/2:print(((n//k)**3+(n//k)**3))
else:print(((n//k)**3+((n//k)+1)**3))
else:print(((n//k)**3)) | p03266 |
n, k = list(map(int, input().split()))
import itertools
ans = 0
for a, b, c in itertools.product(list(range(1,n+1)), repeat=3):
if (a+b)%k == 0 and (b+c)%k == 0 and (c+a)%k == 0:
ans += 1
print(ans) | n, k = list(map(int, input().split()))
ans = 0
temp = n // k
ans += temp ** 3
if k % 2 == 0:
n += k // 2
temp = n // k
ans += temp ** 3
print(ans) | p03266 |
N, K = list(map(int, input().split()))
num = [0 for _ in range(K)]
for i in range(1, N + 1):
num[i % K] += 1
ans = 0
for a in range(K):
b = (K - a) % K
c = (K - a) % K
if (b + c) % K == 0:
ans += num[a] * num[b] * num[c]
print(ans)
| N, K = list(map(int, input().split()))
num = [0 for _ in range(K)]
for i in range(1, N + 1):
num[i % K] += 1
ans = 0
if K % 2 == 0:
ans += num[0] ** 3 + num[K // 2] ** 3
else:
ans += num[0] ** 3
print(ans)
| p03266 |
N,K = list(map(int,input().split()))
odd = []
even1 = []
even2 = []
if K % 2 == 1:
for i in range(1,N+1):
if i % K == 0:
odd += [i]
ans = len(odd) ** 3
else:
for i in range(1,N+1):
if i % K == 0:
even1 += [i]
elif i % (K/2) == 0:
even2 += [i]
ans = len(even1) ** 3 + len(even2) ** 3
print(ans) | N,K = list(map(int,input().split()))
ary1 = []
ary2 = []
if K % 2 == 1:
for i in range(K,N+1,K):
ary1 += [i]
print((len(ary1)**3))
else:
for i in range(1,N+1):
if i % K == 0:
ary1 += [i]
elif i % (K//2) == 0:
ary2 += [i]
print(((len(ary1) ** 3) + (len(ary2) ** 3)))
| p03266 |
n, k =list(map(int, input().split()))
ans = 0
#kで割り切れるn以下の数のリスト
odd = [i for i in range(k, n+1, k)] #kから始まり、nまで、k刻み
ans += len(odd) ** 3 #abcの選び方のため3乗
if k % 2 == 0:
#k/2から始まり、nまでで、kで割ると余りがk/2になる
even = [i for i in range(k//2, n+1, k)]
ans += len(even) ** 3
print(ans)
|
N,K = list(map(int, input().split()))
a = N // K
b = 0
if K % 2 == 0:
b = a+1 if N - a*K >= K/2 else a
print((a**3 + b**3))
| p03266 |
from itertools import product
N, K = list(map(int, input().split()))
x = list(product(list(range(1, N + 1)), repeat=3))
ans = 0
for q in x:
w = [q[0] + q[1], q[0] + q[2], q[1] + q[2]]
if all(list([x % K == 0 for x in w])):
ans += 1
print(ans) | N, K = list(map(int, input().split()))
if K % 2 == 1:
print((sum(1 for n in range(1, N + 1) if n % K == 0) ** 3))
else:
print((sum(1 for n in range(1, N + 1) if n % K == 0) ** 3 + sum(1 for n in range(1, N + 1) if n % K == K // 2) ** 3))
| p03266 |
N, K = list(map(int, input().split()))
if K % 2 == 1:
print((sum(1 for n in range(1, N + 1) if n % K == 0) ** 3))
else:
print((sum(1 for n in range(1, N + 1) if n % K == 0) ** 3 + sum(1 for n in range(1, N + 1) if n % K == K // 2) ** 3))
| N, K = list(map(int, input().split()))
ans = (N // K) ** 3
ans += ((N + K // 2) // K) ** 3 if K % 2 == 0 else 0
print(ans) | p03266 |
import sys
input=sys.stdin.readline
def main():
N,K = list(map(int,input().split()))
ans = 0
if K%2==0:
a0 = K//2
else:
a0 = K
for a in range(a0, N+1, a0):
for _ in range(K - a%K, N+1, K):
for _ in range(K - a%K, N+1, K):
ans += 1
print(ans)
if __name__ == '__main__':
main() | import sys
input=sys.stdin.readline
def main():
N,K = list(map(int,input().split()))
r1 = 0
for i in range(1,N+1):
if i%K == 0:
r1 += 1
if K%2 == 1:
print((r1**3))
else:
r2 = 0
for i in range(1,N+1):
if i%K == K//2:
r2 += 1
print((r1**3+r2**3))
if __name__ == '__main__':
main() | p03266 |
import sys
input = sys.stdin.readline
def main():
N, K = [int(x) for x in input().split()]
ans = 0
if K & 1:
print(((N // K) ** 3))
else:
ans += (N // K) ** 3
ans += ((N + K // 2) // K) ** 3
print(ans)
if __name__ == '__main__':
main()
| N, K = [int(x) for x in input().split()]
ans = 0
ans += (N // K) ** 3
if K % 2 == 0:
ans += ((N - K // 2) // K + 1) ** 3
print(ans)
| p03266 |
# 初期入力
import sys
input = sys.stdin.readline #文字列では使わない
N,K = list(map(int, input().split()))
#Kの倍数(2N以下)⓵
multip_K =[i for i in range(0,2*N+1,K)]
#multip_K =multip[K::K]
#2数の和が上記⓵ ⇒a,bの候補探す
from collections import defaultdict
ad =defaultdict(list)
ad_set =set()
for i in multip_K:
for j in range(1,N+1):
if 0 < i-j <=N:
ad[i].append((j,i-j))
ad_set.add((j,i-j))
#cを探す
count =0
for i in range(1,N+1):
for ab in list(ad.values()):
for abc in ab:
if (abc[0],i) in ad_set and (abc[1],i) in ad_set:
count +=1
print(count) | #プログラミング日記 一寸先は闇が人生
# https://pyteyon.hatenablog.com/entry/2018/09/02/094228
# 初期入力
import sys
input = sys.stdin.readline #文字列では使わない
N,K = list(map(int, input().split()))
# 偶数、奇数共に
# a,b,c mod 0,0,0 ⇒Kの倍数
multip_K =[i for i in range(K,N+1,K)]
#multip_Kから3つ選び並べる順列組み合わせ
le =len(multip_K)
ans_mod_0 =le **3
# 偶数のみ
# a,b,c mod K/2,K/2,K/2
ans_mod_haf =0
if K%2 ==0:
multip_haf =[i for i in range(1,N+1) if i %K ==K//2 ]
le_haf =len(multip_haf)
ans_mod_haf =le_haf **3
ans =ans_mod_0 +ans_mod_haf
print(ans) | p03266 |
import math
N,K = (int(i) for i in input().split())
count = 0
minus = []
for i in range(1,N+1):
a = i
b = i
c = i
if (a + b) % K == 0:
count += 1
count += len(minus) * (3*2)
if len(minus) >= 2:
kumiawase = math.factorial(len(minus))/(math.factorial(len(minus)-2)*math.factorial(2))
count += kumiawase * (3*2)
if i % K == 0:
minus.append(i)
print((int(count))) | #import math
N,K = (int(i) for i in input().split())
count = 0
minus = []
for i in range(1,N+1):
a = i
b = i
c = i
if (a + b) % K == 0:
count += 1
count += len(minus) * (3*2)
if len(minus) >= 2:
#kumiawase = math.factorial(len(minus))/(math.factorial(len(minus)-2)*math.factorial(2))
kazu = len(minus)
kumiawase = kazu * (kazu - 1) / 2
count += kumiawase * (3*2)
if i % K == 0:
minus.append(i)
print((int(count))) | p03266 |
N, K = list(map(int, input().strip().split()))
count = set()
for a in range(K, N+1, K):
for b in range(K, N + 1, K):
for c in range(K, N + 1, K):
count.add((a, b, c))
if K % 2 == 1:
print((len(count)))
else:
start = K // 2
for a in range(start, N + 1, K):
for b in range(start, N + 1, K):
for c in range(start, N + 1, K):
count.add((a, b, c))
print((len(count))) | N, K = list(map(int, input().strip().split()))
result = 0
for a in range(K, N+1, K):
for b in range(a, N + 1, K):
for c in range(b, N + 1, K):
if a == b and b == c:
result += 1
elif a == b or a == c or b == c:
result += 3
else:
result += 6
if K % 2 == 1:
print(result)
else:
start = K // 2
for a in range(start, N + 1, K):
for b in range(a, N + 1, K):
for c in range(b, N + 1, K):
if a == b and b == c:
result += 1
elif a == b or a == c or b == c:
result += 3
else:
result += 6
print(result) | p03266 |
N, K = list(map(int, input().strip().split()))
result = 0
num = N // K
result += num
if num == 2:
result += num * (num - 1) * 3
elif num > 2:
result += num * (num - 1) * 3
result += num * (num - 1) * (num - 2)
if K % 2 == 1:
print(result)
else:
start = K // 2
for a in range(start, N + 1, K):
for b in range(a, N + 1, K):
for c in range(b, N + 1, K):
if a == b and b == c:
result += 1
elif a == b or b == c:
result += 3
else:
result += 6
print(result) | N, K = list(map(int, input().strip().split()))
result = 0
num = N // K
result += num
if num == 2:
result += num * (num - 1) * 3
elif num > 2:
result += num * (num - 1) * 3
result += num * (num - 1) * (num - 2)
if K % 2 == 1:
print(result)
else:
start = K // 2
num = (N - start) // K + 1
result += num
if num == 2:
result += num * (num - 1) * 3
elif num > 2:
result += num * (num - 1) * 3
result += num * (num - 1) * (num - 2)
print(result) | p03266 |
def check():
a = []
bDict2 = {}
count = 0
removeList = []
N, K = [int(i) for i in input().split(' ')]
for i in range(1,N+1):
if (2*i)%K == 0:
a.append(i)
while a != []:
temp = a[0]
for i in a:
if (temp+i)%K == 0:
removeList.append(i)
if str(temp) not in bDict2:
bDict2[str(temp)] = 1
else:
bDict2[str(temp)] += 1
for i in removeList:
a.remove(i)
removeList.clear()
for i in bDict2:
count += bDict2[i]**3
print(count)
check() | N, K = list(map(int, input().split()))
cnt1,cnt2 = 0,0
for i in range(1,N+1):
if i%K == 0:
cnt1 += 1
elif i%K == K/2:
cnt2 += 1
print((cnt1**3+cnt2**3)) | p03266 |
# -*- coding: utf-8 -*-
import itertools
N, K = list(map(int, input().split()))
Nlst = []
# for i in range(1,N+1):
# Nlst.append(i)
if K % 2 == 0:
for i in range(1,N+1):
if i % K == 0 or i % (K/2) == 0:
Nlst.append(i)
else:
for i in range(1,N+1):
if i % K == 0:
Nlst.append(i)
tmplst = []
for i in itertools.product(Nlst, repeat=3):
tmplst.append(i)
ans = 0
for (a,b,c) in tmplst:
if (a+b) % K == 0 and (b+c) % K == 0 and (c+a) % K == 0:
ans += 1
print(ans)
| # -*- coding: utf-8 -*-
import itertools
N, K = list(map(int, input().split()))
Nlst = []
Nlst_gu = []
# for i in range(1,N+1):
# Nlst.append(i)
if K % 2 == 0:
for i in range(1,N+1):
if i % K == 0:
Nlst.append(i)
elif i % (K/2) == 0:
Nlst_gu.append(i)
else:
for i in range(1,N+1):
if i % K == 0:
Nlst.append(i)
#
# print(Nlst)
# print(Nlst_gu)
ans = len(Nlst)
ans_gu = len(Nlst_gu)
print((ans * ans * ans + ans_gu * ans_gu * ans_gu))
#
# tmplst = []
# for i in itertools.product(Nlst, repeat=3):
# tmplst.append(i)
#
# ans = 0
#
# for (a,b,c) in tmplst:
# if (a+b) % K == 0 and (b+c) % K == 0 and (c+a) % K == 0:
# ans += 1
#
#
# print(ans)
| p03266 |
N,K=list(map(int,input().split()))
if K%2==1:
print(((N//K)**3))
else:
k=K//2
ans=0
for a in range(k,N+1,k):
for b in range(a,N+1,k):
for c in range(b,N+1,k):
if (a+b)%K==0 and (b+c)%K==0 and (c+a)%K==0:
if a==b==c:
ans+=1
elif a==b or b==c or c==a:
ans+=3
else:
ans+=6
print(ans) | N,K=list(map(int,input().split()))
if K%2==1:
print(((N//K)**3))
else:
k=K//2
print(((N//K)**3+((N-k)//K+1)**3)) | p03266 |
import sys
from functools import reduce
from operator import mul
import math
def nCr(n, r):
r = min(r, n-r)
upper = reduce(mul, list(range(n, n-r, -1)), 1)
lower = reduce(mul, list(range(r, 0, -1)), 1)
return upper // lower
def oddCaseK(n, k):
cand = 0
for x in range(1, n+1):
if x % k == 0:
cand += 1
number_of_possibles = 0
if cand >= 1:
number_of_possibles += cand
if cand >= 2:
number_of_possibles += nCr(cand, 2) * nCr(2, 1) * 3
if cand >= 3:
number_of_possibles += nCr(cand, 3) * math.factorial(3)
return number_of_possibles
def evenCaseK(n, k):
cand1 = 0
cand2 = 0
for x in range(1, n+1):
if x % k == 0:
cand1 += 1
elif x % k == k // 2:
cand2 += 1
number_of_possibles = 0
number_of_possibles = 0
if cand1 >= 1:
number_of_possibles += cand1
if cand1 >= 2:
number_of_possibles += nCr(cand1, 2) * nCr(2, 1) * 3
if cand1 >= 3:
number_of_possibles += nCr(cand1, 3) * math.factorial(3)
if cand2 >= 1:
number_of_possibles += cand2
if cand2 >= 2:
number_of_possibles += nCr(cand2, 2) * nCr(2, 1) * 3
if cand2 >= 3:
number_of_possibles += nCr(cand2, 3) * math.factorial(3)
return number_of_possibles
def main():
n, k = [int(x) for x in sys.stdin.readline().split()]
if k % 2 == 0:
ans = evenCaseK(n, k)
else:
ans = oddCaseK(n, k)
print(ans)
if __name__ == "__main__":
main()
| import sys
from functools import reduce
from operator import mul
import math
def nCr(n, r):
r = min(r, n-r)
upper = reduce(mul, list(range(n, n-r, -1)), 1)
lower = reduce(mul, list(range(r, 0, -1)), 1)
return upper // lower
def oddCaseK(n, k):
cand = 0
for x in range(1, n+1):
if x % k == 0:
cand += 1
number_of_possibles = 0
if cand >= 1:
number_of_possibles += cand
if cand >= 2:
number_of_possibles += nCr(cand, 2) * nCr(2, 1) * 3
if cand >= 3:
number_of_possibles += nCr(cand, 3) * math.factorial(3)
return number_of_possibles
def evenCaseK(n, k):
cand1 = 0
cand2 = 0
for x in range(1, n+1):
if x % k == 0:
cand1 += 1
elif x % k == k // 2:
cand2 += 1
number_of_possibles = 0
for cand in [cand1, cand2]:
if cand >= 1:
number_of_possibles += cand
if cand >= 2:
number_of_possibles += nCr(cand, 2) * nCr(2, 1) * 3
if cand >= 3:
number_of_possibles += nCr(cand, 3) * math.factorial(3)
return number_of_possibles
def main():
n, k = [int(x) for x in sys.stdin.readline().split()]
if k % 2 == 0:
ans = evenCaseK(n, k)
else:
ans = oddCaseK(n, k)
print(ans)
if __name__ == "__main__":
main()
| p03266 |
n, k = [int(i) for i in input().split()]
ans = [0, 0]
d = k // 2
for i in range(1, n+1):
if k % 2 == 0 and i % k == d:
ans[0] += 1
if i % k == 0:
ans[1] += 1
print((ans[0] ** 3 + ans[1] ** 3)) | n, k = [int(i) for i in input().split()]
ans = (n // k) ** 3
if k % 2 == 0:
ans += ((n - k // 2) // k + 1) ** 3
print(ans) | p03266 |
from math import *
n,k=list(map(int,input().split()))
dict1={}
flag=0
ans=0
if(k%3==0):
flag=1
for i in range(1,n+1):
try:
dict1[i%k]+=1
except:
KeyError
dict1[i%k]=1
dict2={}
for i in list(dict1.keys()):
try:
if(dict2[i]==1):
a=1
except:
KeyError
if((i+i)%k==0):
ans+=dict1[i]*dict1[i]*dict1[i]
dict2[i]=1
dict2[k-i]=1
print(ans) | from math import *
n,k=list(map(int,input().split()))
dict1={}
flag=0
ans=0
if(k%3==0):
flag=1
for i in range(1,n+1):
try:
dict1[i%k]+=1
except:
KeyError
dict1[i%k]=1
for i in list(dict1.keys()):
if((i+i)%k==0):
ans+=dict1[i]*dict1[i]*dict1[i]
print(ans) | p03266 |
import itertools
n,k = list(map(int,input().split()))
if k%2!=0:
ans = len(list(itertools.product(list(range(1,n//k+1)),repeat=3)))
print(ans)
else:
ans = len(list(itertools.product(list(range(1,n//k+1)),repeat=3))) + len(list(itertools.product(list(range(1,(2*n)//k-n//k+1)),repeat=3)))
print(ans) | import itertools
n,k = list(map(int,input().split()))
if k%2!=0:
ans = (n//k)**3
print(ans)
else:
ans = (n//k)**3 + ((2*n)//k - n//k)**3
print(ans) | p03266 |
n,k = list(map(int,input().split()))
cnt = 0
cnt2 = 0
for i in range(1,n+1):
if k%2 == 0:
if i%k == 0:
cnt += 1
if i%k == k//2:
cnt2 += 1
else:
if i%k == 0:
cnt += 1
print((cnt**3+cnt2**3)) | n,k = list(map(int,input().split()))
if k%2 == 0:
print(((n//k)**3+(n//(k//2)-n//k)**3))
else:
print(((n//k)**3)) | p03266 |
n,k = list(map(int, input().split()))
kk = 2*k
if k % 2:
tmp = (n//k)**3
print(tmp)
exit()
kk = 0
k2 = 0
kkk = 0
for i in range(k//2,n+1):
if i % k == 0:
kk += 1
if i % k == k//2:
k2 += 1
if i % k == 0 and i % k == k//2:
kkk += 1
# print(i, kk, k2, kkk)
print((kk**3 + k2**3 - kkk**3))
| n,k = list(map(int, input().split()))
a = 0
b = 0
c = 0
kk = k // 2
for i in range(1,n+1):
d = 2*i
if k % 2 == 0 and i % k == kk:
a += 1
if i % k == 0:
b += 1
if (k % 2 == 0 and i % k == kk) and i % k == 0:
c += 1
print((a**3 + b**3 - c**3))
# print(a,b,c) | p03266 |
import itertools as itr
N, K = tuple(int(i) for i in input().split())
n = int(N/K)
c1 = list(itr.product(list(range(n)), repeat=3))
c1 = len(c1)
k_2 = K/2
if N < k_2:
print((0))
elif K%2 != 0:
print(c1)
else:
if N < n*K + k_2:
c2 = list(itr.product(list(range(n)), repeat=3))
else:
c2 = list(itr.product(list(range(n+1)), repeat=3))
c2 = len(c2)
print((c1 + c2)) | import math
def cc(n, r):
return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
N, K = tuple(int(i) for i in input().split())
n = int(N/K)
c1 = n**3
k_2 = K/2
if N < k_2:
print((0))
elif K%2 != 0:
print(c1)
else:
if N < n*K + k_2:
c2 = n**3
else:
c2 = (n+1)**3
print((c1 + c2)) | p03266 |
def Count(a,b,c,K):
if (a+b)%K==0 and (a+c)%K==0 and (b+c)%K==0:
return 1;
else:
return 0;
N,K=(int(i) for i in input().split())
count=0
for i in range(1,N+1):
for j in range(1,N+1):
for k in range(1,N+1):
count=count+Count(i,j,k,K)
print(count) | N,K=(int(i) for i in input().split())
countG=0
count=0
if K%2==0:
for i in range(1,N+1):
if i%K==0:
count+=1
elif i%K==K/2:
countG+=1
else:
for i in range(1,N+1):
if i%K==0:
count+=1
print((count*count*count+countG*countG*countG)) | p03266 |
N, K = list(map(int, input().split()))
from itertools import combinations_with_replacement
# a+b % K == 0
# b+c % K == 0
# c+a % K == 0
# a が Kで割り切れる場合
# -> bもcもKで割り切れる
# -> a,b,cともにKの倍数
# -> Kの倍数の個数(N//K) ** 3
# a がKで割れきれない場合
# -> 同じくbもcもKで割り切れない
# Q. Kが奇数の場合
# -> それを満たす(a,b,c)の組み合わせはあり得ない
# よってKは偶数
# K=2のとき例より (a,b,c)=(1,1,1),(1,3,3),...
# K=4のとき(a,b,c)=(2,2,2),(2,6,6),...
# K=6のとき(a,b,c)=(3,3,3),(3,3,9),...
# よりa % K == K/2である必要がある
# a = x*K+K/2 (x=0以上の整数)
num_double = N//K
if K % 2 == 1:
# Kが奇数の場合
# (1) (a,b,c)ともにKの倍数
print((num_double ** 3))
else:
# Kが偶数の場合,
# (1) (a,b,c)ともにKの倍数
# (2) (a,b,c)がいずれも Kで割った余りがK/2となる
s = K // 2
nums = [a for a in range(s, N+1, K)]
count = 0
for a, b, c in combinations_with_replacement(nums, 3):
if a == b and b == c:
count += 1
elif a == b or b == c:
count += 3
else:
count += 6
print((num_double ** 3 + count))
| N, K = list(map(int, input().split()))
def combination(n, r):
ans = 1
for i in range(r):
ans *= n - i
for i in range(r):
ans //= i + 1
return ans
# a+b % K == 0
# b+c % K == 0
# c+a % K == 0
# a が Kで割り切れる場合
# -> bもcもKで割り切れる
# -> a,b,cともにKの倍数
# -> Kの倍数の個数(N//K) ** 3
# a がKで割れきれない場合
# -> 同じくbもcもKで割り切れない
# Q. Kが奇数の場合
# -> それを満たす(a,b,c)の組み合わせはあり得ない
# よってKは偶数
# K=2のとき例より (a,b,c)=(1,1,1),(1,3,3),...
# K=4のとき(a,b,c)=(2,2,2),(2,6,6),...
# K=6のとき(a,b,c)=(3,3,3),(3,3,9),...
# よりa % K == K/2である必要がある
# a = x*K+K/2 (x=0以上の整数)
num_double = N//K
if K % 2 == 1:
# Kが奇数の場合
# (1) (a,b,c)ともにKの倍数
print((num_double ** 3))
else:
# Kが偶数の場合,
# (1) (a,b,c)ともにKの倍数
# (2) (a,b,c)がいずれも Kで割った余りがK/2となる
s = K // 2
nums = [a for a in range(s, N+1, K)]
n = len(nums)
count = 0
count += 6 * combination(n, 3) # a,b,cがいずれも異なる値になるケースはnC3通り 順列は6倍
count += 3 * n * (n-1) # a,b,cのうち2つが同じ値になるケースはn(n-1)通り 順列は3倍
count += n # a,b,cが同じ値になるケースはn通り
print((num_double ** 3 + count))
| p03266 |
import math
n, k = list(map(int, input().split()))
a = int(n/k)
if(a < 2):
b = 0
else:
b = math.factorial(a) // (math.factorial(a - 2) * math.factorial(2))
if(a < 3):
c = 0
else:
c = math.factorial(a) // (math.factorial(a - 3) * math.factorial(3))
ans = a + b * 6 + c * 6
if(k % 2 == 0):
t = int(k / 2)
if(a * k + t > n):
ans = ans * 2
else:
d = a + 1
if(d < 2):
e = 0
else:
e = math.factorial(d) // (math.factorial(d - 2) * math.factorial(2))
if(d < 3):
f = 0
else:
f = math.factorial(d) // (math.factorial(d - 3) * math.factorial(3))
ans += (d + e * 6 + f * 6)
print(ans) | import math
n, k = list(map(int, input().split()))
ans = int(math.pow(int(n/k),3))
if(k % 2 == 0):
m = int( (n+int(k/2))/ k )
ans += int(math.pow(m,3))
print(ans) | p03266 |
from operator import mul
from functools import reduce
def cmb(n,r):
r = min(n-r,r)
if r == 0: return 1
over = reduce(mul, list(range(n, n - r, -1)))
under = reduce(mul, list(range(1,r + 1)))
return over // under
N,K=list(map(int,input().split()))
if K%2==0:
print(((N//K)**3+(((N-K//2)//K+1)**3)))
else:
print(((N//K)**3)) | N,K=list(map(int,input().split()))
if K%2==0:
print(((N//K)**3+(((N-K//2)//K+1)**3)))
else:
print(((N//K)**3)) | p03266 |
N, K = list(map(int, input().split()))
# N, K = 3, 2
if K % 2 != 0:
ans = (N // K)**3
else:
full = []
for i in range(K, N+1, K):
full.append(i)
# print(full)
ans = len(full) ** 3
half = []
for i in range(int(K/2), N+1, int(K/2)):
half.append(i)
half = list(set(half) - set(full))
# print(half)
ans += len(half) ** 3
print(ans) | N, K = list(map(int, input().split()))
# N, K = 3, 2
if K % 2 != 0:
ans = (N // K)**3
else:
full = []
half = []
for i in range(int(K/2), N+1, int(K/2)):
if i % K == 0:
full.append(i)
else:
half.append(i)
ans = len(half) ** 3 + len(full) ** 3
print(ans) | p03266 |
n,k = list(map(int,input().split()))
if k%2 == 1:
cnts = []
for i in range(1,n+1):
if i % k == 0:
cnts.append(i)
print((len(cnts)**3))
else:
k0 = 0
k1 = 0
for i in range(1,n+1):
if i % k == 0:
k0 += 1
elif i %k == k//2:
k1 += 1
print((k0**3 + k1**3)) | n,k = list(map(int,input().split()))
a = 0
a2 = 0
for i in range(1,n+1):
if i % k == 0:
a += 1
elif 2*i % k == 0:
a2 += 1
print((a**3 + a2**3)) | p03266 |
import itertools
N, K = list(map(int, input().split()))
seq = list(range(1,N + 1))
cand = list(itertools.product(seq, repeat=3))
ans = 0
for c in cand:
ab = c[0] + c[1]
bc = c[1] + c[2]
ca = c[2] + c[0]
if ab % K == 0 and bc % K == 0 and ca % K == 0:
ans += 1
print(ans) | N, K = list(map(int, input().split()))
p1 = N // K
p2 = 0
if K % 2 == 0:
p2 = (N + (K // 2)) // K
print((p1**3 + p2**3)) | p03266 |
from itertools import*
n, k = list(map(int, input().split()))
a, b = divmod(n, k)
t = 0 if k%2 else a + (b >= k//2)
j = lambda i: len(list(product(list(range(i)), repeat=3)))
print((j(a)+j(t))) | n,k=list(map(int,input().split()))
print(((n//k) ** 3 + (k%2 == 0) * ((n + k//2) // k) ** 3)) | p03266 |
import collections
n,k=list(map(int,input().split()))
l_n=[(v+1)%k for v in range(n)]
c = collections.Counter(l_n)
m = len(c)
ret=0
for i in range(0,m):
for j in range(0,m):
if (i+j)%k==0:
for r in range(0,m):
if (i+r)%k ==0 and (r+j)%k==0:
ret+=c[i]*c[j]*c[r]
print(ret)
| n, k = list(map(int, input().split()))
print((sum([1 if v % k == 0 else 0 for v in range(1, n+1)])**3 +
sum([1 if v % k == k/2 else 0 for v in range(1, n+1)])**3))
| p03266 |
def logic(N, K):
if K == 1:
return N**3
count = 0
for a in range(1, N + 1):
b_s = K - (a % K)
for b in range(b_s, N + 1, K):
for c in range(b_s, N + 1, K):
if (b + c) % K == 0:
count += 1
return count
def main():
N, K = list(map(int, input().split()))
print((logic(N, K)))
if __name__ == '__main__':
main() | N, K = list(map(int,input().split()))
print((int(N/K)**3+[int((N+K/2)/K)**3,0][K%2])) | p03266 |
n,k = (int(_) for _ in input().split())
c = 0
for i in range(1,n+1):
for j in range(1,n+1):
if (i+j)%k != 0:
continue
for a in range(1,n+1):
if (j+a)%k == 0 and (a+i)%k == 0:
c += 1
else:
continue
print(c) | #!/usr/bin/env python3
import sys
import math
def solve(N: int, K: int):
ans = int(N/K) ** 3
if(K%2 == 0):
cnt = 0
for i in range(int(K/2), N+1, K):
cnt += 1
ans += cnt ** 3
print(ans)
return
# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
def main():
def iterate_tokens():
for line in sys.stdin:
for word in line.split():
yield word
tokens = iterate_tokens()
N = int(next(tokens)) # type: int
K = int(next(tokens)) # type: int
solve(N, K)
if __name__ == '__main__':
main()
| p03266 |
import sys
from collections import deque
from itertools import permutations
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
l1 = deque([])
l2 = deque([])
ans = deque([])
c = K
while c <= N:
l1.append(c)
c += K
for i in l1:
for j in l1:
for k in l1:
ans.append((i, j, k))
if K % 2 == 0:
c = K / 2
while c <= N:
l2.append(c)
c += K
for i in l2:
for j in l2:
for k in l2:
ans.append((i, j, k))
print((len(list(set(ans))))) | import sys
from collections import deque
from itertools import permutations
input = sys.stdin.readline
N, K = [int(x) for x in input().strip().split()]
l1 = deque([])
l2 = deque([])
ans = deque([])
c = K
while c <= N:
l1.append(c)
c += K
ans = len(l1) ** 3
if K % 2 == 0:
c = K / 2
while c <= N:
l2.append(c)
c += K
ans += len(l2) ** 3
print(ans) | p03266 |
N,K = (int(i) for i in input().split())
MAX = int(2*N/K+1)
ans1=0
ans2=0
ans3=0
for alpha in range(1,MAX):
for beta in range(alpha,MAX):
for gamma in range(beta,MAX):
a = K*(alpha-beta+gamma)
b = K*(alpha+beta-gamma)
c = K*(-alpha+beta+gamma)
if(all([a>0,b>0,c>0]) and
all([a<=N*2,b<=N*2,c<=N*2]) and
all([a%2==0,b%2==0,c%2==0])
):
if a==b and b==c:
ans1+=1
elif a==b or b==c or c==a:
ans2+=1
else:
ans3+=1
print((ans1+ans2*3+ans3*6)) | N,K = (int(i) for i in input().split())
if K%2==1:
print(((N//K)**3))
else:
print(((N//K)**3+((N-K//2)//K+1)**3)) | p03266 |
N, K = list(map(int, input().split()))
v = 0
t = (2*N) // K
for i in range(1,t+1):
if (K*i) % 2 == 0:
if i % 2 == 1:
for j in range(1,t+1,2):
if (K*j) % 2 == 0:
for k in range(1,t+1,2):
if (K*k) % 2 == 0:
v += 1
else:
for j in range(2,t+1,2):
if (K*j) % 2 == 0:
for k in range(2,t+1,2):
if (K*k) % 2 == 0:
v += 1
print(v) | N, K = list(map(int, input().split()))
v = 0
t = (2*N) // K
s = N // K
if K % 2 == 0:
if t % 2 == 0:
v = 2*((t/2)**3)
else:
v = (((t+1)/2)**3) + (((t-1)/2)**3)
else:
v = s**3
v = int(v)
print(v) | p03266 |
N, K = list(map(int, input().split()))
num = [0] * K
for i in range(1, N + 1):
num[i % K] += 1
res = 0
for a in range(K):
b = (K - a) % K
c = (K - a) % K
if (b + c) % K == 0:
res += num[a] * num[b] * num[c]
print(res) | N, K = list(map(int, input().split()))
x = N // K
ans = x ** 3
if K % 2 == 0:
y = (N + (K // 2)) // K
ans += y ** 3
print(ans)
| p03266 |
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
n,k=list(map(int,input().split()))
if(k&1):
s=n//k
print((s**3))
else:
s=n//k
t=(n//(k//2)+1)//2
print((s**3+t**3))
resolve() | import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
n,k=list(map(int,input().split()))
if(k&1):
s=n//k
print((s**3))
else:
s=n//k
t=(n+k//2)//k
print((s**3+t**3))
resolve() | p03266 |
import itertools
N, K = list(map(int, input().split()))
comb = []
if K % 2 != 0:
tail = N//K+1
for a in range(1, tail):
for b in range(1, tail):
for c in range(1, tail):
# print('odd:', a*K, b*K, c*K)
comb.append((a, b, c))
else:
tail = N//K+1
for a in range(1, tail):
for b in range(1, tail):
for c in range(1, tail):
# print('even_1:', a*K, b*K, c*K)
comb.append((a, K, b, K, c, K))
tail = (2*N+K)//(2*K)+1
for a in range(1, tail):
for b in range(1, tail):
for c in range(1, tail):
# print('even_2:', a*K-K//2, b*K-K//2, c*K-K//2)
comb.append((a, K, 1, b, K, 1, c, K, 1))
print((len(set(comb))))
| N, K = list(map(int, input().split()))
if K % 2 == 0:
ans = (N//K)**3 + ((N+K//2)//K)**3
else:
ans = (N//K)**3
print(ans)
| p03266 |
N,K=list(map(int,input().split()))
ans=0
for h in range(2//K,(2*N//K)+1):
if h*K%2==1:
continue
a=h*K//2
if 1<=a<=N:
for i in range((1+a)//K,(N+a)//K+1):
b=i*K-a
if 1<=b<=N:
for j in range((1+b)//K,(N+b)//K+1):
c=j*K-b
if 1<=c<=N and (c+a)%K==0:
ans+=1
print(ans)
| N,K=list(map(int,input().split()))
if K%2==1:
print(((N//K)**3))
else:
print(((N//K)**3+(2*N//K-N//K)**3)) | p03266 |
N, K = list(map(int, input().split()))
ans = 0
for a in range(1, N+1):
cnt = 0
tmp = K - a % K
if tmp > N:
continue
for b in range(tmp, N+1, K):
if K - b % K != tmp:
continue
for c in range(tmp, N+1, K):
cnt += 1
ans += cnt
print(ans)
| N, K = list(map(int, input().split()))
ans = (N // K) ** 3
if K % 2 == 0:
ans += ((N + K//2) //K) ** 3
print(ans) | p03266 |
import itertools
n, k = list(map(int, input().split()))
cnt = 0
for i in itertools.combinations_with_replacement(list(range(1, n + 1)), 3):
if (i[0] + i[1]) % k == 0 and (i[1] + i[2]) % k == 0 and (i[2] + i[0]) % k == 0:
cnt += len(list(set((itertools.permutations(i)))))
print(cnt) | n, k = list(map(int, input().split()))
s = n // k
ans = s ** 3
if k % 2 == 0:
s_ = (n + (k // 2)) // k
ans += s_ ** 3
print(ans)
| p03266 |
from itertools import product
def solve():
n, k = list(map(int, input().split()))
count = 0
# # A解法
# for a in range(1, n+1):
# for b in range(1, n+1):
# # for c in range(1, n+1):
# count += min(a, cal(a, b, 3, n, k))
# B解法
ary = [(a, b, c)
for a in range(1, n+1)
for b in range(1, n+1)
for c in range(1, n+1)]
# print(ary)
for i in range(len(ary)):
A = ary[i][0] + ary[i][1]
B = ary[i][1] + ary[i][2]
C = ary[i][0] + ary[i][2]
if A%k ==0 and B%k ==0 and C%k ==0:
count += 1
# for a, b, c in ary:
# if (a+b)%k == 0 and (b+c)%k == 0 and (c+a)%k == 0:
# count += 1
# C解法
# a = range(1, n+1)
# for i in range(1, n+1):
# b = i
print(count)
solve()
| def solve():
n, k = list(map(int, input().split()))
divList = [0 for _ in range(k)]
for i in range(1, n+1):
divList[i % k] += 1
count = 0
for i in range(k):
if (i*2) % k ==0:
count += divList[i] ** 3
print(count)
solve() | p03266 |
n, k = list(map(int, input().split()))
# num[x]:kで割ってxあまる数が1以上N以下に何個あるか
num = [0 for _ in range(k)]
for i in range(1, n + 1):
num[i % k] += 1
res = 0
for a in range(k):
b = (k - a) % k
c = (k - a) % k
if (b + c) % k != 0:
continue
res += num[a] * num[b] * num[c]
print(res)
| n, k = list(map(int, input().split()))
if k % 2 != 0:
x = n // k
print((x ** 3))
else:
x = n // k
y = 0
for i in range(1, n + 1):
if i % k == k // 2:
y += 1
print((x ** 3 + y ** 3))
| p03266 |
import itertools
n,k=list(map(int,input().split()))
aa=k/2
a=[]
b=[]
if k%2==0:
for i in range(1,n+1):
if i%k==0:
a.append(i)
elif i%k==aa:
b.append(i)
else:
for i in range(1,n+1):
if i%k==0:
a.append(i)
print((len(list(itertools.product(a,repeat=3)))+len(list(itertools.product(b,repeat=3)))))
| import itertools
n,k=list(map(int,input().split()))
aa=k/2
a=[]
b=[]
if k%2==0:
for i in range(1,n+1):
if i%k==0:
a.append(i)
elif i%k==aa:
b.append(i)
else:
for i in range(1,n+1):
if i%k==0:
a.append(i)
tot=len(set(a))**3+len(set(b))**3
print(tot)
| p03266 |
n, k = list(map(int, input().split()))
list = []
p = 0
for a in range(1,n+1):
x = int(a/k)
y = int((n+a)/k)
for i in range(x+1,y+1):
if (i*k-a+i*k-a) % k ==0:
p = p + (y-x)
else:
pass
print(p) | n, k = list(map(int, input().split()))
list = []
p = 0
for a in range(1,n+1):
x = int(a/k)
y = int((n+a)/k)
if 2*a % k ==0:
p = p + (y-x)**2
else:
pass
print(p) | p03266 |
n,k = list(map(int,input().split()))
# print('n,k',n,k)
# print('---')
ans = 0
q,r = divmod(n,k)
# print('q',q,'r',r)
for a in range(1, n+1):
b = k - a%k
c = k - b%k
if (c+a)%k == 0:
# print(a,b,c)
now = 1
if 1 <= b%k <= r: now *= q+1
else: now *= q
if 1 <= c%k <= r: now *= q+1
else: now *= q
ans += now
# print('ans',ans)
print(ans)
| n, k = list(map(int,input().split()))
ans = 0
q, r = divmod(n, k)
for a in range(1, n+1):
b = k - a%k
c = k - b%k
if (c + a)%k == 0:
now = 1
if 1 <= b%k <= r: now *= q+1
else: now *= q
if 1 <= c%k <= r: now *= q+1
else: now *= q
ans += now
print(ans)
| p03266 |
N,K = list(map(int,input().split()))
flag = 0
a = 1
while a <= N:
b = 1
while b <= a:
c = 1
while c <= b:
if (a+b)%K == 0 and (b+c)%K == 0 and (c+a)%K == 0:
if a == b and b == c:
flag = flag + 1
elif a == b or b == c or c == a:
flag = flag + 3
else:
flag = flag + 6
c = c + 1
b = b + 1
a = a + 1
print(flag) | N,K = list(map(int,input().split()))
if K%2==1:
maxn = int(N/K)
print((maxn**3))
else:
maxn=int(N/K)
if N%K >= int(K/2):
print((maxn**3+(maxn+1)**3))
else:
print((2*maxn**3)) | p03266 |
import itertools
a, b = list(map(int, input().split()))
c = []
for i in range(1, a + 1):
if i % b == 0 or i % b == b / 2:
c.append(i)
d = 0
for i in itertools.product(c, repeat=3):
if (i[0] + i[1]) % b == 0 and (i[0] + i[2]) % b == 0 and (i[1] + i[2]) % b == 0:
d += 1
print(d)
| a, b = list(map(int, input().split()))
c = []
d = []
for i in range(1, a + 1):
if i % b == 0:
c.append(i)
elif i % b == b / 2:
d.append(i)
e = 0
print((len(c) * len(c) * len(c) + len(d) * len(d) * len(d)))
| p03266 |
n,k=list(map(int,input().split()))
p=0
for a in range(1,n+1):
for b in range(1,n+1):
for c in range(1,n+1):
if (a+b)%k==0 and (b+c)%k==0 and (c+a)%k==0:
p+=1
print(p) | n,k=list(map(int,input().split()))
a=n//k
if k%2==0:
b=0
for i in range(1,n+1):
if i%k==int(k/2):
b+=1
print((a**3+b**3))
else:
print((a**3)) | p03266 |
def iin(): return int(eval(input()))
def nl(): return list(map(int, input().split()))
def ary(r, c, v): return [[v for _ in range(c)] for _ in range(r)]
n, k = nl()
d = dict()
for i in range(1, n + 1):
t = i % k
d[t] = d.get(t, 0) + 1
ans = 0
if k % 2 == 0:
ans += d.get(k // 2, 0) ** 3
ans += d.get(0, 0) ** 3
print(ans) | n, k = list(map(int, input().split()))
ans = 0
if k % 2 == 0:
ans += ((n - k // 2) // k + 1) ** 3
ans += (n // k) ** 3
print(ans) | p03266 |
import itertools
N,K = list(map(int,input().split()))
count = 0
list_abc = list(itertools.product(list(range(1,N+1)),repeat = 3))
for i in list_abc:
if (i[0] + i[1])%K==0 and (i[1] + i[2])%K==0 and (i[0] + i[2])%K==0:
count = count +1
print(count) | N,K = list(map(int,input().split()))
ans1 = 0
ans2 = 0
for i in range(1,N+1):
if i%K == 0:
ans1 += 1
if K%2 == 0:
for i in range(1,N+1):
if i%K == K//2:
ans2 += 1
print((ans1**3 + ans2**3)) | p03266 |
# -*- coding: utf-8 -*-
import itertools
N,K = list(map(int, input().split()))
p = list(itertools.product(list(range(1,N+1)), repeat=3))
ans = 0
for pair in p:
if (pair[0] + pair[1]) % K == 0 and (pair[1] + pair[2]) % K == 0 and (pair[2] + pair[0]) % K == 0:
ans += 1
print(ans) | # -*- coding: utf-8 -*-
# mod Kをうまく使ってaを全検索
N,K = list(map(int, input().split()))
num = [0] * K
# 元のNまでの数がmod Kの世界で何になるかを集計
for i in range(1,N+1):
num[i%K] += 1
ans = 0
# a mod Kを決め打ちしてb mod Kとc mod Kを出す
for a in range(K):
b = (K - a) % K
c = (K - a) % K
# つじつまが合ってるか確認
if (b + c) % K == 0:
# 予め集計してあるmod Kでの数を合計させる
ans += num[a] * num[b] * num[c]
print(ans) | p03266 |
n, k = list(map(int, input().split()))
ans = 0
for a in range(k, n + 1, k):
for b in range(k, n + 1, k):
for c in range(k, n + 1, k):
ans += 1
if k % 2 == 0:
for a in range(int(k / 2), n + 1, k):
for b in range(int(k / 2), n + 1, k):
for c in range(int(k / 2), n + 1, k):
ans += 1
print(ans) | n, k = list(map(int, input().split()))
ans = 0
t = 0
for i in range(k, n + 1, k):
ans += 1
ans **= 3
if k % 2 == 0:
for j in range(int(k / 2), n + 1, k):
t += 1
ans += t ** 3
print(ans) | p03266 |
N,K = (int(i) for i in input().split())
ans=0
for a in range(1,N+1):
for b in range(1,N+1):
for c in range(1,N+1):
if (a+b)%K ==0 and (b+c)%K ==0 and (c+a)%K==0:
ans +=1
print(ans) | N,K = (int(i) for i in input().split())
a=0
b=0
if K%2 !=0:
print(((N//K)**3))
else:
for i in range (1,N+1):
if i % K == 0:
a += 1
elif i % K == K/2:
b += 1
print((a**3+b**3)) | p03266 |
from itertools import product
N, K = list(map(int, input().split(" ")))
# print(len(list(filter(lambda x: 2*x%K==0, range(1, N+1))))**2)
l = []
for i in range(1, N+1):
if (2*i)%K == 0:
l.append(i)
count = 0
for a, b, c in product(l, repeat=3):
if (a+b)%K == (b+c)%K == (c+a)%K == 0:
count += 1
print(count) | from itertools import product
N, K = list(map(int, input().split(" ")))
"""
i%K == 0 ならその数同士で条件に合う数列を作れる
i%K == K//2 ならその数同士で条件に合う数列を作れる(Kが偶数の場合)
これらは条件によって導ける
"""
count = (N//K)**3
if K%2 == 0:
count += (N//K + (N%K >= K//2))**3
print(count)
| p03266 |
N, K = list(map(int, input().split()))
div, rem = divmod(N, K)
mods = [div + (0 < i <= rem) for i in range(K)]
total = mods[0] ** 3
if not K & 1:
total += mods[K >> 1] ** 3
print(total) | N, K = list(map(int, input().split()))
div, rem = divmod(N, K)
total = div ** 3
if not K & 1:
total += (div + (0 < K <= (rem << 1))) ** 3
print(total) | p03266 |
import itertools
N,K = list(map(int,input().split()))
a=[]
b=[]
ans = 0
for i in range(1,N+1):
a.append(i)
b += itertools.product(a,repeat = 3)
for i in b:
#print(i[0],i[1],i[2])
if (i[0]+i[1])%K==0 and (i[1]+i[2])%K==0 and (i[0]+i[2])%K==0:
ans+=1
#print(a)
#print(b)
print(ans) | N,K = list(map(int,input().split()))
ans = 0
if K%2==0:
d1=N//(K//2)
d2=N//K
ans=d2**3+(d1-d2)**3
else:
d=N//K
ans = d**3
print(ans)
| p03266 |
n,k= list(map(int,input().split()))
cnt = 0
for a in range(1,n+1):
if (2*a) % k == 0:
for b in range(a,n+1):
if ((2*b) % k == 0)and((a+b) % k == 0 )and((a-b) % k == 0 ):
for c in range(b,n+1):
if ((2*c) % k == 0)and((a+c) % k == 0 )and((c+b) % k == 0 ):
if (a==b)and(b==c):
cnt += 1
elif (a!=b)and(b!=c)and(a!=c):
cnt += 6
else:
cnt +=3
print(cnt)
| #!/usr/bin/env python3
import sys
def solve(N: int, K: int):
count = 0
if K % 2 == 0:
count += (N//K)**3
# a,b,c == 0 mod K//2
count += (N//(K//2) - N//K)**3
else:
count += (N//K)**3
print(count)
return
def main():
N,K= list(map(int,input().split()))
# N = int(next(tokens)) # type: int
# K = int(next(tokens)) # type: int
solve(N, K)
if __name__ == '__main__':
main() | p03266 |
import itertools
n, k = list(map(int, input().split()))
a = []
x = k/2
count = 0
while x <= n:
if x % 1 == 0:
a.append(int(x))
x += k/2
ori_list = list(itertools.product(a, repeat=3))
for i in range(len(ori_list)):
if (ori_list[i][0] + ori_list[i][1]) % k == 0:
if (ori_list[i][1] + ori_list[i][2]) % k == 0:
if (ori_list[i][0] + ori_list[i][2]) % k == 0:
count += 1
print(count) | n, k = list(map(int, input().split()))
cnt = 0
if k % 2 == 1:
for i in range(1, n+1):
if i % k == 0:
cnt += 1
ans = cnt ** 3
elif k % 2 == 0:
cnt1 = 0
cnt2 = 0
for i in range(1, n+1):
if i % k == 0:
cnt1 += 1
elif i % k == k/2:
cnt2 += 1
ans = cnt1 ** 3 + cnt2 ** 3
print(ans) | p03266 |
ans = []
inp = [int(x) for x in input().split()]
n = inp[0]
k = inp[1]
for a in range(1, n+1):
for b in range(1, n+1):
for c in range(1, n+1):
if (a+b) % k == 0 and (b+c) % k == 0 and (c+a) % k == 0:
ans.append([a, b, c])
print((len(ans))) | inp = [int(x) for x in input().split()]
n = inp[0]
k = inp[1]
num = [x for x in range(0, n+1, k)]
ans = (len(num)-1) ** 3
if k % 2 == 0:
c = 0
for i in range(1, n+1):
if i % k == k / 2:
c += 1
ans += c ** 3
print(ans) | p03266 |
N, K = list(map(int, input().split()))
counter = 0
for i in range(1, N+1):
j = K - i
while j <= 0:
j += K
while j <= N:
k = K - i
while k <= 0:
k += K
while k <= N:
if (j+k) % K == 0 and (j+k) != 0:
counter += 1
k += K
j += K
print(counter)
| N, K = list(map(int, input().split()))
result = (N//K)**3
if K % 2 == 0:
if N-K*(N//K) < K/2:
result += (N//K)**3
else:
result += ((N//K)+1)**3
print(result)
| p03266 |
from itertools import combinations_with_replacement
N,K = list(map(int,input().split()))
ans = 0
for cmb in combinations_with_replacement([i for i in range(1,N+1)],3):
if all([True if a%K ==0 else False for a in [cmb[0]+cmb[1], cmb[1]+cmb[2],cmb[2]+cmb[0]]]):
set_num = len(set(cmb))
if set_num == 1: ans += 1
elif set_num == 2: ans += 3
elif set_num == 3: ans += 6
#print(ans, cmb)
print(ans) | from itertools import combinations_with_replacement
def calc_cases(num):
cases = num
if num == 2: cases += num*(num-1)*3
elif num >= 3: cases += num*(num-1)*3 + num*(num-1)*(num-2)
return int(cases)
N,K = list(map(int,input().split()))
num, num2 = 0, 0
if K%2 ==1:
for i in range(1,N+1):
if i%K == 0: num +=1
else:
for i in range(1,N+1):
if i%K == 0:
num +=1
elif i%K == K//2:
num2 += 1
print((calc_cases(num)+calc_cases(num2)))
#print(num, calc_cases(num), num2, calc_cases(num2)) | p03266 |
N, K = list(map(int, input().split()))
A = []
for i in range(2, 2*N+1):
if i%K == 0:
A.append(i)
ans = 0
for x in range(len(A)):
for y in range(len(A)):
for z in range(len(A)):
a = (A[x]-A[y]+A[z])/2
if int(a) == a and 1 <= a <= N and 1 <= A[x]-a <= N and 1 <= A[z]-a <= N:
ans += 1
print(ans) | N, K = list(map(int, input().split()))
if K%2 == 1:
print(((N//K)**3))
else:
a, b = 0, 0
for i in range(1, N+1):
if i%K == 0:
a += 1
elif i%K == K//2:
b += 1
print((b**3+a**3)) | p03266 |
n,k=list(map(int,input().split()))
t=0
v=2*n//k
for apb in range(k,k*(v+1),k):
for bpc in range(k,k*(v+1),k):
for cpa in range(k,apb+bpc,k):
if (apb+bpc+cpa)%2:
continue
a = (apb+bpc+cpa - 2*bpc)//2
b = (apb+bpc+cpa - 2*cpa)//2
c = (apb+bpc+cpa - 2*apb)//2
if 0 < a and a <= n and 0 < b and b<=n and 0 < c and c <= n:
t += 1
print(t)
| n,k=list(map(int,input().split()))
x = n//k
y = x if k%2 else n//(k//2)
print((x**3+(y-x)**3))
| p03266 |
import sys
input = sys.stdin.readline
N,K = list(map(int,input().split()))
target =[]
i=3
x = i/2*K
while x <= 3*N:
if x == int(x):
target.append(int(x))
i += 1
x = i/2*K
count = 0
if K%2 == 0:
A = list(range(K//2,N+1,K//2))
else:
A = list(range(K,N+1,K))
for trg in target:
for a in A:
for b in range((a//K+1)*K-a,N+1,K):
c = trg - a - b
#print(a,b,c,target)
if 0 < c <= N and (b+c)%K == 0 and (c+a)%K ==0:
#print(a,b,c)
count += 1
print(count)
| import sys
input = sys.stdin.readline
N,K=list(map(int,input().split()))
if K%2 == 0:
A = list(range(K//2,N+1,K))
B = list(range(K,N+1,K))
else:
A = list(range(K,N+1,K))
B = []
print((len(A)**3+len(B)**3)) | p03266 |
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans = count1 = count2 = 0
n,k=list(map(int,input().split()))
count1+=n//k
if k%2==0:count2+=(n+k//2)//k
# for ii in itertools.product(range(1,n//k+2), repeat=3):
# print(ii)
# print(count)
a=len(list(itertools.product(list(range(count1)),repeat=3)))
b=len(list(itertools.product(list(range(count2)),repeat=3)))
print((a+b))
| ans = count1 = count2 = 0
n,k=list(map(int,input().split()))
count1+=n//k
if k%2==0:count2+=(n+k//2)//k
print((count1**3+count2**3))
| p03266 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.