写在前面:
这一类dp特别要求不重不漏。统计对象的可能情况一般比较多,通常需要精确的划分和整体性计算。因此,使用动态规划抽象出问题中的子结构和推导的阶段,将有助于我们准确而高效的进行求解。
在求解计数类动态规划的问题时,通常要找到一个基准点,围绕这个基准点构造一个不可划分的整体,以避免子问题之间的重复。
一、Gerald and Giant Chess CodeForces - 559C:
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we’ll just say that the game takes place on an h × w field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win?
The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.
Input
The first line of the input contains three integers: h, w, n — the sides of the board and the number of black cells (1 ≤ h, w ≤ 105, 1 ≤ n ≤ 2000).
Next n lines contain the description of black cells. The i-th of these lines contains numbers ri, ci (1 ≤ ri ≤ h, 1 ≤ ci ≤ w) — the number of the row and column of the i-th cell.
It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.
Output
Print a single line — the remainder of the number of ways to move Gerald’s pawn from the upper left to the lower right corner modulo 109 + 7.
Examples
Input
3 4 2
2 2
2 3
Output
2
Input
100 100 3
15 16
16 15
99 88
Output
545732279
题意:有一个h * w的棋盘,棋盘上有n个格子是黑色的,保证左上角和右下角是白色的。其他格子都是白色的,从左上角开始,可以向右走也可以向下走,但是不能走到黑色格子里面,求从左上角到右下角有多少种走法。
以看这题的数据量就知道关键点在黑色格子上。
我们设 最右下角的格子为第N+1个黑色格子。
设F(i) 为到达第 i 个黑色格子,并且途中不经过其他黑色格子的路线数,那么我们只需要求出 F(N+1)即可。
我们先把黑色格子按照行列的坐标递增排序。
则 F( i )= C(xi - 1 + yi - 1 ,xi - 1) - sum of F(j) * C(xi - xj + yi - yj , xi - xj)
其中 1 <= j <i ,xi >= xj,yi >=yj
上式表示,从 左上角到第 i 个黑色格子的总路径数,减去第一个经过的不是第 i 个黑色格子的路径数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=200100;
struct node
{
int x,y;
}a[maxn];
ll fac[maxn],inv_fac[maxn];
ll dp[maxn];
bool cmp(const node &a,const node &b)
{
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;
}
ll mypow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void get_fac(void)
{
int n=200000;
fac[0]=1;
for(int i=1;i<=n;i++)
fac[i]=fac[i-1]*i%mod;
inv_fac[n]=mypow(fac[n],mod-2);
for(int i=n-1;i>=0;i--)
inv_fac[i]=inv_fac[i+1]*(i+1)%mod;
}
ll C(int n,int m)
{
return fac[n]*inv_fac[m]%mod*inv_fac[n-m]%mod;
}
int main(void)
{
int h,w,n;
get_fac();
scanf("%d%d%d",&h,&w,&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
}
sort(a+1,a+n+1,cmp);
a[n+1].x=h,a[n+1].y=w;
n++;
for(int i=1;i<=n;i++)
{
dp[i]=C(a[i].x-1+a[i].y-1,a[i].x-1);
for(int j=1;j<i;j++)
{
if(a[i].x>=a[j].x&&a[i].y>=a[j].y)
dp[i]-=dp[j]*C(a[i].x-a[j].x+a[i].y-a[j].y,a[i].x-a[j].x)%mod,dp[i]%=mod;
}
}
printf("%lld\n",(dp[n]%mod+mod)%mod);
return 0;
}
二、Connected Graph POJ - 1737:
An undirected graph is a set V of vertices and a set of E∈{V*V} edges.An undirected graph is connected if and only if for every pair (u,v) of vertices,u is reachable from v.
You are to write a program that tries to calculate the number of different connected undirected graph with n vertices.
For example,there are 4 different connected undirected graphs with 3 vertices.
Input
The input contains several test cases. Each test case contains an integer n, denoting the number of vertices. You may assume that 1<=n<=50. The last test case is followed by one zero.
Output
For each test case output the answer on a single line.
Sample Input
1
2
3
4
0
Sample Output
1
1
4
38
求N个节点的无向连通图有多少个。
连通图不容易进行划分,而不连通图比较容易划分,我们用全部有可能的图的数量,减去不连通的就是连通的。因为一张不连通图一定是由若干个连通块组成的,我们就可以枚举标号为1的那个节点所在的连通块的连通图的数量。
F( 1 ) = 1,目标 F(N)。
额。。。这题竟然没有要求取余。。。。
等学了java大数再回来补上吧。。。
三、A decorative fence POJ - 1037:
Richard just finished building his new house. Now the only thing the house misses is a cute little wooden fence. He had no idea how to make a wooden fence, so he decided to order one. Somehow he got his hands on the ACME Fence Catalogue 2002, the ultimate resource on cute little wooden fences. After reading its preface he already knew, what makes a little wooden fence cute.
A wooden fence consists of N wooden planks, placed vertically in a row next to each other. A fence looks cute if and only if the following conditions are met:
�The planks have different lengths, namely 1, 2, . . . , N plank length units.
�Each plank with two neighbors is either larger than each of its neighbors or smaller than each of them. (Note that this makes the top of the fence alternately rise and fall.)
It follows, that we may uniquely describe each cute fence with N planks as a permutation a1, . . . , aN of the numbers 1, . . . ,N such that (any i; 1 < i < N) (ai − ai−1)*(ai − ai+1) > 0 and vice versa, each such permutation describes a cute fence.
It is obvious, that there are many dierent cute wooden fences made of N planks. To bring some order into their catalogue, the sales manager of ACME decided to order them in the following way: Fence A (represented by the permutation a1, . . . , aN) is in the catalogue before fence B (represented by b1, . . . , bN) if and only if there exists such i, that (any j < i) aj = bj and (ai < bi). (Also to decide, which of the two fences is earlier in the catalogue, take their corresponding permutations, find the first place on which they differ and compare the values on this place.) All the cute fences with N planks are numbered (starting from 1) in the order they appear in the catalogue. This number is called their catalogue number.
After carefully examining all the cute little wooden fences, Richard decided to order some of them. For each of them he noted the number of its planks and its catalogue number. Later, as he met his friends, he wanted to show them the fences he ordered, but he lost the catalogue somewhere. The only thing he has got are his notes. Please help him find out, how will his fences look like.
Input
The first line of the input file contains the number K (1 <= K <= 100) of input data sets. K lines follow, each of them describes one input data set.
Each of the following K lines contains two integers N and C (1 <= N <= 20), separated by a space. N is the number of planks in the fence, C is the catalogue number of the fence.
You may assume, that the total number of cute little wooden fences with 20 planks fits into a 64-bit signed integer variable (long long in C/C++, int64 in FreePascal). You may also assume that the input is correct, in particular that C is at least 1 and it doesn抰 exceed the number of cute fences with N planks.
Output
For each input data set output one line, describing the C-th fence with N planks in the catalogue. More precisely, if the fence is described by the permutation a1, . . . , aN, then the corresponding line of the output file should contain the numbers ai (in the correct order), separated by single spaces.
Sample Input
2
2 1
3 3
Sample Output
1 2
2 3 1
题意:N块长方形木板,长度为1—N,宽度为1,现在要拿它们组成一个长度为N的栅栏,要求某一块木板要不就比两侧的高,要不就比两侧的低。会有很多序列,把他们按照字典序排序。输出排名为C的序列。
F(i,j,k) 表示用 i 块长度不同的木板构成栅栏,其中最左边的木板长度从小到大排在第 j 位,并且状况为 k 的方案总数 (k=0,表示处于低位,k=1,表示处于高位 )。
状态转移方程就很显然了。
找排名为C的的时候,从第一位依次往后确定就好了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#define ll long long
#define llu unsigned ll
using namespace std;
const int inf=0x3f3f3f3f;
const ll lnf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=200100;
ll k,n,c;
ll dp[21][21][2];
//dp[i][j][k]:用了i个长度不同的木板,其中最左边
//的长度从小到大排在第j位,并且状态为k时的方案总数。
bool ha[21];
void init(void)
{
dp[1][1][0]=dp[1][1][1]=1;
for(int i=2;i<=20;i++)
{
for(int j=1;j<=i;j++)
{
for(int k=j;k<i;k++)
dp[i][j][0]+=dp[i-1][k][1];//在前面加了个小的
for(int k=1;k<j;k++)
dp[i][j][1]+=dp[i-1][k][0];//在前面加了个大的
}
}
}
int main(void)
{
init();
scanf("%lld",&k);
while(k--)
{
scanf("%lld%lld",&n,&c);
memset(ha,0,sizeof(ha));
int last=0,nowk=1;
for(int i=1;i<=n;i++)
{
if(dp[n][i][1]>=c)
{
last=i,nowk=1;
ha[i]=true;
break;
}
c-=dp[n][i][1];
if(dp[n][i][0]>=c)
{
last=i,nowk=0;
ha[i]=true;
break;
}
c-=dp[n][i][0];
}
printf("%d",last);
for(int i=2;i<=n;i++)
{
nowk^=1;
int j=0;
for(int len=1;len<=n;len++)
{
if(ha[len]) continue;
j++;
if(nowk==0&&len<last||nowk==1&&len>last)
{
if(dp[n-i+1][j][nowk]>=c)
{
last=len;
ha[len]=true;
printf(" %d",len);
break;
}
c-=dp[n-i+1][j][nowk];
}
}
}
putchar('\n');
}
return 0;
}