AT2705 Yes or No(组合数学)
生活随笔
收集整理的這篇文章主要介紹了
AT2705 Yes or No(组合数学)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
解題思路
首先將這個模型放到坐標軸上,\(x\)軸表示\(1\),\(y\)軸表示\(0\)。問題就轉化成了從\((0,0)\)走到\((n,m)\),每次可以猜測向\(x\)軸或向\(y\)軸,而實際也有一條路線,求猜中的個數的期望。假設\(n<m\)首先如果一直猜\(m\),答案必然為\(m\),那么這是答案的下界。再考慮過\((n,m)\)做一條斜率為\(1\)的直線,如果在直線上,那么猜中的概率其實就為\(\frac{1}{2}\)。,而不在坐標軸上猜中的期望其實就為\(m\)。那么現在就是求走到直線的概率,根據期望的線性,可以考慮直線上每一個點產生的貢獻,過這個點的路線就可以用組合數輕松算出了。
代碼
#include<iostream> #include<cstdio> #include<cstring> #include<cmath>using namespace std; const int N=1000005; const int MOD=998244353; typedef long long LL;int n,m,fac[N],inv[N]; int Ans1,Ans2;//LL gcd(LL x,LL y) { // if(!y) return x; // return gcd(y,x%y); //} // //struct Data{ // LL x,y; // Data(LL _x=0,LL _y=0) {x=_x; y=_y;} // friend Data operator+(const Data A,const Data B){ // Data ret; ret.y=A.y*B.y; ret.x=A.x*B.y+A.y*B.x; // LL tmp=gcd(ret.x,ret.y); ret.x/=tmp; ret.y/=tmp; // return ret; // } // friend Data operator*(const Data A,const Data B){ // Data ret; ret.x=A.x*B.x; ret.y=A.y*B.y; // LL tmp=gcd(ret.x,ret.y); ret.x/=tmp; ret.y/=tmp; // return ret; // } //}ans;inline int fast_pow(int x,int y){int ret=1;for(;y;y>>=1){if(y&1) ret=(LL)ret*x%MOD;x=(LL)x*x%MOD;}return ret; }inline int C(int x,int y){return (LL)fac[x]*inv[y]%MOD*inv[x-y]%MOD; }int main(){scanf("%d%d",&n,&m);if(n>m) swap(n,m); fac[0]=inv[0]=1;for(int i=1;i<=n+m;i++) fac[i]=1ll*fac[i-1]*i%MOD;inv[n+m]=fast_pow(fac[n+m],MOD-2);for(int i=n+m-1;~i;i--) inv[i]=1ll*inv[i+1]*(i+1)%MOD;Ans1=1ll*2*m*C(n+m,n)%MOD; Ans2=fast_pow(C(n+m,n)*2%MOD,MOD-2);for(int i=1;i<=n;i++) {Ans1=Ans1+1ll*C(n-i+m-i,n-i)*C(i+i,i)%MOD; Ans1%=MOD;}printf("%lld\n",1ll*Ans1*Ans2%MOD); // ans=ans+Data(1,2)*Data(C(n-i+m-i,n-i)*C(i+i,i),C(n+m,n)); // printf("%lld\n",1ll*ans.x*fast_pow(ans.y,MOD-2)%MOD);return 0; }轉載于:https://www.cnblogs.com/sdfzsyq/p/10396377.html
總結
以上是生活随笔為你收集整理的AT2705 Yes or No(组合数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring MVC学习step1——框
- 下一篇: nginx+keepalived双机热备