E - The Arrow

Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu

Submit Status

Description

The history shows that We need heroes in every dynasty. For example, Liangshan Heroes. People hope that these heroes can punish the bad  guys and recover the justice. Nowadays, we also need heroes to provent us from being chopped, or being attacked by a bomb.

Kuangbin is a very very very very very.... (very * 1e9 ) good boy, after he knows The Arrow, he want to be The Arrow of China. But he is also a little afraid of being killed by the bad guys. So he decides to throw dices to make the decision.

The dice is a cube with 1 2 3 4 5 6 on it's sides. When he throws a dice, every number is of the same probablity to appear. He will write down a number N in the paper at first, and then throw the dice. When the sum of the number he throwed is less than N, he will keep throwing. But if the sum exceeds N, this throwing does not count.

For example, If the sum is 5,and N is 6, if we throw 2, 5 + 2 > 6, then  the sum keeps to be 5.

If he can end the throwing in a certain time, he will make the decision to become The Arrow.

Now , kuangbin wonders that what's the expectation of the time of throwing dices.

Input

First line, The number of cases t <= 100

In the next t lines, there will be t numbers.

every number is not bigger than 100000

Output

Each test output a number rounded to the second digit.

Sample Input

1
1

Sample Output

6.00
题意:给定一个数n。现有一个骰子,六个面分别是123456,掷骰子,记录下掷出来的数的总和以及掷的次数,求和为n的时候,掷的次数的期望为多少。如果掷的和超过n就保持原来的n,比如n=10,第一次掷5第二次掷6,和是11>10,n保持5不变。有T组数据,每组数据给定一个n。
题解:掷到123456的期望是6,掷到7的期望是掷到123456的期望总和除以6+1。给出优化前和优化后的代码:
优化前:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
int t;
double dp[];
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(dp,,sizeof(dp));
for(int i=;i<=;i++)
dp[i]=;
for(int i=;i<=n;i++)
{
dp[i]++; //又掷了一次
for(int j=;j<=;j++)
dp[i]=dp[i]+dp[i-j]/;
}
printf("%.2lf\n",dp[n]);
}
return ;
}

优化后:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=1e5+;
double dp[maxn];
void get()
{
memset(dp,,sizeof(dp));
for(int i=; i<=; i++)
dp[i]=;
for(int i=; i<=maxn; i++)
dp[i]=dp[i-]-dp[i-]/+dp[i-]/;
}
int main()
{
get();
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
printf("%.2lf\n",dp[n]);
}
return ;
}
 

ACdream 1113 The Arrow (概率dp求期望)的更多相关文章

  1. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  2. POJ2096 Collecting Bugs(概率DP,求期望)

    Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...

  3. LightOJ 1030 【概率DP求期望】

    借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...

  4. HDU-3853 LOOPS(概率DP求期望)

    题目大意:在nxm的方格中,从(1,1)走到(n,m).每次只能在原地不动.向右走一格.向下走一格,概率分别为p1(i,j),p2(i,j),p3(i,j).求行走次数的期望. 题目分析:状态转移方程 ...

  5. HDU 3853 LOOP (概率DP求期望)

    D - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  6. HDU 4405 Aeroplane chess (概率DP求期望)

    题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...

  7. HDU-4035 Maze (概率DP求期望)

    题目大意:在一个树形迷宫中,以房间为节点.有n间房间,每间房间存在陷阱的概率为ki,存在出口的概率为ei,如果这两种情况都不存在(概率为pi),那么只能做出选择走向下一个房间(包括可能会走向上一个房间 ...

  8. HDU-4405 Aeroplane chess(概率DP求期望)

    题目大意:一个跳棋游戏,每置一次骰子前进相应的步数.但是有的点可以不用置骰子直接前进,求置骰子次数的平均值. 题目分析:状态很容易定义:dp(i)表示在第 i 个点出发需要置骰子的次数平均值.则状态转 ...

  9. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. Linux环境搭建系列之sorl服务器的安装部署

    http://blog.csdn.net/upxiaofeng/article/details/51425732

  2. Vue学习(四):条件渲染

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. lua优化

    前言 Lua是一门以其性能著称的脚本语言,被广泛应用在很多方面,尤其是游戏.像<魔兽世界>的插件,手机游戏<大掌门><神曲><迷失之地>等都是用Lua来 ...

  4. 树莓派putty远程登录windows

    刚买树莓派的你,还在为要不要购买昂贵的屏幕而纠结吗?看完本博客学会远程登录,妈妈再也不用担心我的学习... 首先我们要知道树莓派的官方推荐系统是raspbian 很建议安装16年9月份的,其他的总是这 ...

  5. spring-data-jpa 简单使用心得

    对于总是使用mybatis的我,突发奇想的想使用spring-data-jpa搭一个小环境,这几天处处碰壁,现总结如下: 环境采用springboot maven需要导入: <dependenc ...

  6. Android问题:could not install *smartsocket* listener;Address already in use

     今天启动genymotion后,发现没有ip地址,运行项目时报错:     可见,没有连接到模拟器,无法运行,而先前说过没有ip,自然而然连接不上,   解决放法:将资源管理器打开,将adb全部退出 ...

  7. hadoop 2.6.0 伪分布式部署安装遇到的问题

    之前读到了一篇关于配置安装hadoop的博文(地址:http://www.powerxing.com/install-hadoop/)能正确安装和运行,但是在网页进行Jobtracker监控时,输入l ...

  8. 如何使用 window.open() 处理ajax请求返回的url: 在本页面打开并防止浏览器拦截

    ajax请求中用window.open()打开请求返回url(例如实现下载功能时),可能会因为跨域问题导致浏览器拦截 解决办法是:在请求前,打开一个窗口,请求成功后将返回的url直接赋值给该窗口的hr ...

  9. 在浏览器中从FTP下载文件

    public static class FTPHelper { /// <summary> /// 得到特定FTP目录的文件列表 /// </summary> /// < ...

  10. 利用 spring 的 task:scheduled-tasks 执行定期任务

    ref是工作类 method是工作类中要执行的方法 initial-delay是任务第一次被调用前的延时,单位毫秒 fixed-delay是上一个调用完成后再次调用的延时 fixed-rate是上一个 ...