n个数围成一个圈,编号0~n-1,从一个数到其上一个和下一个的数的概率相同(即都是0.5)。给出n,求从0出发到达一个数x所需要的步数的数学期望。
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2878
某大神从小数找到一个规律。。。
d[0]=0,d[1]=n-1,d[2]=(n-1)+(n-3),d[3]=(n-1)+(n-3)+(n-5),……。
这样就可以递推了。
但是这道题正解好像是高斯消元,但是在省赛这种稍微水点的,尤其是感觉和数学有关的用递推的方式试试,可能列举几个数就能找出规律,这种方法看起来比较白痴,但是作用有的时候还是比较大的...谨记。。。
高斯消元的方法之后补上。。。
看完这简短的代码瞬间感觉递推找规律很吊很吊有木有。。。
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string> #include<map> #include<set> #include<ctime> #define eps 1e-6 #define MAX 100005 #define INF 0x3f3f3f3f #define LL long long #define pii pair<string,int> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) using namespace std; int main() { int T,x,n; rd(T); int dp[1005]; dp[0]=0; while(T--) { rd2(n,x); for(int i=1,j=1;i<=x;i++,j+=2) dp[i]=dp[i-1]+(n-j); printf("%.4f ",(float)dp[x]); } return 0; }