You can check the Code directly. The idea is simple. That is, the probability should be calculated during the transfer process. It cannot be regarded as an established statement divided by the total number of solutions (this can be done for poj. Is the data quite Watery. In addition, the value of %. 5f, %. 5lf will be wa for poj .)
#include <map>#include <set>#include <list>#include <cmath>#include <ctime>#include <deque>#include <stack>#include <queue>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <climits>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>#define LL long long#define PI 3.1415926535897932626using namespace std;int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}double dp[105][15];int N,K;void slove(){ if (K <= 1) {puts("100.00000");return;} for (int i = 0 ;i < 105; i++) for (int j = 0;j <15; j++) dp[i][j]=0.0; for (int i = 0 ;i <= K; i++) dp[1][i]=100.0/(double)(K+1); for (int i = 2; i <= N; i++) { dp[i][0] = 1.0/(double)(K+1) * ( dp[i-1][0] + dp[i-1][1]); for (int j = 1; j <= K ; j++) { if (j == K) dp[i][j] = 1.0/(double)(K+1) * (dp[i-1][K] + dp[i-1][K-1]); else dp[i][j] = 1.0/(double)(K+1) * (dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1]); } } double ans=0.0; for (int i = 0 ;i <= K; i ++) ans+=dp[N][i]; printf("%.5lf\n",ans);}int main(){ while (scanf("%d%d",&K,&N)!=EOF) slove(); return 0;}
Ultraviolet A 10081 tight numbers (poj 2537)