Game on Sum--组合数学--DP
\(Codeforces-Round 767\) (Div. 2) F2. \(Game \ on \ Sum\)
题意
\(QZS\) 和 \(HANGRY\) 玩游戏。
游戏共有 \(n\) 轮, 对于整一局游戏给定一个 \(K\) , 游戏的过程是在改变一个数 \(a\) 的值 , 初始为 \(1\) .
在每一轮中 \(QZS\) 会在 \([1 , k]\) 的区间里取出一个 \(\bf{实数}\) , \(HANGRY\) 选择用 \(a\) 加上这个实数还是减去这个实数 , \(HANGRY\) 的目的是让 \(a\) 最小 , \(QZS\) 相反.
在这 \(n\) 轮中 , \(HANGRY\) 至少选择 \(m\) 轮选择加 .
\(HANGRY\) 和 \(QZS\) 都是绝顶聪明的 , 这意味着他们会选择最优解 .
问最后的 \(a\) 是多少 . 多测.答案对 \(1e9+7\) 取模.
数据范围:
$n \le 10^6 , m \le n , T \le 5 \times 10^5 \sum n = 10^6 $
\(\sum n\) 为每个测试点所有测试数据中 \(n\) 的和.
题解
\(O(nm)\) (<100 pts)
考虑 \(DP\)
\(dp_{i,j}\) 表示长度为 \(i\) , 选了 \(j\) 个减。
则方程为:
\]
显然
code
114514
#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N = 2e3 + 10 ;
const int mod = 1e9 + 7 ;
int dp[N][N] , n , T , m , K ;
int nueyong ;
inline int read() {
int x = 0 , f = 1 ;
char c = getchar() ;
while ( c < '0' || c > '9' ) {
if ( c == '-' ) f = -f ;
c = getchar() ;
}
while ( c >= '0' && c <= '9' ) {
x = x * 10 + c - '0' ;
c = getchar() ;
}
return x * f ;
}
inline int Regular_Quick_Pow( int a , int b ) {
int ans = 1 ;
while ( b > 0 ) {
if ( b & 1 ) ans = ( ans * a ) % mod ;
b >>= 1 ; a = ( a * a ) % mod ;
}
return ans ;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen( "1.in" , "r" , stdin ) ;
freopen( "1.out" , "w" , stdout ) ;
#endif
T = read() ;
nueyong = Regular_Quick_Pow( 2 , mod - 2 ) ;
while ( T -- ) {
n = read() , m = read() , K = read() ;
dp[1][0] = K , dp[1][1] = 0 ;
for ( int i = 2 ; i <= n ; ++ i ) {
dp[i][0] = ( i * K ) % mod ;
for( int j = 1 ; j <= min(i - 1 , n - m) ; ++ j ) {
dp[i][j] = ( ( ( dp[i - 1][j - 1] + dp[i - 1][j] ) % mod ) * nueyong ) % mod ;
}
}
cout << dp[n][n - m] << '\n' ;
}
}
组合方法
我们目前把这个东西写出来
1 0 0 0 0 0 0 0 0 0 0
4 1 0 0 0 0 0 0 0 0 0
12 5 1 0 0 0 0 0 0 0 0
32 17 6 1 0 0 0 0 0 0 0
80 49 23 7 1 0 0 0 0 0 0
192 129 72 30 8 1 0 0 0 0 0
448 321 201 102 38 9 1 0 0 0 0
1024 769 522 303 140 47 10 1 0 0 0
2304 1793 1291 825 443 187 57 11 1 0 0
5120 4097 3084 2116 1268 630 244 68 12 1 0
对于点 \(i , j\) 来说, 答案是 \(\frac{dp_{i , j}}{2^{i - 1}} \times K\)
我们考虑点 \((i , j)\) , 使他用第 \(0\) 列表示出来,我们摆出来一个杨辉三角,来看看:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
我们能够发现,将 \(i , j\) 看做 \(0 , 0\) 他下面的选的次数可以用杨辉三角的某个点表示。
并且为(点设为 \((n , n - m)\) , 与上面我们的dp做法衔接一下 ):
\]
注意两个 -1 , 关于这个位置为啥要减一,这里解释一下:
对于当已经到 \(0\) 时,在杨辉三角中的位置依然可能有上一位转过来,但是两行的首位是没有关系的,那么他只能够由他的左上方的点转移过来,就相当于左上方的值了。
code
114514
#include <bits/stdc++.h>
#define int long long
using namespace std ;
const int N = 2e6 + 10 ;
const int mod = 1e9 + 7 ;
int n , T , m , K ;
int now[N] ;
inline int read() {
int x = 0 , f = 1 ;
char c = getchar() ;
while ( c < '0' || c > '9' ) {
if ( c == '-' ) f = -f ;
c = getchar() ;
}
while ( c >= '0' && c <= '9' ) {
x = x * 10 + c - '0' ;
c = getchar() ;
}
return x * f ;
}
namespace Combination {
int D[N] ; int nueyong[N] , sum_neo[N] , sum[N] ;
inline void lear_neoyong() {
sum_neo[0] = sum_neo[1] = 1 ;
nueyong[1] = 1 ; nueyong[0] = 1 ;
sum[0] = sum[1] = 1 ;
for( int i = 2 ; i < N ; ++ i ) {
int p = mod ;
int k = p / i ;
nueyong[i] = ( k * ( p - nueyong[p % i] ) ) % p ;
sum_neo[i] = ( nueyong[i] * sum_neo[i - 1] ) % p ;
sum[i] = ( i * sum[i - 1] ) % p ;
}
}
int Quick_Pow( int alpha , int beta )
{
int ans = 1 ;
while ( beta > 0 ) {
if( beta & 1 ) ans = ( ans * alpha ) % mod ;
beta >>= 1 ; alpha = ( alpha * alpha ) % mod ;
}
return ans ;
}
int Regular_C_of_Pow_Class( int n , int m ) {
int alpha = 1 , beta = 1 , rereturn = 0 ;
if( m <= n && n >= 0 && m >= 0 ) {
for( int i = n - m + 1 ; i <= n ; ++ i ) {
alpha = ( alpha * i ) % mod ;
}
for( int i = 1 ; i <= m ; ++ i ) {
beta = ( beta * i ) % mod ;
}
rereturn = ( alpha * Quick_Pow( beta , mod - 2 ) ) % mod ;
return rereturn ;
}
else return 0 ;
}
inline int jc( int x ) {
return sum[x] ;
}
inline int neo_jc( int x ) {
if ( x == 0 ) return 1 ;
return sum_neo[x] ;
}
int Regular_C_of_Inv( int n , int m ) {
return ( ( ( jc( n ) * neo_jc( n - m ) ) % mod ) * neo_jc( m ) ) % mod ;
}
int C_Lucas_Using_Inv( int n , int m ) {
if ( m > n ) return 0 ;
if ( m == 0 ) return 1 ;
return ( Regular_C_of_Inv( n % mod , m % mod ) * C_Lucas_Using_Inv( n / mod , m / mod ) ) % mod ;
}
int C_Lucas_Using_Pow( int n , int m ) {
if( m == 0 ) return 1 ;
return ( Regular_C_of_Pow_Class( n % mod , m % mod ) * C_Lucas_Using_Pow( n / mod , m / mod ) ) % mod ;
}
void Asking_for_Derangement() {
D[0] = 1 ;
D[1] = 0 ;
D[2] = 1 ;
for( int i = 3 ; i < N ; ++ i ) {
D[i] = ( i - 1 ) * ( D[i - 1] + D[i - 2] ) % mod ;
}
}
inline void Cleared() {
memset( D , 0 , sizeof(D) ) ;
memset( sum_neo , 0 , sizeof(sum_neo) ) ;
memset( sum , 0 , sizeof(sum) ) ;
memset( nueyong , 0 , sizeof(nueyong) ) ;
}
} ;
using namespace Combination ;
inline int Regular_Quick_Pow( int a , int b ) {
int ans = 1 ;
while ( b > 0 ) {
if ( b & 1 ) ans = ( ans * a ) % mod ;
b >>= 1 ; a = ( a * a ) % mod ;
}
return ans ;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen( "1.in" , "r" , stdin ) ;
freopen( "1.out" , "w" , stdout ) ;
#endif
T = read() ;
lear_neoyong() ;
for ( int i = 1 ; i <= 1e6 * 2 ; ++ i ) {
now[i] = ( i * Regular_Quick_Pow( 2 , i - 1 ) ) % mod ;
}
while ( T -- ) {
n = read() , m = read() , K = read() ;
if ( n - m == 0 ) {
cout << ( n * K ) % mod << '\n' ;
continue ;
}
int ans = 0 ;
for ( int i = 1 ; i <= n - 1 ; ++ i ) {
ans = ( ans + C_Lucas_Using_Inv( n - i - 1 , n - m - 1 ) * now[i] ) % mod ;
}
cout << ( ( ( K * ans ) % mod ) * Regular_Quick_Pow( Regular_Quick_Pow( 2 , n - 1 ) , mod - 2 ) ) % mod << '\n' ;
}
}
结尾撒花 \(\color{pink}{✿✿ヽ(°▽°)ノ✿}\)
Game on Sum--组合数学--DP的更多相关文章
- 【uoj#22】[UR #1]外星人 组合数学+dp
题目描述 给你一个长度为 $n$ 的序列 $\{a_i\}$ 和一个数 $x$ ,对于任意一个 $1\sim n$ 的排列 $\{p_i\}$ ,从 $1$ 到 $n$ 依次执行 $x=x\ \tex ...
- 【bzoj1925】[Sdoi2010]地精部落 组合数学+dp
题目描述 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中Hi是1到 ...
- UVA 10891 Game of Sum(DP)
This is a two player game. Initially there are n integer numbers in an array and players A and B get ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- UVALive 7143 Room Assignment(组合数学+DP)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- HDU-4532 湫秋系列故事——安排座位 组合数学DP
题意:有来自n个专业的学生,每个专业分别有ai个同学,现在要将这些学生排成一行,使得相邻的两个学生来自不同的专业,问有多少种不同的安排方案. 分析:首先将所有专业的学生视作一样的,最后再乘以各自学生的 ...
- hdu 1003 Max sum(简单DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
- UVA - 10891 Game of Sum 区间DP
题目连接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19461 Game of sum Description This ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
随机推荐
- 详解Web应用安全系列(4)失效的访问控制
在Web安全中,失效的访问控制(也称为权限控制失效或越权访问)是指用户在不具备相应权限的情况下访问了受限制的资源或执行了不允许的操作.这通常是由于Web应用系统未能建立合理的权限控制机制,或者权限控制 ...
- 学习嵌入式为什么要学习uboot
ref:http://www.elecfans.com/d/617674.html 为什么要有BootLoader 背景 很多人学习嵌入式一开始就搞Linux,这样子容易对底层缺少了解. 基础介绍 计 ...
- VulnHub_DC-5渗透流程
DC-5 主机探测 nmap 192.168.157.0/24 访问网站 探测网站得知是Nginx 目录扫描 在Contact处可以提交东西,抓个包,发现在thankyou.php界面时间年份会变,应 ...
- SpringBoot集成Knife4j
Knife4j简介 Knife4j 官网地址:https://doc.xiaominfo.com/ knife4j 是为Java MVC框架集成Swagger生成Api文档的增强解决方案. Knife ...
- SpringBoot结合easyexcel处理Excel文件
文/朱季谦 假如有这样一个需求,每天需要读取以下表头的Excel文件,统计文件里击中黑名单的比例,该文件is_blacklist列的1表示击中了黑名单,0表示未击中黑名单. 基于该需求,可以在定时任务 ...
- PAT-1003 我要通过! (20分) JavaScript(node)
"答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确&q ...
- oeasy教您玩转vim - 90 - # 语法定义syntax
内容查找 grep 回忆 我们这次研究了一下配色方案 murphy虽然配色好看 但是对于java的支持并不好 我们对于murphy进行了修改 增加了String.StorageClass颜色的定义 ...
- vue项目 回到顶部功能 定位在头部
'backBox'是外层容器类名, 根据传入的index,定位在不同的位置 组件: <template> <div class="toTop" @click=&q ...
- docker 安装 centos8 mysql8 java tomcat
docker 安装 centos8 mysql8 java tomcat 一,首先在window10系统安装docker,这里就不再描述了. 二,启动docker下载安装centos8镜像 注意: ...
- Jmeter函数助手8-counter
counter函数用于线程计数,类似计数器. TRUE每个用户有自己的计数器:FALSE使用全局计数器:即线程之间是否需要共享累加计数器,TRUE否,FALSE是 存储结果的变量名(可选) 1.线程之 ...