P5023 [NOIP2018 提高组] 填数游戏

 

#include<cstdio>
#include<iostream>
#include<algorithm>
//#include<queue>
//#include<vector>
//#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=1e6+10 ;

int a[30][maxn],f[30][maxn];
ll ans;


void dfs(int x,int y,int n,int m)
{
    if(x>n){
        ans++;
        return;
    }
    int tx=x,ty=y+1;  
    if(ty>=m+1) tx+=1,ty=1;
    
    for(int i=0;i<=1;i++)
    {
        if(i<a[x-1][y+1]) continue;
        if(f[x-1][y]&&i!=a[x-1][y+1]&&x-1<=n&&x-1>=1&&y+1>=1&&y+1<=m) continue;
        a[x][y]=i,f[x][y]= a[x-1][y]==a[x][y-1]||f[x-1][y]||f[x][y-1];
        dfs(tx,ty,n,m);
    }
}
ll cal(int x,int y)
{
    ans=0;
    dfs(1,1,x,y);
    return ans;
}

int main()
{
    cin.tie(0);cout.tie(0); 
    ios::sync_with_stdio(false); //不能有 
    
    for(int n=1;n<=8;n++){
        for(int m=n;m<=n+4;m++){
        //    dfs(1,1,i,j); 
            
            for(int i=1;i<=n+1;i++) a[i][0]=10;
            for(int j=1;j<=m+1;j++) a[0][j]=-10;
            
            cout<<n<<"->"<<m<<"->"<<cal(n,m)<<'\n';
            
            for(int i=0;i<30;i++)
                for(int j=0;j<30;j++)
                    a[i][j]=f[i][j]=0;
        }
    }
    
    return 0;
}
View Code
#include<bits/stdc++.h>
#define int long long
const int qiyang=1e9+7;
using namespace std;
int a[]={0,0,0,0,912,7136,56768,453504,3626752};
int b[]={0,0,0,0,2688,21312,170112,1360128,10879488};
int power(int a,int b)
{
    int res=1;
    a%=qiyang;
    while(b)
    {
        if(b&1) (res*=a)%=qiyang;
        b>>=1,(a*=a)%=qiyang;
    }
    return res%qiyang;
}
int mod(int x){return (x%qiyang+qiyang)%qiyang;}
signed main()
{
    int n,m;
    scanf("%lld%lld",&n,&m);
    if(n>m) swap(n,m);
    if(n==1) printf("%lld\n",power(2,m));
    else if(n==2) printf("%lld\n",mod(4*power(3,m-1)));
    else if(n==3) printf("%lld\n",mod(112*power(3,m-3)));
    else 
    {
        if(n==m) printf("%lld\n",a[n]);
        else printf("%lld\n",mod(b[n]*power(3,m-n-1)));
    }
    return 0;
}
AC

 

posted @ 2023-08-22 19:10  JMXZ  阅读(8)  评论(0)    收藏  举报