HDU 4599 Dice

來源:互聯網
上載者:User

標籤:機率dp   數論   模的逆元   數學推導   

DiceTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 211    Accepted Submission(s): 127


Problem DescriptionGiven a normal dice (with 1, 2, 3, 4, 5, 6 on each face), we define: 
F(N) to be the expected number of tosses until we have a number facing up for N consecutive times.
H(N) to be the expected number of tosses until we have the number ‘1‘ facing up for N consecutive times.
G(M) to be the expected number of tosses until we have the number ‘1‘ facing up for M times.
Given N, you are supposed to calculate the minimal M1 that G (M1) >= F (N) and the minimal M2 that G(M2)>=H(N) 
InputThe input contains multiple cases. 
Each case has a positive integer N in a separated line. (1<=N<=1000000000) 
The input is terminated by a line containing a single 0. 
OutputFor each case, output the minimal M1 and M2 as required in a single line, separated by a single space. 
Since the answer could be very large, you should output the answer mod 2011 instead. 
Sample Input
120
 
Sample Output
1 1 2 7
 
Source2013 ACM-ICPC吉林通化全國邀請賽——題目重現 
Recommendliuyiding   |   We have carefully selected several similar problems for you:  4934 4933 4932 4931 4930  
Statistic | Submit | Discuss | Note

設dp[i]為已經連續擲出i次相同點數,要到達目標狀態所需的平均期望
則:dp[i] = (1/6)*(dp[i+1]+1) + (5/6)*(dp[1]+1)-----------------(1)

{ dp[0]是我們最終要求的值,dp[n] = 0 }


其中,下次擲出相同點數的機率為(1/6),再加上本身擲的一次,因此為(1/6)*(dp[i+1]+1)
如果下次擲的點數不同則相當於從1開始擲,因此為(5/6)*(dp[1]+1)
化簡上式(1)可得:
dp[i] = (1/6)*(dp[i+1]) + (5/6)*(dp[1]) + 1------------------------(2)
dp[i+1] = (1/6)*(dp[i+2]) + (5/6)*(dp[1]) + 1---------------------(3)



兩式相減(3) - (2)可得:
dp[i+1] - dp[i] = (1/6)*(dp[i+2] - dp[i+1])------------------------(4)


令a[i] = dp[i] - dp[i+1]-----------------------------------------------(5)
則a[i+1] = dp[i+2] - dp[i+1]
則(4)式變為:a[i+1] = 6*a[i]---------------------------------------(6)
由(2)式可知:dp[0] - dp[1] = 1,則a[0] = 1


因此a[i] = (1/6)^i------------------------------------------------------(7)


dp[n-1] - dp[n] = 6^(n-1)
dp[n-2] - dp[n-1] = 6^(n-2)
...
dp[0] - dp[1] = 1

則dp[0] = dp[n] + 6^0 + 6^1 + 6^2 + ... + 6^(n-2) + 6^(n-1)-----(8)
由於dp[n] = 0,則最終我們要求的結果dp[0]:
dp[0] = (6^n - 1)/5----------------------------------------------------(9)
即F[n] = (6^n - 1)/5--------------------------------------------------(10)
H[n] = 6*F[n] = 6*(6^n - 1)/5--------------------------------------(11)
G[m] = 6*m-----------------------------------------------------------(12)

由於題目要求使得G[m1]>=F[n] && G[m2]>=H[n]的最小整數值
則 m1 >= (6^n - 1)/30, m2>=(6^n - 1)/5
由於6^n尾數為6,因此要使得m1、m2為整數,則
m1 >= (6^n + 24)/30, m2>=(6^n - 1)/5

即求出在n的情況下上述兩個運算式的值即可,
分母的情況用模n情況下的a的逆元來解決。

#include<stdio.h>#include<iostream>using namespace std;typedef long long LL;const LL MOD = 2011;LL quick_mod(LL a,LL b,LL m){    LL ans = 1;    while(b)    {        if(b&1)        {            ans = (ans*a)%m;            b--;        }        b/=2;        a = a*a%m;    }    return ans;}int main(){    //F(n) = (6^n-1)/5-----H(n) = 6*F(n)-----G(m) = 6*m;    //M1 = (6^n+24)/30-----M2 = (6^n-1)/5    LL N;    while(scanf("%I64d",&N) && N)    {        LL hehe,ans1,ans2;        hehe = quick_mod(6,N,MOD);        ans1 = ((hehe+24+MOD)%MOD*1944)%MOD;        ans2 = ((hehe-1+MOD)%MOD*1609)%MOD;        printf("%I64d %I64d\n",ans1,ans2);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.