[Noip2018] fill in the number game

Portal

Description

Familiar, do not say

Solution

Will not push for a formula of konjac, how in the examination room gracefully through this problem

  1. Play Sample hand, found to be \ (n-=. 1 \) , \ (ANS = 2 ^ m \) . For \ (n-2 = \) , \ (. 4 ANS = \ Times. 1. 3-m} ^ {\) . Or simply play \ (n, m \ le 3 \) Table

  2. Visual inspection, seems to have found \ (f (the n-, m + 1) = 3F (the n-, m) \) , but this is not correct, but only if you think so, you still can get a lot of points

  3. I think the conclusion is particularly evident in particular that:

  • \ (F (n-, m) = F (m, n-) \) , so as far as the \ (n \ le m \) case

  • Since each step to the right or down, it is possible to follow the steps put into a slash FIG from the lower left to upper right, to be considered for a single grid, the number found on each oblique monotonically increasing (So, you can slash by enumerating each location at which begins to become \ (0 \) on it, simply hit the table)

  • In order to verify the correctness of the map, we also need to explore the nature of the legal number of ways to fill:

    How to consider not unlawfully, there are two paths, they are not legally in a position, then they have the same path before the corresponding string 01, and the same position, in this step illegal, large walk path \ (0 \) , a small path to go \ (1 \) .

    This inspired us to chart a legitimate, if at some point, there are two paths to reach its 01 correspond to the same string, then its successor the same, so we make a point called \ (A \) points, one point is \ (a \) point if and only if there are several of its precursors in the point a or its precursor is the same

  1. With so many properties, we find out that can play table to get a lot of points, so the search began happily, according to a diagonal strip search, the search side again to update the current point figure \ (A \) Point class, Meanwhile, only slash on each \ (0 \) and \ (1 \) junction may result in legitimate, the following is determined whether a point is on its \ (a \) class can be a point
  2. Hit the table, found conclusions \ (F (n-,. 1 + m) = 3F (n-, m), m> n-\) ! So very happy over
  3. This search is really fast, limit data \ (n = 8, m = 9 \) can be in the \ (\ 0.6s) the past, so even too lazy to hit the table, direct violence on the line


Code 

#include<bits/stdc++.h>
#define ll long long
#define db double
#define reg register
using namespace std;
#define dbg1(x) cerr<<#x<<'='<<(x)<<' '
#define dbg2(x) cerr<<#x<<'='<<(x)<<'\n'
#define dbg3(x) cerr<<#x<<'\n'
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
const int P=1e9+7;
int Add(int x,int y){return (x+y)%P;}
int Mul(int x,int y){return (1ll*x*y)%P;}
int fp(int x,int y){int r=1;if(y>0)for(;y;y>>=1,x=Mul(x,x))if(y&1)r=Mul(r,x);return r;}
int n,m;ll ans=0;
int Nm[20],X[20][20],Y[20][20];
bool mk[10][10],mp[10][10];
inline void getmk(int now)
{
    int i,j;
    for(i=X[now][1],j=Y[now][1];i&&j<=m;--i,++j)
        if(i>1&&j>1)mk[i][j]=(mk[i-1][j]|mk[i][j-1]|(mp[i][j-1]==mp[i-1][j]));
}
void dfs(int now)
{
    int i,j,p=Nm[now];getmk(now-1);
    for(i=0;i<=p;++i)
    {
        if(i)mp[X[now][i]][Y[now][i]]=true;
        if((i==0||i==p)||(i>0&&i<p&&!mk[X[now][i]-1][Y[now][i]]))
            if(now+1==n+m)++ans;else dfs(now+1);
    }
    for(i=1;i<=p;++i)mp[X[now][i]][Y[now][i]]=0;
}
int main()
{
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    n=read();m=read();
    if(n>m)swap(n,m);
    if(n==1)return 0*printf("%d\n",fp(2,m));
    int c=max(0,min(m-n,m-n-1));m-=c;
    reg int i,j;
    for(i=1;i<=n;++i)Nm[i]=i;
    for(i=n+1;i<m;++i)Nm[i]=n;
    for(i=m;i<n+m;++i)Nm[i]=n+m-i;
    for(i=1;i<=n;++i)X[i][1]=i,Y[i][1]=1;
    for(i=n+1;i<n+m;++i)X[i][1]=n,Y[i][1]=i-n+1;
    for(i=1;i<n+m;++i)for(j=2;j<=Nm[i];++j)X[i][j]=X[i][j-1]-1,Y[i][j]=Y[i][j-1]+1;
    dfs(1);printf("%lld\n",Mul(ans,fp(3,c)));
}



Blog from PaperCloud , without permission, please do not reprint, TKS!

Guess you like

Origin www.cnblogs.com/PaperCloud/p/11808887.html