codeforces#Round 604-E. Beautiful Mirrors(期望DP)

codeforces# Round 604-E. Beautiful Mirrors(期望DP)

鏈接:

https://codeforces.com/contest/1265/problem/E

題意:

image

Examples

input

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">1
50</pre>

output

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">2</pre>

input

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">3
10 20 50</pre>

output

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n51" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">112</pre>

Note

In the first test, there is only one mirror and it tells, that Creatnx is beautiful with probability 1/2. So, the expected number of days until Creatnx becomes happy is 2.

有n個鏡子,每個鏡子有pi/100的概率回答是,初始從1開始,如果詢問鏡子回答為是,則下一天將詢問第i+1個鏡子,當(dāng)?shù)趎個鏡子回答是時,游戲結(jié)束。如果鏡子回答為否,則下一天將重新訪問第1個鏡子。求游戲結(jié)束的期望步數(shù)。

思路:經(jīng)典概率DP + 快速冪+費馬定理+逆元:

代碼:

<pre mdtype="fences" cid="n70" lang="" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#include <bits/stdc++.h>
using namespace std;

define ll long long

const ll mod = 998244353;
const int maxn = 2e5+10;
ll dp[maxn];
ll pow(ll a, ll b){
ll res=1;
while(b){
if(b&1){
res = (resa) % mod;
}
a = (a
a) % mod;
b >>= 1;
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
int n;
cin >> n;
for(int i=1; i<=n; i++){
ll x;
cin >> x;
ll p = 100 * pow(x, mod-2) % mod;
dp[i] = (dp[i-1]+1)*p%mod;
}
cout << dp[n] << endl;
return 0;
}</pre>

知識點:

  • 同余定理:

給定一個正整數(shù)m,如果兩個整數(shù)a和b滿足a-b能夠被m整除,即(a-b)/m得到一個整數(shù),那么就稱整數(shù)a與b對模m同余,記作a≡b(mod m)。對模m同余是整數(shù)的一個等價關(guān)系。例如26≡2(mod 12)。

  • 費馬小定理:

[圖片上傳失敗...(image-25cb07-1576556846159)]

,其中 [圖片上傳失敗...(image-93433d-1576556846159)]

為任意質(zhì)數(shù)而 [圖片上傳失敗...(image-751c0-1576556846159)]

為與其互質(zhì)的任意整數(shù)。

  • 逆元:

對于正整數(shù)
img

img
,如果有
img
,那么把這個同余方程中
img

的最小正整數(shù)解叫做
img
img
的逆元。

逆元一般用擴展歐幾里得算法來求得,如果
img

為素數(shù),那么還可以根據(jù)費馬小定理得到逆元為
img
。

有關(guān)求逆元的方法,我在學(xué)習(xí)之后會更新(^ ^)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在C語言中,五種基本數(shù)據(jù)類型存儲空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 4,007評論 0 2
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,142評論 0 2
  • 回溯算法 回溯法:也稱為試探法,它并不考慮問題規(guī)模的大小,而是從問題的最明顯的最小規(guī)模開始逐步求解出可能的答案,并...
    fredal閱讀 13,993評論 0 89
  • 初五下午,雨心來找我玩,爸爸說要不一起去爬山吧,我和雨心說好呀,就這樣我們準備去爬山了,我們走到了山的最底下,發(fā)現(xiàn)...
    李鶴軒1閱讀 765評論 11 13
  • 微影鏡界閱讀 143評論 0 3

友情鏈接更多精彩內(nèi)容