---
title: "ABC275 E問題の考え方"
date: 2022-12-08
tags: ["AtCoder","Python"]
url: https://aruma256.dev/posts/abc275-e
---

[ABC275 E - Sugoroku 4](https://atcoder.jp/contests/abc275/tasks/abc275_e) の考え方を書きました。

# はじめに

こんにちは。[@aruma256](https://twitter.com/aruma256) です。  
[GMOペパボエンジニア Advent Calendar 2022 (第2会場)](https://adventar.org/calendars/7784) の8日目の記事です。

私は社内の競技プログラミング好きが集まる #kyopro チャンネルに生息しているので、最近解いた競プロ問題の解説記事を書くことにしました。  

競技プログラミングは「提示された要求を満たすコードをいかに早く提出できるか」を競うものです。文字列操作やテキストの入出力など基本的な処理実装はもちろん、難しい問題では高度なアルゴリズムの知識や思考、実装力が要求されます。

今回は [AtCoder Beginner Contest 275 E問題](https://atcoder.jp/contests/abc275/tasks/abc275_e)の解説です。

より易しい問題の解説記事も投稿しています。「競プロを初めて知った」という方は↓を先にご覧頂くと良いかもしれません。
* [ABC245 A～D問題の考え方](/posts/abc245)
* [ABC275 D問題の考え方](/posts/abc275-d)

# 問題の概要

[ABC275 E - Sugoroku 4](https://atcoder.jp/contests/abc275/tasks/abc275_e)

* 双六において、指定回数以内にゴールできる確率を求める
* ゴールを超過する出目の場合、折り返す
* 答えは mod 998244353 で整数として出力する
    * 998244353 は素数です（伏線）

# 有理数表現の確率で考えてみる

mod 998244353 については後で考えることにして、まずは普通の確率計算問題として考えてみます。

## step 0

初期状態（ルーレットを "0回" まわしたとき）では、

* はじめのマスにいる確率は 1
* その他のマスにいる確率は 0

です。

`N=5` のときの初期状態は下図のように表現できます。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="472px" height="183px" viewBox="-0.5 -0.5 472 183" style="background-color: rgb(255, 255, 255);"><defs/><g><ellipse cx="125" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">S</div></div></div></foreignObject><text x="125" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">S</text></switch></g><ellipse cx="205" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><ellipse cx="285" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><ellipse cx="365" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><ellipse cx="445" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">G</div></div></div></foreignObject><text x="445" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">G</text></switch></g><path d="M 150 26 L 173.63 26" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 178.88 26 L 171.88 29.5 L 173.63 26 L 171.88 22.5 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 230 26 L 253.63 26" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 258.88 26 L 251.88 29.5 L 253.63 26 L 251.88 22.5 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 310 26 L 333.63 26" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 338.88 26 L 331.88 29.5 L 333.63 26 L 331.88 22.5 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 390 26 L 413.63 26" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 418.88 26 L 411.88 29.5 L 413.63 26 L 411.88 22.5 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><ellipse cx="125" cy="156" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 156px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="125" y="163" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">1</text></switch></g><ellipse cx="205" cy="156" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 156px; margin-left: 181px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="205" y="163" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="285" cy="156" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 156px; margin-left: 261px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="285" y="163" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="365" cy="156" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 156px; margin-left: 341px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="365" y="163" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="445" cy="156" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 156px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="445" y="163" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><path d="M 100 91 L 470 91" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><rect x="0" y="141" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 156px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 0</div></div></div></foreignObject><text x="45" y="163" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 0</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

## step 0 → step 1

上の状態からルーレットを1回まわして、出目の数だけコマを進めます。  
`M = 3` とすると、

* 1マス進む確率 : 1/3
* 2マス進む確率 : 1/3
* 3マス進む確率 : 1/3

なので、各マスにいる確率は下のように変化します。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="472px" height="173px" viewBox="-0.5 -0.5 472 173" style="background-color: rgb(255, 255, 255);"><defs/><g><ellipse cx="125" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 101px;"><div data-drawio-colors="color: #FF6666; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(255, 102, 102); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1</div></div></div></foreignObject><text x="125" y="33" fill="#FF6666" font-family="Helvetica" font-size="24px" text-anchor="middle">1</text></switch></g><ellipse cx="205" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 181px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="205" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="285" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 261px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="285" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="365" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 341px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="365" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="445" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="445" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="125" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="125" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="205" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 181px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/3</div></div></div></foreignObject><text x="205" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">1/3</text></switch></g><ellipse cx="285" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 261px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/3</div></div></div></foreignObject><text x="285" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">1/3</text></switch></g><ellipse cx="365" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 341px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/3</div></div></div></foreignObject><text x="365" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">1/3</text></switch></g><ellipse cx="445" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="445" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><path d="M 142.68 43.68 L 184.81 116.49" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 187.44 121.03 L 180.9 116.73 L 184.81 116.49 L 186.96 113.22 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 142.68 43.68 L 267.52 117.75" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 272.04 120.43 L 264.23 119.87 L 267.52 117.75 L 267.8 113.85 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><path d="M 142.68 43.68 L 344.03 118.77" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 348.95 120.61 L 341.17 121.44 L 344.03 118.77 L 343.62 114.88 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><rect x="80" y="76" width="82" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-end; width: 80px; height: 1px; padding-top: 91px; margin-left: 80px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: right;"><div style="display: inline-block; font-size: 19px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">出目 : ①<font color="#ff6666"><br />1</font>×1/3</div></div></div></foreignObject><text x="160" y="97" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="19px" text-anchor="end">出目 : ①...</text></switch></g><rect x="257" y="64" width="60" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 58px; height: 1px; padding-top: 79px; margin-left: 258px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 19px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">③ <font color="#ff6666">1</font>×1/3</div></div></div></foreignObject><text x="287" y="85" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="19px" text-anchor="middle">③ 1×1/3</text></switch></g><rect x="187" y="77" width="60" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe flex-start; width: 58px; height: 1px; padding-top: 92px; margin-left: 189px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: left;"><div style="display: inline-block; font-size: 19px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">②<font color="#ff6666"><br />1</font>×1/3</div></div></div></foreignObject><text x="189" y="98" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="19px">②&#xa;1×1/3</text></switch></g><rect x="0" y="11" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 26px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 0</div></div></div></foreignObject><text x="45" y="33" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 0</text></switch></g><rect x="0" y="131" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 146px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 1</div></div></div></foreignObject><text x="45" y="153" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 1</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

1ステップごとに、他のマスへ確率が分配されていくイメージです。

## step 1 → step 2

ゴールを超過する場合に折り返すというルールのため、異なる出目でも実質的に同じ移動となる場合があります。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="622px" height="241px" viewBox="-0.5 -0.5 622 241" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 186 171 L 276 171 L 276 211 L 220.4 211 L 189.6 240 L 200.4 211 L 186 211 Z" fill="rgb(255, 255, 255)" stroke="#000000" stroke-miterlimit="10" transform="rotate(-180,231,205.5)" pointer-events="all"/><ellipse cx="125" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="125" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="205" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="#ff6666" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 181px;"><div data-drawio-colors="color: #FF0000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(255, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/3</div></div></div></foreignObject><text x="205" y="33" fill="#FF0000" font-family="Helvetica" font-size="24px" text-anchor="middle">1/3</text></switch></g><ellipse cx="285" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="#0000ff" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 261px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/3</div></div></div></foreignObject><text x="285" y="33" fill="#0000FF" font-family="Helvetica" font-size="24px" text-anchor="middle">1/3</text></switch></g><ellipse cx="365" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="#00cc66" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 341px;"><div data-drawio-colors="color: #00CC66; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 204, 102); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/3</div></div></div></foreignObject><text x="365" y="33" fill="#00CC66" font-family="Helvetica" font-size="24px" text-anchor="middle">1/3</text></switch></g><ellipse cx="445" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="445" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="125" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="125" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="205" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 181px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="205" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="285" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 261px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/9</div></div></div></foreignObject><text x="285" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">1/9</text></switch></g><ellipse cx="365" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 341px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">5/9</div></div></div></foreignObject><text x="365" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">5/9</text></switch></g><ellipse cx="445" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">3/9</div></div></div></foreignObject><text x="445" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">3/9</text></switch></g><path d="M 222.68 43.68 L 268.7 119.2" fill="none" stroke="#ff6666" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 271.43 123.68 L 264.8 119.53 L 268.7 119.2 L 270.78 115.88 Z" fill="#ff6666" stroke="#ff6666" stroke-miterlimit="10" pointer-events="all"/><path d="M 222.68 43.68 L 349.57 121.67" fill="none" stroke="#ff6666" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 354.05 124.41 L 346.25 123.73 L 349.57 121.67 L 349.92 117.77 Z" fill="#ff6666" stroke="#ff6666" stroke-miterlimit="10" pointer-events="all"/><path d="M 222.68 43.68 L 421.44 125.89" fill="none" stroke="#ff6666" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 426.29 127.9 L 418.48 128.45 L 421.44 125.89 L 421.16 121.99 Z" fill="#ff6666" stroke="#ff6666" stroke-miterlimit="10" pointer-events="all"/><path d="M 302.68 43.68 L 361 116.04" fill="none" stroke="#0000ff" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 364.3 120.13 L 357.18 116.88 L 361 116.04 L 362.63 112.48 Z" fill="#0000ff" stroke="#0000ff" stroke-miterlimit="10" pointer-events="all"/><path d="M 302.68 43.68 L 424.56 117.69" fill="none" stroke="#0000ff" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 429.04 120.42 L 421.24 119.78 L 424.56 117.69 L 424.88 113.79 Z" fill="#0000ff" stroke="#0000ff" stroke-miterlimit="10" pointer-events="all"/><path d="M 311 40 L 368.05 112.01" fill="none" stroke="#0000ff" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 371.31 116.12 L 364.22 112.81 L 368.05 112.01 L 369.7 108.46 Z" fill="#0000ff" stroke="#0000ff" stroke-miterlimit="10" pointer-events="all"/><path d="M 365 51 L 365 114.63" fill="none" stroke="#00cc66" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 365 119.88 L 361.5 112.88 L 365 114.63 L 368.5 112.88 Z" fill="#00cc66" stroke="#00cc66" stroke-miterlimit="10" pointer-events="all"/><path d="M 375 51 L 375 114.63" fill="none" stroke="#00cc66" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 375 119.88 L 371.5 112.88 L 375 114.63 L 378.5 112.88 Z" fill="#00cc66" stroke="#00cc66" stroke-miterlimit="10" pointer-events="all"/><path d="M 380 51 L 430.92 112.11" fill="none" stroke="#00cc66" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 434.28 116.14 L 427.11 113 L 430.92 112.11 L 432.49 108.52 Z" fill="#00cc66" stroke="#00cc66" stroke-miterlimit="10" pointer-events="all"/><rect x="176" y="205" width="110" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 108px; height: 1px; padding-top: 220px; margin-left: 177px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 19px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;"><font color="#ff6666">1/3</font>×1/3</div></div></div></foreignObject><text x="231" y="226" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="19px" text-anchor="middle">1/3×1/3</text></switch></g><path d="M 293 171 L 620 171 L 620 211 L 572.33 211 L 551.33 240 L 551.33 211 L 293 211 Z" fill="rgb(255, 255, 255)" stroke="#000000" stroke-miterlimit="10" transform="rotate(-180,456.5,205.5)" pointer-events="all"/><rect x="290" y="205" width="330" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 328px; height: 1px; padding-top: 220px; margin-left: 291px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 19px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;"><font color="#ff6666">1/3</font>×1/3 + (<font color="#0000ff">1/3</font>×1/3) ×2 + (<font color="#00cc66">1/3</font>×1/3) ×2</div></div></div></foreignObject><text x="455" y="226" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="19px" text-anchor="middle">1/3×1/3 + (1/3×1/3) ×2 + (1/3×1/3)...</text></switch></g><rect x="0" y="11" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 26px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 1</div></div></div></foreignObject><text x="45" y="33" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 1</text></switch></g><rect x="0" y="131" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 146px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 2</div></div></div></foreignObject><text x="45" y="153" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 2</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

## step 2 → step 3

ゴールマスの確率は分配や 1/M などせず、単に次stepにて加算されるのみであることに気をつけます。

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="487px" height="241px" viewBox="-0.5 -0.5 487 241" style="background-color: rgb(255, 255, 255);"><defs/><g><path d="M 270 171 L 485 171 L 485 211 L 317.95 211 L 306.55 240 L 297.95 211 L 270 211 Z" fill="rgb(255, 255, 255)" stroke="#000000" stroke-miterlimit="10" transform="rotate(-180,377.5,205.5)" pointer-events="all"/><ellipse cx="125" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="125" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="205" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 181px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="205" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="285" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="#ff3333" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 261px;"><div data-drawio-colors="color: #FF3333; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(255, 51, 51); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">1/9</div></div></div></foreignObject><text x="285" y="33" fill="#FF3333" font-family="Helvetica" font-size="24px" text-anchor="middle">1/9</text></switch></g><ellipse cx="365" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="#0000ff" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 341px;"><div data-drawio-colors="color: #0000FF; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 255); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">5/9</div></div></div></foreignObject><text x="365" y="33" fill="#0000FF" font-family="Helvetica" font-size="24px" text-anchor="middle">5/9</text></switch></g><ellipse cx="445" cy="26" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 26px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">3/9</div></div></div></foreignObject><text x="445" y="33" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">3/9</text></switch></g><ellipse cx="125" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 101px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="125" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="205" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 181px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">0</div></div></div></foreignObject><text x="205" y="153" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="24px" text-anchor="middle">0</text></switch></g><ellipse cx="285" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 261px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">5/27</div></div></div></foreignObject><text x="285" y="151" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">5/27</text></switch></g><ellipse cx="365" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 341px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">7/27</div></div></div></foreignObject><text x="365" y="151" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">7/27</text></switch></g><ellipse cx="445" cy="146" rx="25" ry="25" fill="rgb(255, 255, 255)" stroke="rgb(0, 0, 0)" stroke-width="3" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 48px; height: 1px; padding-top: 146px; margin-left: 421px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 18px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">15/27</div></div></div></foreignObject><text x="445" y="151" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="18px" text-anchor="middle">15/27</text></switch></g><path d="M 298 53 L 343.83 122.99" fill="none" stroke="#ff3333" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 346.71 127.39 L 339.95 123.45 L 343.83 122.99 L 345.8 119.61 Z" fill="#ff3333" stroke="#ff3333" stroke-miterlimit="10" pointer-events="all"/><path d="M 308 45.68 L 353.83 115.67" fill="none" stroke="#ff3333" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 356.71 120.07 L 349.95 116.13 L 353.83 115.67 L 355.8 112.29 Z" fill="#ff3333" stroke="#ff3333" stroke-miterlimit="10" pointer-events="all"/><path d="M 310 41 L 420.24 126.74" fill="none" stroke="#ff3333" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 424.38 129.97 L 416.71 128.43 L 420.24 126.74 L 421.01 122.9 Z" fill="#ff3333" stroke="#ff3333" stroke-miterlimit="10" pointer-events="all"/><path d="M 365 51 L 306.67 123.36" fill="none" stroke="#0000ff" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 303.38 127.45 L 305.05 119.81 L 306.67 123.36 L 310.5 124.2 Z" fill="#0000ff" stroke="#0000ff" stroke-miterlimit="10" pointer-events="all"/><path d="M 365 51 L 365 114.63" fill="none" stroke="#0000ff" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 365 119.88 L 361.5 112.88 L 365 114.63 L 368.5 112.88 Z" fill="#0000ff" stroke="#0000ff" stroke-miterlimit="10" pointer-events="all"/><path d="M 365 51 L 424.7 116.3" fill="none" stroke="#0000ff" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 428.25 120.17 L 420.94 117.37 L 424.7 116.3 L 426.11 112.65 Z" fill="#0000ff" stroke="#0000ff" stroke-miterlimit="10" pointer-events="all"/><path d="M 445 51 L 445 114.63" fill="none" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="stroke"/><path d="M 445 119.88 L 441.5 112.88 L 445 114.63 L 448.5 112.88 Z" fill="rgb(0, 0, 0)" stroke="rgb(0, 0, 0)" stroke-miterlimit="10" pointer-events="all"/><rect x="260" y="205" width="225" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 223px; height: 1px; padding-top: 220px; margin-left: 261px;"><div data-drawio-colors="color: rgb(0, 0, 0); " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 19px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;"><font color="#ff6666">1/9</font>×1/3 +<font color="#0000ff" style="text-align: left; background-color: initial;"> 5/9</font><span style="text-align: left; background-color: initial;">×1/3 + <b>3/9</b></span></div></div></div></foreignObject><text x="373" y="226" fill="rgb(0, 0, 0)" font-family="Helvetica" font-size="19px" text-anchor="middle">1/9×1/3 + 5/9×1/3 + 3/9</text></switch></g><rect x="0" y="11" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 26px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 2</div></div></div></foreignObject><text x="45" y="33" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 2</text></switch></g><rect x="0" y="131" width="90" height="30" fill="none" stroke="none" pointer-events="all"/><g transform="translate(-0.5 -0.5)"><switch><foreignObject pointer-events="none" width="100%" height="100%" requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" style="overflow: visible; text-align: left;"><div xmlns="http://www.w3.org/1999/xhtml" style="display: flex; align-items: unsafe center; justify-content: unsafe center; width: 88px; height: 1px; padding-top: 146px; margin-left: 1px;"><div data-drawio-colors="color: #000000; " style="box-sizing: border-box; font-size: 0px; text-align: center;"><div style="display: inline-block; font-size: 24px; font-family: Helvetica; color: rgb(0, 0, 0); line-height: 1.2; pointer-events: all; white-space: normal; overflow-wrap: normal;">step 3</div></div></div></foreignObject><text x="45" y="153" fill="#000000" font-family="Helvetica" font-size="24px" text-anchor="middle">step 3</text></switch></g></g><switch><g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/><a transform="translate(0,-5)" xlink:href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank"><text text-anchor="middle" font-size="10px" x="50%" y="100%">Text is not SVG - cannot display</text></a></switch></svg>

## ここまでをまとめる

この操作をK回繰り返したときのゴールマスに存在している確率が答えです。

このシミュレーションの計算量を見積もると

* K回、ルーレットを回す
    * N個のマス、それぞれについて
        * M通りの行き先の確率を加算する

より、 `O(KNM)` です。

# 確率を mod 998244353 で扱う

さて、先送りにしていた mod 998244353 で確率を表すという部分について考えていきます。

簡単に説明すると、ある素数 p を法とするとき
「aで割る」という操作は「モジュラ逆数 a^(p-2) をかける」として扱うことができるので、これを利用します。

参考：フェルマーの小定理 a^(p-1) ≡ 1 (mod p)

## モジュラ逆数を高速に求める

a^p-2 を求める計算アルゴリズムにも工夫が必要です。

以下のような単純な実装では、`inv_a` が非常に大きい数になってしまいます。

```ruby
# ruby

inv_a = a

(p-3).times {
  inv_a *= a
}

inv_a = inv_a % 998244353
```

逐次、剰余をとりましょう。

```ruby
# ruby

inv_a = a

(p-3).times {
  inv_a = (inv_a * a) % 998244353
}
```

pが小さい数であればこれで十分ですが、pが大きい（目安としては、10^7 より大きい)場合にはさらに高速化が必要です。

### 繰り返し二乗法

例えば n^104 を求めるとき、
n * n * n * ...
と103回の掛け算をするよりも効率が良いような計算方法を考えます。

目的の数を2の累乗の和として表現してみると、

```
104
= 2^3 + 2^5 + 2^6
= 8 + 32 + 64
```

つまり、

```
n^104
= n^(8 + 32 + 64)
= n^8 * n^32 * n^64
```

です。

[n^1, n^2, n^4, n^8, n^16, n^32, n^64, ... ] は前から順に高速に求められるので、これを事前に計算しておき、それらを組み合わせて目的の値を得るということが可能です。

# 解法

以上より、

1. 入力を受け取る。
1. Mのモジュラー逆数を求める。
1. 以下のシミュレーションを行う
    * K回、ルーレットを回す
        * N個のマス、それぞれについて
            * M通りの行き先の確率を加算する
1. 最後のマスに存在している確率が答え

という手順で解くことができます。

# Python標準のpow関数

Python3.8以降では、[組み込み関数のpow](https://docs.python.org/ja/3.8/library/functions.html#pow)で簡単にモジュラー逆数を求められます。

# 回答例

* Python (3.8.2)
    * [https://atcoder.jp/contests/abc275/submissions/36069352](https://atcoder.jp/contests/abc275/submissions/36069352)
