首页 > 其他分享 >【题解 CF1628D2】 Game on Sum

【题解 CF1628D2】 Game on Sum

时间:2023-11-17 10:23:28浏览次数:33  
标签:10 CF1628D2 le 题解 Alice long number Game Bob

Game on Sum (Hard Version)

题面翻译

Alice 和 Bob 正在玩一个游戏,游戏分为 \(n\) 个回合,Alice 和 Bob 要轮流对一个数 \(x\) 进行操作,已知这个数初始值是 \(0\)。

具体每个回合的行动规则如下:

  1. Alice 选择一个在区间 \([0,k]\) 之间的实数 \(t\)。
  2. Bob 可以选择让 \(x\) 变成 \(x+t\) 或者 \(x-t\),但是 Bob 在 \(n\) 个回合之内至少选择 \(m\) 次让 \(x\) 变成 \(x+t\)。

Alice想让最终的 \(x\) 最大,Bob 想让最终的 \(x\) 最小。

已知双方均采用最优策略,求最终的 \(x\) 值(对 \(10^9+7\) 取模)。

数据范围保证:\(1\le m\le n\le 10^6,k\le10^9+7\), \(\sum\limits n\leq10^6\) 。

题目描述

This is the hard version of the problem. The difference is the constraints on $ n $ , $ m $ and $ t $ . You can make hacks only if all versions of the problem are solved.

Alice and Bob are given the numbers $ n $ , $ m $ and $ k $ , and play a game as follows:

The game has a score that Alice tries to maximize, and Bob tries to minimize. The score is initially $ 0 $ . The game consists of $ n $ turns. Each turn, Alice picks a real number from $ 0 $ to $ k $ (inclusive) which Bob either adds to or subtracts from the score of the game. But throughout the game, Bob has to choose to add at least $ m $ out of the $ n $ turns.

Bob gets to know which number Alice picked before deciding whether to add or subtract the number from the score, and Alice gets to know whether Bob added or subtracted the number for the previous turn before picking the number for the current turn (except on the first turn since there was no previous turn).

If Alice and Bob play optimally, what will the final score of the game be?

输入格式

The first line of the input contains a single integer $ t $ ( $ 1 \le t \le 10^5 $ ) — the number of test cases. The description of test cases follows.

Each test case consists of a single line containing the three integers, $ n $ , $ m $ , and $ k $ ( $ 1 \le m \le n \le 10^6, 0 \le k < 10^9 + 7 $ ) — the number of turns, how many of those turns Bob has to add, and the biggest number Alice can choose, respectively.

It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^6 $ .

输出格式

For each test case output a single integer number — the score of the optimal game modulo $ 10^9 + 7 $ .

Formally, let $ M = 10^9 + 7 $ . It can be shown that the answer can be expressed as an irreducible fraction $ \frac{p}{q} $ , where $ p $ and $ q $ are integers and $ q \not \equiv 0 \pmod{M} $ . Output the integer equal to $ p \cdot q^{-1} \bmod M $ . In other words, output such an integer $ x $ that $ 0 \le x < M $ and $ x \cdot q \equiv p \pmod{M} $ .

样例 #1

样例输入 #1

7
3 3 2
2 1 10
6 3 10
6 4 10
100 1 1
4 4 0
69 4 20

样例输出 #1

6
5
375000012
500000026
958557139
0
49735962

提示

In the first test case, the entire game has $ 3 $ turns, and since $ m = 3 $ , Bob has to add in each of them. Therefore Alice should pick the biggest number she can, which is $ k = 2 $ , every turn.

In the third test case, Alice has a strategy to guarantee a score of $ \frac{75}{8} \equiv 375000012 \pmod{10^9 + 7} $ .

In the fourth test case, Alice has a strategy to guarantee a score of $ \frac{45}{2} \equiv 500000026 \pmod{10^9 + 7} $ .

解题思路

我们先思考用 \(DP\) 来解决这道题。
我们可以设 \(f_{i,j}\) ,表示已进行了 \(i\) 次,其中 \(j\) 次是加运算,但是因为博弈论,每个 \(f_{i,j}\) 只基于后面的决策,而且一般概率 \(DP\) 是从前往后考虑的,所应把 \(f_{i,j}\) 设为还剩下 \(i\) 次未进行,还未加 \(j\) 次。
边界条件即为 \(f_{i,i}=i \times k\) 。
转移方程即为 \(f_{i,j}=min(f_{i+1,j}-x,f_{i+1,j-1}+x)\) ,当 \(x=\frac{f_{i+1,j}-f_{i+1,j-1}}{2}\) 是 \(f_{i,j}\) 取最大值,所以 \(f_{i,j} =\frac{f_{i+1,j}+f_{i+1,j-1}}{2}\) 。
这样就有了 \(nm\) 的做法了。
要拿 \(100\) ,我们需要思考这个 \(DP\) 的本质。
对于每一个 \(f_{i,i}\) ,思考他对答案的贡献,它走到答案即为 \(k\) 乘上路径条数再除以若干次 \(2\) 。
因为 \(f_{i,i}\) 不能走到 \(f_{i+1,i+1}\),路径条数即为 \(f_{i+1,i}\) 走到 \(f_{n,m}\) 的条数,而除以 \(2\) 的次数即为 \(n-i\) ,所以答案为 \(k \times \sum_{i = 1}^{m} \frac{i \times C_{n-i-1}^{m-1}}{2^{n-i}}\)
特判 \(n=m\) 的情况,时间复杂度 \(O(nlogn)\) 。

Code

