Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 12, 2026 11:02
Show Gist options
  • Select an option

  • Save maehrm/89ebf8e307a6b1ddb6cc90f3dd8da070 to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/89ebf8e307a6b1ddb6cc90f3dd8da070 to your computer and use it in GitHub Desktop.
N = int(input())
T = input()
# dp[r][0] : 0が偶数個(結果が1になる)
# dp[r][1] : 0が奇数個(結果が0になる)
dp = [[0] * 2 for _ in range(N + 1)]
for r in range(1, N + 1):
if T[r - 1] == "0":
dp[r][0] = dp[r - 1][1]
dp[r][1] = dp[r - 1][0] + 1 # 自分自身の分を+1
else:
dp[r][0] = dp[r - 1][0] + 1
dp[r][1] = dp[r - 1][1]
ans = 0
for r in range(1, N + 1):
ans += dp[r][0]
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment