﻿ AT2301 Solitaire - HS的博客
HS
AFO
AT2301 Solitaire

# 题目链接

AtCoder Grand Contest 024 E

# 题目描述

Snuke has decided to play with $N$ cards and a deque (that is, a double-ended queue). Each card shows an integer from $1$ through $N$ , and the deque is initially empty. Snuke will insert the cards at the beginning or the end of the deque one at a time, in order from $1$ to $N$ . Then, he will perform the following action $N$ times: take out the card from the beginning or the end of the deque and eat it. Afterwards, we will construct an integer sequence by arranging the integers written on the eaten cards, in the order they are eaten. Among the sequences that can be obtained in this way, find the number of the sequences such that the $K$ -th element is $1$ . Print the answer modulo $10^{9}\ +\ 7$ .

# 输入格式

The input is given from Standard Input in the following format:
N K

# 输出格式

Print the answer modulo $10^9 \ + \ 7$ .

# 输入输出样例

## 输入 #1

2 1


## 输出 #1

1


## 输入 #2

17 2


## 输出 #2

262144


## 输入 #3

2000 1000


## 输出 #3

674286644


## 说明/提示

$1\ \le \ K\ \le \ N\ \le \ 2000$

# 分析

$$f_{i,j}=\sum_{k=j+1}^{n}f_{i-1,k}$$

$$f_{i,j}=f_{i-1,j}$$

# 一些对我而言的坑

• $n-K-1$可能$<0$。

# 代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 2010
const int p=1e9+7;
{
register T sum=0;
register char cc=getchar();
int sym=1;
while(cc!='-'&&(cc>'9'||cc<'0'))cc=getchar();
if(cc=='-')sym=-1,cc=getchar();
sum=sum*10+cc-'0';
cc=getchar();
while(cc>='0'&&cc<='9')sum=sum*10+cc-'0',cc=getchar();
return sym*sum;
}
template <typename T>inline T read(T &a)
{
return a;
}
template <typename T,typename... Others> inline void read(T& a, Others&... b)
{
}
int n,K,f[maxn];
{
x+=y;
if(x>=p)
x-=p;
}
int mul(int x,int y)
{
return 1ll*x*y%p;
}
int fpow(int x,int y)
{
if(y<0)return 1;
int res=1;
while(y)
{
if(y&1)
res=mul(res,x);
x=mul(x,x);
y>>=1;
}
return res;
}
int main()
{
f[n+1]=1;
for(int i=1;i<=K;i++)
{
for(int j=n;j>=1;j--)
{
if(n-j+1>=i)
else
f[j]=0;
}
}
printf("%d\n",mul(f[1],fpow(2,n-K-1)));
return 0;
}



HS的博客

AT2301 Solitaire

2020-01-10