(组合数学)AtCoder Grand Contest 019 F - Yes or No

F - Yes or No


Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB

配点 : 2000

問題文

あなたは N+M 問のマルバツクイズが出題されるクイズゲームに参加します。

出題される問題のうち、N 問の正解がマル、M 問の正解がバツであることは事前に知らされていますが、問題は無作為な順序で出題されます。

あなたにはどの問題の正解も見当がつきません。 問題には一問ずつ解答していき、解答するごとにその問題の正解をすぐに知ることができます。

ここで、あなたが問題に正解する回数の期待値を最大化する戦略をとったと仮定します。

この期待値を PQ(既約分数)とします。また、M=998244353 とします。このとき、0 以上 M−1 以下の整数 R がただ一つ存在して P=Q×R mod M となることが証明でき、その値は P×Q−1 mod M と等しくなります。ここで、Q−1Q のモジュラ逆数です。R を求めてください。

制約

  • 1≤N,M≤500,000
  • N,M はともに整数である。

部分点

  • N=M および 1≤N,M≤105 を満たすデータセットに正解すると、1500 点が与えられる。

入力

入力は以下の形式で標準入力から与えられる。

N M

出力

PQ を最適な戦略に従った場合の問題に正解する回数の期待値を表す既約分数とする。P×Q−1 mod 998244353 を出力せよ。


入力例 1

Copy
1 1

出力例 1

Copy
499122178

問題が二問あります。 一問目には無作為に答えてよく、正解する確率は 50% です。 そして、二問目の答えは一問目と異なることが分かっているため、二問目に正解する確率は 100% です。

以上から、正解数の期待値は 3 / 2 です。 したがって、P=3, Q=2, Q−1=499122177 (mod 998244353), P×Q−1=499122178 (mod 998244353) となります。


入力例 2

Copy
2 2

出力例 2

Copy
831870297

正解数の期待値は 17 / 6 です。


入力例 3

Copy
3 4

出力例 3

Copy
770074220

正解数の期待値は 169 / 35 です。


入力例 4

Copy
10 10

出力例 4

Copy
208827570

入力例 5

Copy
42 23

出力例 5

Copy
362936761

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 PQ, 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.

Input

Input is given from Standard Input in the following format:

N M

Output

Let PQ be the expected number of correct answers you give if you follow an optimal strategy, represented as an irreducible fraction. Print P×Q−1 modulo 998244353.


Sample Input 1

Copy
1 1

Sample Output 1

Copy
499122178

There are two questions. You may answer randomly to the first question, and you'll succeed with 50% probability. Then, since you know the second answer is different from the first one, you'll succeed with 100% probability.

The expected number of your correct answers is 3 / 2. Thus, P=3, Q=2, Q−1=499122177 (modulo 998244353), and P×Q−1=499122178 (again, modulo 998244353).


Sample Input 2

Copy
2 2

Sample Output 2

Copy
831870297

The expected number of your correct answers is 17 / 6.


Sample Input 3

Copy
3 4

Sample Output 3

Copy
770074220

The expected number of your correct answers is 169 / 35.


Sample Input 4

Copy
10 10

Sample Output 4

Copy
208827570

Sample Input 5

Copy
42 23

Sample Output 5

Copy
362936761

 

设余x个Yes,y个No,则每次最优的策略显然是选择多的那个。

整个过程可以用一个n*m的矩形及其上格点来表示(图片参照官方题解)。(不妨设n>=m) 过程为从(n,m)走到(0,0) 

作直线y=x 易证在该直线右侧区域(不含该直线)内YES比NO多,即每一次回答YES。在y=x左侧区域相反,在y=x上则为二者数量相等(不含(0,0))

首先可以证明一个结论:从(n,m)到(0,0)的任意路径,不考虑其中经过y=x的点,其余路径选对的次数恰为n  (任意坐标(x,y)表示还有x个YES的问题,y个NO的问题)

证明如下:(以下的y=x左右亦不包含y=x)

设在y=x右侧向左走a次(a>=n-m),则在y=x右侧必向下走a-n+m次,在y=x左侧必向下走n-a次。

而在y=x右侧向左走表明选对,在y=x左侧向下走表明选对。故总共n次。

这样就将y=x以外的点的所有贡献计算了出来 为n*C(n,m)/C(n,m)=n。

接下来只需要考虑y=x上的点。对于任意一点(a,a)其向左、向下走走对的可能性均为1/2 故对最终期望的贡献为 (1/2)*C(n-i,n+m-2*i)*C(i,2*i)/C(n,m)   (为该点期望乘以经过该点的路径数/总路径数)

将两种情况之和加起来即可。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <set>
 6 #include <map>
 7 #include <string>
 8 #include <cstring>
 9 #include <stack>
10 #include <queue>
11 #include <cmath>
12 #include <ctime>
13 #include<bitset>
14 #include <utility>
15 using namespace std;
16 #define rank rankk
17 #define mp make_pair
18 #define pb push_back
19 #define xo(a,b) ((b)&1?(a):0)
20 //#define LL ll
21 typedef unsigned long long ull;
22 typedef pair<int,int> pii;
23 typedef long long ll;
24 typedef pair<ll,int> pli;
25 const int INF=0x3f3f3f3f;
26 const ll INFF=0x3f3f3f3f3f3f3f3fll;
27 const int MAX=1e6+5;
28 const int MAX_N=MAX;
29 const ll MOD=998244353;
30 const long double pi=acos(-1.0);
31 //const double eps=0.00000001;
32 int gcd(int a,int b){return b?gcd(b,a%b):a;}
33 template<typename T>inline T abs(T a) {return a>0?a:-a;}
34 template<class T> inline
35 void read(T& num) {
36     bool start=false,neg=false;
37     char c;
38     num=0;
39     while((c=getchar())!=EOF) {
40         if(c=='-') start=neg=true;
41         else if(c>='0' && c<='9') {
42             start=true;
43             num=num*10+c-'0';
44         } else if(start) break;
45     }
46     if(neg) num=-num;
47 }
48 inline ll powMM(ll a,ll b,ll M){
49     ll ret=1;
50     a%=M;
51 //    b%=M;
52     while (b){
53         if (b&1) ret=ret*a%M;
54         b>>=1;
55         a=a*a%M;
56     }
57     return ret;
58 }
59 void open()
60 {
61     freopen("1009.in","r",stdin);
62     freopen("out.txt","w",stdout);
63 }
64 ll inv[MAX],fac[MAX];
65 ll C(ll x,ll y)
66 {
67     return fac[x]*inv[y]%MOD*inv[x-y]%MOD;
68 }
69 int n,m;ll da=1e6;
70 ll an;
71 int main()
72 {
73     fac[0]=1;for(ll i=1;i<=da;i++)fac[i]=i*fac[i-1]%MOD;
74     inv[da]=powMM(fac[da],MOD-2,MOD);for(ll i=da;i>=1;i--)inv[i-1]=inv[i]*i%MOD;
75     scanf("%d%d",&n,&m);if(n<m)swap(n,m);
76     for(ll i=1;i<=m;i++)
77         an=(an+C(n+m-2*i,n-i)*C(2*i,i)%MOD)%MOD;
78     an=an*inv[2]%MOD*fac[n]%MOD*fac[m]%MOD*inv[n+m]%MOD;
79     printf("%lld\n",(an+n)%MOD);
80 }

 

转载于:https://www.cnblogs.com/quintessence/p/7464176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值