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
  2. 1

Sample Output

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

优化后:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. using namespace std;
  5. const int maxn=1e5+;
  6. double dp[maxn];
  7. void get()
  8. {
  9. memset(dp,,sizeof(dp));
  10. for(int i=; i<=; i++)
  11. dp[i]=;
  12. for(int i=; i<=maxn; i++)
  13. dp[i]=dp[i-]-dp[i-]/+dp[i-]/;
  14. }
  15. int main()
  16. {
  17. get();
  18. int t;
  19. cin>>t;
  20. while(t--)
  21. {
  22. int n;
  23. cin>>n;
  24. printf("%.2lf\n",dp[n]);
  25. }
  26. return ;
  27. }
  1.  

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. centos linux 因别名问题引起的麻烦及解决技巧

    老男孩儿-19期 L005-13节中分享.自己整理后发到自己微博中留档. 原文:http://oldboy.blog.51cto.com/2561410/699046 实例:老男孩linux实战培训第 ...

  2. python基础——元组、文件及其它

    Python核心数据类型--元组 元组对象(tuple)是序列,它具有不可改变性,和字符串类似.从语法上讲,它们便在圆括号中,它们支持任意类型.任意嵌套及常见的序列操作. 任意对象的有序集合:与字符串 ...

  3. Android Google Maps 开始

    由于工作需要,最近对Android的各大地图进行了试用. 其中有Google地图,百度地图,高德地图,还有开源的OSM. 在使用Google地图的时候,官网流程写的非常清楚,但是其中也遇到一些问题.这 ...

  4. 『AngularJS』理解$Scope

    理解$Scope 执行概要 在AngularJS,一个子scope通常原型继承于它的父scope.应用于这个规则的表达式是一个使用scope:{...}的指令,这将创建一个『孤岛』scope(非原型继 ...

  5. iframe底边多出4px或5px解决办法

    问题: 在处理iframe框架自适应时,并且已经去掉iframe的边框,但仍然出现底边多出4px或5px高度的情况.如图 <div id="content"> < ...

  6. 自动化测试(一)-get和post的简单应用

    今天主要介绍两种测试的接口post和get: get和post是http的两种基本请求方式,区别在于get把参数包含在url中传递:给而post把参数以json或键值对的方式利用工具传递. get的传 ...

  7. Mysql性能优化四:分库,分区,分表,你们如何做?

    分库分区分表概念 分区 就是把一张表的数据分成N个区块,在逻辑上看最终只是一张表,但底层是由N个物理区块组成的 分表 就是把一张数据量很大的表按一定的规则分解成N个具有独立存储空间的实体表.系统读写时 ...

  8. LeetCode 4——两个排序数组中的中位数

    1. 题目 2. 解答 2.1. 方法一 由于两个数组都是排好序的,因此首先可以想到的思路就是利用归并排序把两个数组合并成一个有序的长数组,然后直接取出中位数即可. class Solution: d ...

  9. ubuntu中 VI 方向键、删除键问题

    这两天重新装的ubuntu系统,发觉使用VI时,方向键按下去后变成ABCD,删除键无效.网上搜寻一番,应该是VI软件本身的问题,顾卸载重装即可,步骤如下: 1.执行命令 sudo apt-get re ...

  10. DFS(3)——poj1321棋盘问题

    一.题目回顾 题目链接:棋盘问题 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于 ...