#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long n,m,k,f[1000005];
long long dijah(long long x,long long y)
{
	long long h=1;
	while(y)
	{
		if(y&1)
		{
			h*=x;
			h%=mod;
		}
		x*=x;
		x%=mod;
		y>>=1;
	}
	return h;
}
long long C(long long x,long long y)
{
	return (f[x]*dijah(f[x-y],mod-2)%mod)*dijah(f[y],mod-2)%mod;
}
void qer()
{
	scanf("%lld%lld%lld",&n,&m,&k);
	if(n==m)
	{
		printf("%lld\n",m*k%mod);
		return;
	}
	long long s=0;
	for(int i=1;i<=m;i++)
	{
		s+=(C(n-i-1,m-i)*i%mod)*dijah(dijah(2,mod-2),n-i)%mod;
		s%=mod;
	}
	printf("%lld\n",(s*k)%mod);
	return;
} 
int main()
{
	long long qwe;
	f[0]=1;
	for(int i=1;i<=1000000;i++)f[i]=f[i-1]*i%mod;
	scanf("%lld",&qwe);
	for(int i=1;i<=qwe;i++)qer();








  return 0;
}

标签:10,CF1628D2,le,题解,Alice,long,number,Game,Bob
From: https://www.cnblogs.com/dijah/p/17838067.html

相关文章

  • A2OJ Ladder 32 简要题解
    https://earthshakira.github.io/a2oj-clientside/server/Ladder32.html只记录Difficultylevel>=8的。有很多题是口胡的。写了的会标注提交记录。还有些很久以前写过的题就懒得搬提交记录了。任何的*都表示该段的后续部分未能想出并查看了题解。159.CF372DChoosingSub......
  • 【题解 ABC180F】 Unbranched
    [ABC180F]Unbranched题面翻译求\(N\)个点,\(M\)条边且满足以下条件的图的数量:图中无自环;每个点度数最多为\(2\);连通块大小的最大值恰好为\(L\)。答案对\(10^9+7\)取模。\(2\leN\le300\),\(1\leM,L\leN\)。题目描述頂点にラベルが付き辺にはラベルが付い......
  • 题解 P7972【[KSN2021] Self Permutation】
    怎么其他两篇题解都是\(O(n\logn)\)的,来发一个\(O(n)\)做法,当考前复习了。对原序列建出小根笛卡尔树,节点编号与原序列中的下标相同。记\(T_u\)表示以\(u\)为根的子树,\(lc(u),rc(u)\)分别表示\(u\)的左儿子和右儿子。设\(f_u\)表示删除若干\(T_u\)中的点(可以不删......
  • [ARC106F] Figures 题解
    题意给定\(N\)个带有若干洞的节点,其中第\(i\)个点上有\(d_i\)个洞。先可以在两个不同的节点的洞之间连边,一个洞最多连一条边,求使得最终形成的图是一棵树的方案数,对\(998244353\)取模。洞之间相互区分,两个方案不同当且仅当存在一条边在两个方案中的连的洞不同。\(2\l......
  • P9400 题解
    blog。很naive的题,写这篇题解,主要是现有题解都用的线段树/平衡树,让我感到很难绷。一眼DP。\(dp_{i,j}\)表示前\(i\)个宿舍,现在有连续\(j\)个灯亮大于\(B\),方案数。\(dp_{i,0}=\max(\min(B,r_i)-l_i+1,0)\cdot\sum\limits_{j=0}^{A-1}dp_{i-1,j}\)。\(dp_{i......
  • CF8E 题解
    blog。抽象意义上单杀了。首先第一位必定为\(0\),然后取反串就不用去考虑了。\(n\le50\),考虑爆搜。搜整个串的前一半(设半长为\(M=\left\lfloor\dfracn2\right\rfloor\),前一半的串在十进制下值为\(v\)),后半段的数量可以计算:整个串最后一位是\(0\),只需满足逆序串。\(n\)为......
  • P7701 [CCC2014] 提前交卷 题解
    目录DescriptionSolutionCodeDescription在一个教室里有\(n\)排座位,每排有\(6\)个,从左至右标号分别为ABCDEF,其中C和D中有过道,通往教室前端和后端的两个房间,每个房间最开始没有人,每个座位上开始都有人。有\(m\)个不同的学生会依次提前交卷,先从这一排走到过道上,在从......
  • 鼠标拖拽拖动盒子时,与盒子内某些点击事件冲突问题解决
    问题:拖动时会触发圆球的点击事件解决鼠标拖动盒子时,将moving设为true意为正在拖动盒子,此时将class="move"遮挡容器展示在悬浮球上层,以覆盖悬浮球,此时也就不存在触发悬浮球点击事件的冲突了;鼠标拖动完盒子弹起时再将 moving设为false意为不在拖动盒子(遮挡容器class=......
  • C++调用Python3实战,和PyImport_ImportModule返回NULL问题解决
    LinuxC++调用Python3入门准备以下面的目录结构演示如何在LinuxC/C++调用python3。|--hello.py|--main.cpp|--CMakeLists.txt hello.py:python的脚本,里面有2个函数main.cpp:c++函数CMakeLists.txt:Cmake文件,生成makefilepython脚本示例python脚本hello.py内容如下,......
  • 赛前集训11天题解大总
    Day1kitty核心思路:将转移过程中的方案加入转移矩阵,边转移边累加stringdp设计:\(f[i][x][y]\)表示长度为\(i\),第一段以\(x\)结尾,且\(x\leqslantp\),第二段以\(p\)开头,以\(y\)结尾的两段完全相同的序列的对数。对于每个\(p\)答案就是\(\sum_{i=1}^{n}i\cdotf[i][x......