码迷,mamicode.com
首页 > 其他好文 > 详细

升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)

时间:2019-08-13 22:25:20      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:现在   hdu   ati   include   杭电   就是   its   sync   compare   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656

题意:

有 1~n 个等级,你现在是1级,求升到n级的花费期望。会给你n个条件(i~i+1级升级所需花费,升级成功概率a/b,失败的话降为x级)。

思路:

期望DP我一般不怎么会,一般都是从 dp[n] 开始转移到 dp[0] 的,但是这题是简单题,从1到n递推就行了(但是赛场是就是不会做)。

我们设 dp[i] 是从 dp[i-1]dp[i] 所需的花费期望值

然后要知道有 a/b 的概率成功,也就是升级 b/a 就有一次成功。(听说期望是概率的倒数,我查了很多资料,还是不懂,只能靠感觉来??)。

所以 dp[ i+1 ] = cost + (1/p - 1)*( dp[ i ] - dp[ x ] + cost )。(p就是a/b,1/p-1就是我们一共搞b/a次有一次会成功,所以要减掉1,后面的dp[i]-dp[x]+cost,就是我们这个一级一级升是满足前缀的关系的,就是你失败了就要dp[x]开始重新升级到dp[i],而且白白浪费了cost,所以要加上)

然后再搞一个前缀,上面的 dp 是单个 i~i+1 的。

所以 sum[i]=dp[i]+sum[i-1] 就行了。

我代码里是一次性搞完,分开的话跟清楚。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>   http://acm.hdu.edu.cn/showproblem.php?pid=6656
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 void swapp(int &a,int &b);
 31 double fabss(double a);
 32 int maxx(int a,int b);
 33 int minn(int a,int b);
 34 int Del_bit_1(int n);
 35 int lowbit(int n);
 36 int abss(int a);
 37 //const long long INF=(1LL<<60);
 38 const double E=2.718281828;
 39 const double PI=acos(-1.0);
 40 const int inf=(1<<29);
 41 const double ESP=1e-9;
 42 const int mod=(int)1e9+7;
 43 const int N=(int)1e6+10;
 44 
 45 long long dp[N];
 46 long long qpow(long long a,long long b,long long mod)
 47 {
 48     long long ans;
 49     a%=mod;
 50     ans=1;
 51     while(b!=0)
 52     {
 53         if(b&1)
 54             ans=(ans*a)%mod;
 55         b/=2;
 56         a=(a*a)%mod;
 57     }
 58     return ans;
 59 }
 60 
 61 int main()
 62 {
 63     int T;
 64     sc("%d",&T);
 65     while(T--)
 66     {
 67         int n,q;
 68         sc("%d%d",&n,&q);
 69         fo(i,1,n)
 70         {
 71             long long a,b,lev,pay;
 72             sc("%lld%lld%lld%lld",&a,&b,&lev,&pay);
 73             dp[i+1]=dp[i]+b*qpow(a,mod-2,mod)%mod*pay%mod+(b-a+mod)%mod*qpow(a,mod-2,mod)%mod*(dp[i]-dp[lev]+mod)%mod;
 74             dp[i+1]%=mod;
 75         }
 76         fo(i,1,q)
 77         {
 78             int a,b;
 79             sc("%d%d",&a,&b);
 80             pr("%lld\n",(dp[b]-dp[a]+mod)%mod);
 81         }
 82     }
 83     return 0;
 84 }
 85 
 86 /**************************************************************************************/
 87 
 88 int maxx(int a,int b)
 89 {
 90     return a>b?a:b;
 91 }
 92 
 93 void swapp(int &a,int &b)
 94 {
 95     a^=b^=a^=b;
 96 }
 97 
 98 int lowbit(int n)
 99 {
100     return n&(-n);
101 }
102 
103 int Del_bit_1(int n)
104 {
105     return n&(n-1);
106 }
107 
108 int abss(int a)
109 {
110     return a>0?a:-a;
111 }
112 
113 double fabss(double a)
114 {
115     return a>0?a:-a;
116 }
117 
118 int minn(int a,int b)
119 {
120     return a<b?a:b;
121 }

 

2019 Multi-University Training Contest 7

升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)

标签:现在   hdu   ati   include   杭电   就是   its   sync   compare   

原文地址:https://www.cnblogs.com/--HPY-7m/p/11348943.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!