agc019f - Yes or No
F - Yes or No
Time limit : 2sec / Memory limit : 256MB
Score : 2000 points
Problem Statement
You are participating in a quiz with N+M questions and Yes/No answers.
It's known in advance that there are N questions with answer Yes and M questions with answer No, but the questions are given to you in random order.
You have no idea about correct answers to any of the questions. You answer questions one by one, and for each question you answer, you get to know the correct answer immediately after answering.
Suppose you follow a strategy maximizing the expected number of correct answers you give.
Let this expected number be P⁄Q, an irreducible fraction. Let M=998244353. It can be proven that a unique integer R between 0 and M−1 exists such that P=Q×R modulo M, and it is equal to P×Q−1 modulo M, where Q−1 is the modular inverse of Q. Find R.
Constraints
- 1≤N,M≤500,000
- Both N and M are integers.
Partial Score
- 1500 points will be awarded for passing the testset satisfying N=M and 1≤N,M≤105.
Solution
对于(a,b),我们肯定是猜数量大的那一个(若a>b,猜yes),相等就随便猜一个
本以为是期望DP,很容易想到O(mn)的方法,统计每一条边会被经过次的次数,每条边的贡献=max{x,y}/(x+y)*次数,然后除以总方案数即可。
但想到max{x,y}是经过y=x这一条线的时候发生改变,若从(n,m)->(0,0)不经y=x显然有max(n,m)的贡献。
考虑正常情况:将经过y=x的点的出边从路径中删去,得到若干段不经过y=x的路径,有上面可知路径的总贡献为max(n,m)。
正解就只用单独统计(i,i)点上出边的贡献,最后加上max(n,m)。
Code
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,x) for(int i=head[x];i;i=e[i].next)
#define mem(a,x) memset(a,x,sizeof(a))
typedef long long LL;
typedef double DB;
using namespace std;
template <typename T> inline T read(T &a) {
T x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') f=(ch=='-')?-1:f,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=getchar();a=f*x;
}
const int maxn=500000+10,mo=998244353;
int fac[maxn*2],inv[maxn*2];
int i,j,k,l,t,n,m,ans;
int ksm(int x,int y) {
int ret=1;
for(;y;x=(LL)x*x%mo,y>>=1) if(y&1) ret=(LL)ret*x%mo;
return ret;
}
int C(int n,int m) {
if(n<m||m<0) return 0;
return (LL)fac[n]*inv[m]%mo*inv[n-m]%mo;
}
int main() {
read(n),read(m);
if(n<m) swap(n,m);
fac[0]=1;
fo(i,1,n*2) fac[i]=(LL)fac[i-1]*i%mo;
inv[n*2]=ksm(fac[n*2],mo-2);
fd(i,n*2-1,0) inv[i]=(LL)inv[i+1]*(i+1)%mo;
fo(i,1,m) (ans+=(LL)C(2*i,i)*C(n+m-2*i,n-i)%mo)%=mo;
ans=(LL)ans*ksm(2*C(n+m,n)%mo,mo-2)%mo;
ans=((n+ans)%mo+mo)%mo;
printf("%d\n",ans);
}