AtCoder - 2705 Yes or No
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.
Input
Input is given from Standard Input in the following format:
N M
Output
Let P⁄Q 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
1 1
Sample Output 1
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
2 2
Sample Output 2
831870297
The expected number of your correct answers is 17 / 6.
Sample Input 3
3 4
Sample Output 3
770074220
The expected number of your correct answers is 169 / 35.
Sample Input 4
10 10
Sample Output 4
208827570
Sample Input 5
42 23
Sample Output 5
362936761
wjz神犇省队集训的时候讲的神题!太笨了当时没有听懂,后来看了看课件懂QWQ
首先贪心策略比较显然,就是先选目前剩的多的类型。。
(先假设 n>=m 也就是 Yes > No)
然后选硬币就相当于在二维平面上行走,起点是(n,m),重点是(0,0),每次向左走代表选Yes,向下走代表选No。
当目前位置在 y=x 上方的时候,向下走会有1的贡献。
当目前位置的 y=x 下方的时候,向左走会有1的贡献。
当目前在 y=x 上的时候,怎么走都会有 1/2 的贡献(期望贡献)
所以我们就可以把在y=x和不在的贡献分开算。。。。
可以发现不在y=x的点的期望贡献总是n,因为当我们把y=x上方的路径对称下来的时候,可以发现不管怎么走,最后都一共向左走了n步。。
然后在y=x上的点的贡献就可以用期望的线性性拆开算了,对于每个点单独算经过它的概率,然后求和即可。
总的Yes/No 排列的种类显然是 C(n+m,n) 的,经过点(i,i) 的方案数则是 C(i*2,i) * C(n+m-i*2,n-i)的。
/* */
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000000,ha=998244353;
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;} inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} int jc[maxn+5],ni[maxn+5],n,m,ans; inline int C(int x,int y){ return x<y?0:jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;} inline void init(){
jc[0]=1;
for(int i=1;i<=maxn;i++) jc[i]=jc[i-1]*(ll)i%ha;
ni[maxn]=ksm(jc[maxn],ha-2);
for(int i=maxn;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
} inline void solve(){
for(int i=1;i<=m;i++)
ADD(ans,C(i<<1,i)*(ll)C(n+m-i*2,n-i)%ha); } int main(){
init(); scanf("%d%d",&n,&m);
if(n<m) swap(n,m); solve(); ans=ans*(ll)ni[2]%ha*(ll)ksm(C(n+m,n),ha-2)%ha; ADD(ans,n);
printf("%d\n",ans);
return 0;
}
AtCoder - 2705 Yes or No的更多相关文章
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- BZOJ 2705: [SDOI2012]Longge的问题
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2554 Solved: 1566[Submit][ ...
- Dividing a Chocolate(zoj 2705)
Dividing a Chocolate zoj 2705 递推,找规律的题目: 具体思路见:http://blog.csdn.net/u010770930/article/details/97693 ...
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- 【BZOJ】【2705】【SDOI2012】Longge的问题
欧拉函数/狄利克雷卷积/积性函数 2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1275 Solv ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- BZOJ 2705: [SDOI2012]Longge的问题 GCD
2705: [SDOI2012]Longge的问题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...
- bzoj 2705: [SDOI2012]Longge的问题 歐拉函數
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1035 Solved: 669[Submit][S ...
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
随机推荐
- final变量属性小记
final 修饰符对于类成员变量来说,具备语法上不可变的特性:对于类成员方法来说,具备语法上子类不可覆盖重写的特性(能被继承的前提下). 但 final 并不限制子类对父类被修饰声明的成员变量进行覆盖 ...
- js和jquery修改背景颜色的区别
html: <HTML> <head> <meta http-equiv="content-type" content="text/html ...
- bzoj1575 [Usaco2009 Jan]气象牛Baric
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1575 [题解] 动态规划,令f[i,j]表示前i个选了j个,且第i个必选的最小值. 转移就枚 ...
- hdu3294 manacher算法
这道题哇 其实是裸的manacher 无论怎么变 是回文的就是回文 所以 特殊处理一下输出就好了 不过最后的左右端点l,r.l=(p-p[pos]+2)/2-1,r=(p+p[pos]-2)/2-1; ...
- 一致性hash算法小结
把服务器的IP或者主机名作为key对2^32求余,余数一定是2^32-1,然后放到(平行映射)0~2^32次方首尾相连的环上. 理想状态下服务器会均匀分布在这个环上,当数据存储时,依然把key对2 ...
- LeetCode 2 :Swap Nodes in Pairs
我的代码是这样的: class Solution { public: ListNode *swapPairs(ListNode *head) { ; ; ListNode *listA; ListNo ...
- [Leetcode Week7]Jump Game
Jump Game 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/jump-game/description/ Description Given a ...
- android 使用开源库zxing生成二维码,扫描二维码【转】
转自:http://blog.csdn.net/qq_16064871/article/details/52422723 zxing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库 ...
- vim 源码分析
vim 源码分析 http://bbs.csdn.net/topics/230031469 Ver7.1 晕.看不明白很正常. 7.1已经很大了. 支持了太多东西. 代码行数那么多(源码压缩了都 ...
- npm编译报错,缺少组件
解决方式: 1.删除安装文件 node_modules: 2.在需要安装 node_modules 文件的文件夹中,打开命令窗口,输入: cnpm install: 3.再输入: npm start, ...