本文已参与「新人创作礼」活动,一起开启掘金创作之路。
Problem Description
Madoka decided to entrust the organization of a major computer game tournament "OSU"!
In this tournament, matches are held according to the "Olympic system". In other words, there are 2n2n participants in the tournament, numbered with integers from 11 to 2n2n. There are nn rounds in total in the tournament. In the ii-th round there are 2n−i2n−i matches between two players (one of whom is right, the other is left), after which the winners go further along the tournament grid, and the losing participants are eliminated from the tournament. Herewith, the relative order in the next round does not change. And the winner of the tournament — is the last remaining participant.
But the smaller the participant's number, the more he will pay Madoka if he wins, so Madoka wants the participant with the lowest number to win. To do this, she can arrange the participants in the first round as she likes, and also determine for each match who will win — the participant on the left or right.
But Madoka knows that tournament sponsors can change the winner in matches no more than kk times. (That is, if the participant on the left won before the change, then the participant on the right will win after the change, and if the participant on the right won, then the participant on the left will win after the change).
Print the minimum possible number of the winner in the tournament, which Madoka can get regardless of changes in sponsors. But since the answer can be very large, output it modulo 109+7109+7. Note that we need to minimize the answer, and only then take it modulo.
Input
The first and the only line contains two integers nn and kk (1≤n≤105,1≤k≤min(2n−1,109)1≤n≤105,1≤k≤min(2n−1,109)) — the number of rounds in the tournament and the number of outcomes that sponsors can change.
Output
Print exactly one integer — the minimum number of the winner modulo 109+7109+7
Examples
input
1 1 output
2 input
2 1 output
3 input
3 2 output
7 Note
In the first example, there is only one match between players 11 and 22, so the sponsors can always make player 22 wins.
The tournament grid from the second example is shown in the picture in the statement.
题意:
有2^n个参赛选手,每两人之间进行比赛,胜者之间再次比赛,这样n轮后会出现一个最终胜者,现在你可以任意安排第一轮的比赛顺序,并且对于每次比赛你都可以选择哪方获胜,最终目标是让最后获胜者的编号尽量小,不过另一个人有至多k次干扰你的机会,每次干扰可以指定一场比赛,使其结果与你预期相反,问最终即使受到干扰下获胜者的最小编号。
分析:
首先可以对于任何一个你的方案,都可以转换成左边选手一定获胜的方案,只需要左右子树交换一下位置即可,这样接下来表述会比较清楚。此时比赛匹配信息构成的一棵树就由最下面一层决定了,当干扰机会为0时显然最小的获胜者就是1,当干扰机会为1时,有效的干扰就是从这n层边中选择出一层进行改变,所以共有C(n, 1)种干扰方案,由于这棵树叶子结点各不相同,所以这C(n, 1)种干扰方案产生的获胜者也各不相同,由于干扰者希望让获胜者编号更大,所以他会选择获胜者编号最大的那个干扰方案,而你希望获胜者编号尽量小,所以你会安排最下面一层顺序使其C(n, 1)种干扰方案得到的获胜者分别为2, 3, 4, ......, n+1,这样才是最优的策略,所以干扰机会为1时答案就是n+1,当干扰机会为2时也是类似的,可以有C(n, 2)种有效的干扰方案,所以你会安排树的结构使获胜者为2, 3, 4, ......, n*(n-1)/2+1,答案就是n*(n-1)/2+1,以此类推就能得出本题解法。
具体代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#define int long long
using namespace std;
const int mod = 1e9+7;
int jie[200005];
int ksm(int base, int power){
int ans = 1;
while(power){
if(power&1) ans = ans*base%mod;
base = base*base%mod;
power >>= 1;
}
return ans;
}
int C(int n, int m){
return jie[n]*ksm(jie[n-m]*jie[m]%mod, mod-2)%mod;
}
signed main()
{
int n, k;
cin >> n >> k;
jie[0] = 1;
for(int i = 1; i <= n; i++)
jie[i] = jie[i-1]*i%mod;
int ans = 1;
for(int i = 1; i <= min(k, n); i++)
ans = (ans+C(n, i))%mod;
cout << ans;
return 0;
}