51nod 1836:战忽局的手段
2017-02-20 16:32
302 查看
51nod 1836:战忽局的手段
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1836
矩阵快速幂
从题目所给的数据范围来看复杂度应该为O(1)或者O(lgn),于是定义ƒ(x)为n个事件,x次忽悠下的期望事件数
很明显可以得到这样一个递推式:ƒ(x)=ƒ(x-1)+[1-ƒ(x-1)/n]=1+(1-1/n)ƒ(x-1)
于是我们就可以构造矩阵以O(lgn)的复杂度来解这道题啦。
/*注意这道题卡精度,可以用__float128来保存中间变量减少精度丢失*/
代码如下:
当然也可以将递推式继续推算下去,以O(1)的复杂度解决。(数据小的话直接用math.h,否则直接取极限)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1836
矩阵快速幂
从题目所给的数据范围来看复杂度应该为O(1)或者O(lgn),于是定义ƒ(x)为n个事件,x次忽悠下的期望事件数
很明显可以得到这样一个递推式:ƒ(x)=ƒ(x-1)+[1-ƒ(x-1)/n]=1+(1-1/n)ƒ(x-1)
于是我们就可以构造矩阵以O(lgn)的复杂度来解这道题啦。
/*注意这道题卡精度,可以用__float128来保存中间变量减少精度丢失*/
代码如下:
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; ll T,n,m; struct matrix{ __float128 mp[2][2]; friend matrix operator * (matrix a,matrix b){ matrix t; for(int i=0;i<2;++i) for(int j=0;j<2;++j){ t.mp[i][j]=0; for(int k=0;k<2;++k) t.mp[i][j]+=a.mp[i][k]*b.mp[k][j]; } return t; } }E,A; matrix pow(matrix A,ll n){ matrix base=A,ans=E; while(n){ if(n&1)ans=ans*base; base=base*base; n>>=1; } return ans; } int main(void){ E.mp[0][0]=E.mp[1][1]=1; E.mp[0][1]=E.mp[1][0]=0; scanf("%lld",&T); while(T--){ scanf("%lld%lld",&n,&m); A.mp[0][0]=(__float128)1-(__float128)1/(__float128)n;A.mp[0][1]=1; A.mp[1][0]=0;A.mp[1][1]=1; matrix temp=pow(A,m-1); double ans=(double)(temp.mp[0][0]+temp.mp[0][1]); printf("%.7lf\n",ans); } }
当然也可以将递推式继续推算下去,以O(1)的复杂度解决。(数据小的话直接用math.h,否则直接取极限)
相关文章推荐
- 51Nod-1836-战忽局的手段
- 51NOD 1836 战忽局的手段(矩阵乘法加速+__float128精度问题+概率期望)——算法马拉松20(告别美国大选及卡斯特罗)
- 51nod 1836 战忽局的手段 矩阵乘法
- 51nod 1836 战忽局的手段 (期望)
- 51nod 1836 战忽局的手段
- 51nod 1836:战忽局的手段(期望)
- 51nod 1836 战忽局的手段
- 51nod 1836(矩阵快速幂,求期望,__float128)
- 51nod算法马拉松20 C、战忽局的手段
- 【51Nod1836】战忽局的手段
- [51nod 1667]概率好题
- 51nod 1299 监狱逃离 树形dp/最小割
- 51nod 1247 可能的路径
- 51nod 1589 移数博弈 | 基数排序(ノಠ益ಠ)ノ彡┻━┻
- 【51nod】1302 矩形面积交【优先队列贪心】
- 51nod 1450 闯关游戏 期望dp
- 51nod 1127 最短的包含字符串
- 51nod 算法马拉松30 A.函数【容斥】【组合数学】
- 51nod 1089 最长回文子串 V2(Manacher算法)
- 51nod 1592 数列积 莫队算法+树状数组