zoukankan      html  css  js  c++  java
  • AtCoder

    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

     

    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=3Q=2Q−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
    ",ans);
    	return 0;
    }
    

      

     
  • 相关阅读:
    BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )
    BZOJ 1040: [ZJOI2008]骑士( 树形dp )
    BZOJ 1691: [Usaco2007 Dec]挑剔的美食家( 平衡树 )
    HDU 5667 Sequence 矩阵快速幂
    FZU 2225 小茗的魔法阵 扫描线+树状数组
    UVA 11916 Emoogle Grid 离散对数 大步小步算法
    UVA 11754 Code Feat 中国剩余定理+暴力
    FZU 2092 收集水晶 dp+bfs
    FZU2090 旅行社的烦恼 巧妙floyd 最短路
    UVA 11426 GCD
  • 原文地址:https://www.cnblogs.com/JYYHH/p/9168880.html
Copyright © 2011-2022 走看看