zoukankan      html  css  js  c++  java
  • HDU

    Covering

     
    Bob's school has a big playground, boys and girls always play games here after school. 

    To protect boys and girls from getting hurt when playing happily on the playground, rich boy Bob decided to cover the playground using his carpets. 

    Meanwhile, Bob is a mean boy, so he acquired that his carpets can not overlap one cell twice or more. 

    He has infinite carpets with sizes of 1×21×2 and 2×12×1, and the size of the playground is 4×n4×n. 

    Can you tell Bob the total number of schemes where the carpets can cover the playground completely without overlapping? 

    InputThere are no more than 5000 test cases. 

    Each test case only contains one positive integer n in a line. 

    1n10181≤n≤1018 
    OutputFor each test cases, output the answer mod 1000000007 in a line. 
    Sample Input

    1
    2

    Sample Output

    1
    5


    题意:用1*2铺满4*n的地面。
    特别综合的一道题。求法是先用模拟暴搜找出初始几个n的情况,//1 5 11 36 95 281 781 2245 6336 18061
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #define MAX 1005
    #define INF 0x3f3f3f3f
    #define MOD 1000000007
    using namespace std;
    typedef long long ll;
    
    int b[5][MAX];
    int n;
    ll ans;
    
    void dfs(int s){
        int i,j;
        if(s==4*n){
            ans++;
            return;
        }
        for(i=1;i<=4;i++){
            for(j=1;j<=n;j++){
                if(b[i][j]==0){
                    if(i+1<=4){
                        if(b[i+1][j]==0){
                            b[i][j]=1;
                            b[i+1][j]=1;
                            dfs(s+2);
                            b[i][j]=0;
                            b[i+1][j]=0;
                        }
                    }
                    if(j+1<=n){
                        if(b[i][j+1]==0){
                            b[i][j]=1;
                            b[i][j+1]=1;
                            dfs(s+2);
                            b[i][j]=0;
                            b[i][j+1]=0;
                        }
                    }
                    return;
                }
            }
        }
    }
    int main()
    {
        int i;
        while(~scanf("%d",&n)){
            memset(b,0,sizeof(b));
            ans=0;
            dfs(0);
            printf("%lld
    ",ans);    
        }
        return 0;
    }
    View Code
    
    
    然后再找规律,猜想存在递推关系,项数可以一点点加,当用五层循环寻找递推系数关系时发现,存在一种相同的系数情况。//1 5 1 -1 0
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #define MAX 1005
    #define INF 0x3f3f3f3f
    #define MOD 1000000007
    using namespace std;
    typedef long long ll;
    
    int x[]={0,1,5,11,36,95,281,781,2245,6336,18061};
    
    void find(int i){
        for(int a=-10;a<=10;a++){
            for(int b=-10;b<=10;b++){
                for(int c=-10;c<=10;c++){
                    for(int d=-10;d<=10;d++){
                        for(int e=-10;e<=10;e++){
                            if(a*x[i-1]+b*x[i-2]+c*x[i-3]+d*x[i-4]+e==x[i]){
                                printf("%d:%d %d %d %d %d
    ",i,a,b,c,d,e);
                            }
                        }
                    }
                }
            }
        }
    }
    int main()
    {
        int n,i;
        for(i=5;i<=10;i++){
            find(i);
        }
        return 0;
    }
    View Code
    
    
    最后确定递推公式:f[i]=f[i-1]+5*f[i-2]+f[i-3]-f[i-4]+0。(注意:负数系数取余时要先加再余)然后套矩阵快速幂。
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #define MAX 10
    #define MOD 1000000007
    using namespace std;
    typedef long long ll;
    
    struct mat{
        ll a[MAX][MAX];
    };
    
    mat operator *(mat x,mat y)    //重载乘运算
    {
        mat ans;
        memset(ans.a,0,sizeof(ans.a));
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                for(int k=1;k<=4;k++){
                    ans.a[i][j]+=(x.a[i][k]*y.a[k][j]+MOD)%MOD;    //先加后余,避免负数取模异常
                    ans.a[i][j]%=MOD;
                }
            }
        }
        return ans;
    }
    mat qMod(mat a,ll n)    //矩阵快速幂
    {
        mat t;
        t.a[1][1]=1;t.a[1][2]=5;t.a[1][3]=1;t.a[1][4]=-1;
        t.a[2][1]=1;t.a[2][2]=0;t.a[2][3]=0;t.a[2][4]=0;
        t.a[3][1]=0;t.a[3][2]=1;t.a[3][3]=0;t.a[3][4]=0;
        t.a[4][1]=0;t.a[4][2]=0;t.a[4][3]=1;t.a[4][4]=0;    //单位矩阵
        while(n){
            if(n&1) a=t*a;    //顺序不可颠倒
            n>>=1;
            t=t*t;
        }
        return a;
    }
    int main()
    {
        ll n;
        while(~scanf("%I64d",&n)){
            if(n==1) printf("1
    ");
            else if(n==2) printf("5
    ");
            else if(n==3) printf("11
    ");
            else if(n==4) printf("36
    ");
            else{
                mat a;
                memset(a.a,0,sizeof(a.a));
                a.a[1][1]=36;
                a.a[2][1]=11;
                a.a[3][1]=5;
                a.a[4][1]=1;   //初始矩阵
                a=qMod(a,n-4);
                printf("%I64d
    ",a.a[1][1]);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    看到你还在用Maven,Gradle难道不香吗?
    技术干货|完美搭建web自动化环境
    使用可视化的Docker进行自动化测试
    使用Robot Framework实现多平台自动化测试
    还在手工写接口测试文档吗,已经out了
    巧用Python脚本解决自动化图形验证码难题
    为什么要进行URL编码
    C# (CLR) 中内存分配解析
    DIV+CSS规范命名大全集合
    Memcached集群/分布式/高可用 及 Magent缓存代理搭建过程 详解
  • 原文地址:https://www.cnblogs.com/yzm10/p/9313916.html
Copyright © 2011-2022 走看看