P5023 fill in the number game

Title Description

Small D especially like to play games. On this day, he was playing a number of games to fill.

The game board is filled number n × m a rectangular form. Players need to fill a number (the number 0 or the number 1) in each grid of the table, need to meet certain restrictions number of refills.

Let's take a detailed description of these restrictions.

For convenience of description, we first give some definitions:

We use the row and column coordinates of each grid to represent a grid, that is (line coordinates, column coordinates). (Note: The row and column coordinates are numbered from 0)

Legal path P: one path is valid if and only if:

This path from the upper left corner of the grid (0,0) form a rectangle, the lower right corner of a rectangular grid (n-1, m-1) ends;

In this path, the current grid can only move to the right from its adjacent lattice, or from the current cell to move below its adjacent grid.

For example: In the following rectangle, only two paths are valid, they are P1: (0,0) → (0,1) → (1,1) and P2: (0,0) → (1, 0) → (1,1).

For a valid paths P, we can use a string w (P) is represented, for the length of the string n + m-2, which contains only the characters "R" or the character "D", the i-th character records the method of the moving path P of step i, "R" denotes the right moves to the current cell and its neighboring grid, "D" represents the current cell to move below its adjacent grid. For example, the figure above the path P1, there are w (P1) = "RD"; and for another path P2, there are w (P2) = "DR".

Meanwhile, after filled in each grid of each path P through legitimate digital sequentially connected, get a length of n + m-1 01 character string, referred to as s (P). For example, if we enter the number 0 on the grid (0,0) and (1,0) in the lattice (0,1) and (1,1) enter the number 1 (see FIG red numbers). Then the path P1, we can obtain s (P1) "011", the path P2, there is s (P2) = "001".

D game requires a small number to find a method of filling of 0,1, such that for the two paths P1, P2, if w (P1)> w (P2), must be s (P1) ≤s (P2). We say that the string is smaller than a string b, if and only if the string is a string lexicographically lexicographically less than b, the lexicographic definition see the first question. But just could not find a way to satisfy the curiosity of a small D, a small D also want to know how many games are played this game, that is, how many fill methods to meet the requirements of digital games?

D small limited capacity, hope you help him solve this problem, that is, how many ways can fill 0,1 meet the subject requirements. Since the answer may be large, the output you need answers to 10 ^ 9 + 7 modulo.

Input formats:

Total input file line, comprising two positive integers n, m separated by a space, the size of the rectangle representing. Wherein n represents the number of rows of the rectangular table, m represents the number of columns of a rectangular table.

Output formats:

Total output one line containing a positive integer indicating how many ways can fill 0,1 meet the requirements of the game. Note: The answer to the result output 10 ^ 7 + 9 modulo.

Sample input:

2 2

Sample output:

12

Sample input:

3 3

Sample output:

112

Sample input:

5 5

Sample output:

7136

Explanation

Sample [explain]

[Agreed] with the scale data

Ideas:

Solution to a problem Portal. . .

Code:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int Mod=1e9+7;

long long n,m;

long long ksm(long long a,long long b) {
    long long r=1;
    for(; b; a=a*a%Mod,b/=2)
        if(b&1)
            r=r*a%Mod;
    return r;
}

int main () {
    scanf("%lld%lld",&n,&m);
    if(n>m)
        swap(m,n);
    if(n==1)
        printf("%lld\n",ksm(2,m));
    else if(n==2)
        printf("%lld\n",4*ksm(3,m-1)%Mod);
    else if(n==3)
        printf("%lld\n",112*ksm(3,m-3)%Mod);
    else {
        if(m==n)
            printf("%lld\n",(83*ksm(8,n)%Mod+5*ksm(2,n+7)%Mod)*190104168%Mod);
        else
            printf("%lld\n",(83*ksm(8,n)%Mod+ksm(2,n+8))*ksm(3,m-n-1)%Mod*570312504%Mod);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11291401.html