山东省第五届ACM省赛题——Circle(递推求概率)
来源:互联网 发布:贵金属软件 编辑:程序博客网 时间:2024/06/07 17:12
题目描述
You have been given a circle from 0 to n - 1. If you are currently at x, you will move to (x - 1) mod n or (x + 1) mod n with equal probability. Now we want to know the expected number of steps you need to reach x from 0.
输入
The first line contains one integer T — the number of test cases.
Each of the next T lines contains two integers n, x (0 ≤ x < n ≤ 1000) as we mention above.
输出
For each test case. Print a single float number — the expected number of steps you need to reach x from 0. The figure is accurate to 4 decimal places.
示例输入
3
3 2
5 4
10 5
示例输出
2.0000
4.0000
25.0000
史上最淫荡的递推!
首先可以得出结论d[i]=0.5*d[i-1]+0.5*d[i+1]+1,那么就先递推吧
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#include <bitset>#define MAXN 10000010using namespace std;int n;double d[1010];void fun(){ memset(d,0,sizeof(d)); for(int t=0;t<5000;++t) for(int i=1;i<n;++i) d[i]=0.5*d[i+1]+0.5*d[i-1]+1.0;}int main(){ int t,x; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&x); fun(); printf("%.4lf\n",d[x]); } return 0;}
我这里模拟走5000步,得出来的数据能过样例,据说当时省赛就有人这样过的,但现在在山东理工上过不了,不知道是不是后台数据加强了。。。但依然可以根据这个得到规律,下面是我得出的n为10的所有期望
50
10 0
0.0000
10 1
9.0000
10 2
16.0000
10 3
21.0000
10 4
24.0000
10 5
25.0000
10 6
24.0000
10 7
21.0000
10 8
16.0000
10 9
9.0000
10 10
0.0000
规律很明显了,就是d[i]=(n-1)+(n-3)+(n-5)…..接着我就过了
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#include <bitset>#define MAXN 10000010using namespace std;int n;double d[1010];int main(){ int t,x; scanf("%d",&t); while(t--) { d[0]=0; scanf("%d%d",&n,&x); if(x>n/2) x=n-x; for(int i=1;i<=n/2;++i) d[i]=d[i-1]+(n-(2*i-1)); printf("%.4lf\n",d[x]); } return 0;}
- 山东省第五届ACM省赛题——Circle(递推求概率)
- 第五届山东省ACM Circle(高斯消元)
- Circle(山东省第五届ACM大学生程序设计竞赛 )
- 2014山东省第五届ACM省赛 Circle
- 2014山东省第五届ACM省赛 Circle
- 山东省第六届ACM省赛题——Circle of Friends(强连通分量+dfs)
- 山东省第五届ACM省赛题——Full Binary Tree(二叉树应用)
- 山东省第五届ACM省赛题——angry_birds_again_and_again(计算几何)
- 山东省第五届ACM省赛题——Hearthstone II(第二类Stiring数)
- 山东省第五届ACM省赛题——Weighted Median(模拟)
- 山东省第五届ACM省赛题——Colorful Cupcakes(四维dp)
- 山东省第五届ACM省赛题——Painting Cottages(求点集的划分)
- 山东省第五届ACM省赛题——Devour Magic(线段树区间覆盖+更新)
- 2014年山东省第五届ACM大学生程序设计竞赛-B-Circle
- 第五届山东省ACM Factorial
- 山东省第一届ACM省赛题——Balloons(搜索)
- 山东省第七届ACM省赛题——Fibonacci(二分)
- sdut 2878 Circle 第五届ACM省赛(高斯消元)
- 【opencv学习】之split函数提取多通道图像的单通道图像
- 学习JAVA一
- HTML 表单(form) 详解
- LeeCode-Remove Linked List Elements
- YEOMAN知识点整理及翻译
- 山东省第五届ACM省赛题——Circle(递推求概率)
- 在虚拟机扩展linux系统的硬盘
- 如果json字符串中的key值中没有数据解析用opt,也会出现空指针异常。
- Oracle_expdp_impdp用法详解
- @RequestBody和@ResponseBody的简单使用
- 最大公约数的实现
- 欢迎使用CSDN-markdown编辑器
- Swift 很强大的图表库-Charts使用
- 25.Spark Sort-Based Shuffle内幕彻底解密