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

(组合数学)AtCoder Grand Contest 019 F - Yes or No相关推荐

  1. AtCoder Grand Contest 019

    最近比较懒,写了俩题就跑了 A - Ice Tea Store 简化背包 #include<cstdio> #include<algorithm> using namespac ...

  2. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...

  3. AtCoder Grand Contest 017

    AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...

  4. AtCoder Beginner Contest 215 F - Dist Max 2

    AtCoder Beginner Contest 215 F - Dist Max 2 平面上有一系列的点(xi,yi)(x_i,y_i)(xi​,yi​),定义两点(xi,yi),(xj,yj)(x ...

  5. AtCoder Beginner Contest 204 F Hanjo 2

    AtCoder Beginner Contest 204 F Hanjo 2 H宽,W长的二维平面上,用1 * 1或者2 * 1的地砖来铺,要求铺满,求出方案数. 数据范围H <= 6, W & ...

  6. AtCoder Grand Contest 008: Contiguous Repainting(思维)

    Contiguous Repainting 时间限制: 2 Sec  内存限制: 256 MB 提交: 69  解决: 22 [提交][状态][讨论版][命题人:admin] 题目描述 There a ...

  7. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  8. AtCoder Beginner Contest 170 F. Pond Skater

    AtCoder Beginner Contest 170 F. Pond Skater 题目链接 第一次碰到会写的 F,真的哭辽/(ㄒoㄒ)/~~,BFS+剪枝 题目有几个坑点: 1.初始化,我们直接 ...

  9. AtCoder Beginner Contest 167 F.Bracket Sequencing

    AtCoder Beginner Contest 167 F.Bracket Sequencing 题目链接 判断括号匹配的字符串问题~ 首先给出的所有字符串的左右括号数是要匹配的,这个很好判断,用一 ...

最新文章

  1. 用动画实现android app启动界面的渐变效果
  2. Spring Cloud Alibaba基础教程:@SentinelResource注解实现限流控制与熔断降级使用详解
  3. boost::hana::when用法的测试程序
  4. 机器学习基础-主成分分析PCA-16
  5. Spring MVC 源码-运行调用阶段
  6. 重操JS旧业第十一弹:BOM对象
  7. python实现mini-batch_Mini-Batch 、Momentum、Adam算法的实现
  8. aspell_如何使用Aspell在Linux命令行上检查拼写
  9. 铺铜需要把agnd和dgnd分开_AGND和DGND的秘密——混合信号器件的接地原则
  10. 李迟2021年11月知识总结
  11. 【小技巧积累】用Style实现必填提示“*”根据选项的不同而显示或隐藏
  12. app 播放服务器文件,配置apple-app-site-association文件并在服务器上传
  13. 以树莓派为控制中心的软硬件之开发之脑电波模块
  14. buuctf:Ping Ping Ping(命令执行)
  15. Netbackup 8.1 许可证
  16. pico-ctf-2013 overflow-2
  17. 获取dom元素的方法
  18. 【转载】html转义字符
  19. iMovie教程:如何将照片添加至iMovie剪辑中?
  20. c语言程序设计医院就医,C语言程序设计(医院信息管理系统)附源代码[精品].doc...

热门文章

  1. 如何查找Fiori UI上某个字段对应的后台存储表的名称
  2. 计算机应用基础寒假作业,计算机应用基础理论试卷寒假作业.doc
  3. 常见Orcale报错问题解决办法
  4. 每小时的数据mysql_荐 mysql查询每小时数据和上小时数据的差值
  5. 命名时取代基优先顺序_【选修五】高中化学重难点知识:有机物的命名方法
  6. 几分钟学会归并排序和快速排序
  7. python 函数可以作为容器对象的元素_python第十二天, 三元表达式, 函数对象,名称空间与作用域,函数的嵌套定义...
  8. js 封装经纬度成json_全国经纬度json文件
  9. zipkin使用_Sleuth和Zipkin进行分布式链路跟踪,一点课堂(多岸学院)
  10. log4j2.xml 文件