2017 ICPC 广西邀请赛1004 Covering

探讨使用1×2和2×1木块完全覆盖4×n矩阵的方案数量,通过状态压缩动态规划与矩阵快速幂求解。

Covering

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 187    Accepted Submission(s): 107


Problem Description
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×2 and 2×1, and the size of the playground is 4×n.

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

 

Input
There are no more than 5000 test cases. 

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

1n1018
 

 

Output
For each test cases, output the answer mod 1000000007 in a line.
 

 

Sample Input
1 2
 

 

Sample Output
1 5
 

 

Source
 
打表
/*
* @Author: Administrator
* @Date:   2017-08-31 17:40:04
* @Last Modified by:   Administrator
* @Last Modified time: 2017-09-01 11:03:00
*/
/*
 题意:给你一个4*n的矩阵,然后让你用1*2和2*1的木块放,问你完美覆盖的
    方案数

 思路:状压DP找规律
*/

#include <bits/stdc++.h>

#define MAXN 100
#define MAXM 20
#define MAXK 15
using namespace std;

int dp[MAXN][MAXM];//dp[i][j]表示前ihang 
int n;

inline bool ok(int x){
    //判断是不是有连续个1的个数是奇数
    int res=0;
    while(x){
        if(x%2==1){
            res++;
        }else{
            if(res%2==1) return false;
            else res=0;
        }
        x/=2;
    }
    if(res%2==1) return false;
    else return true;
}

inline void init(){
    memset(dp,0,sizeof dp);
}

int main(){
    freopen("in.txt","r",stdin);
    for(int n=1;n<=50;n++){
        init();
        for(int i=0;i<=MAXK;i++){//初始化第一行的没种状态
            if(ok(i)==true)
                dp[1][i]=1;
        }
        for(int i=1;i<n;i++){
            for(int j=0;j<=MAXK;j++){
                if(dp[i][j]!=0){
                    for(int k=0;k<=MAXK;k++){
                        if( (j|k)==MAXK && ok(j&k) )
                            ///j|k==tot-1的话就是能拼起来组成
                            dp[i+1][k]+=dp[i][j];
                    }
                }
            }
        }
        printf("%d\n",dp[n][MAXK]);
    }
    return 0;
}
/*
* @Author: Administrator
* @Date:   2017-09-01 11:17:37
* @Last Modified by:   Administrator
* @Last Modified time: 2017-09-01 11:28:09
*/
#include <bits/stdc++.h>

#define MAXN 5
#define mod 1000000007
#define LL long long

using namespace std;

/********************************矩阵快速幂**********************************/
class Matrix {
    public:
        LL a[MAXN][MAXN];
        LL n;  

        void init(LL x) {
            memset(a,0,sizeof(a));
            if (x)  
                for (int i = 0; i < MAXN ; i++)
                    a[i][i] = 1LL;
        }
        
        Matrix operator +(Matrix b) {
            Matrix c;
            c.n = n;
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                    c.a[i][j] = (a[i][j] + b.a[i][j]) % mod;
            return c;
        }

        Matrix operator +(LL x) {
            Matrix c = *this;
            for (int i = 0; i < n; i++)
                c.a[i][i] += x;
            return c;
        }

        Matrix operator *(Matrix b)
        {
            Matrix p;
            p.n = b.n;   
            p.init(0);
            for (int i = 0; i < n; i++)
                for (int j = 0; j < n; j++)
                for (int k = 0; k < n; k++)
                    p.a[i][j] = (p.a[i][j] + (a[i][k]*b.a[k][j])%mod) % mod;
            return p;
        }

        Matrix power(LL t) {
            Matrix ans,p = *this;
            ans.n = p.n;  
            ans.init(1);
            while (t) {
                if (t & 1)
                    ans=ans*p;
                p = p*p;
                t >>= 1;
            }
            return ans;
        }
}init,unit;
/********************************矩阵快速幂**********************************/

LL n;

int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%lld",&n)!=EOF){
        if(n<=4){
            switch(n){
                case 1:
                    puts("1");
                    break;
                case 2:
                    puts("5");
                    break;
                case 3:
                    puts("11");
                    break;
                case 4:
                    puts("36");
                    break;
            }
            continue;
        }
        init.init(0);
        init.n=4;
        init.a[0][0]=36;
        init.a[0][1]=11;
        init.a[0][2]=5;
        init.a[0][3]=1;
        unit.init(0);
        unit.n=4;
        unit.a[0][0]=1;
        unit.a[1][0]=5;
        unit.a[2][0]=1;
        unit.a[3][0]=-1;
        unit.a[0][1]=1;
        unit.a[1][2]=1;
        unit.a[2][3]=1;
        unit=unit.power(n-4);
        init=init*unit;
        printf("%lld\n",(init.a[0][0]+mod)%mod);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wuwangchuxin0924/p/7462479.html

### ICPC昆明邀请赛题目I解题思路 #### 背景分析 ICPC昆明邀请赛中的题目通常涉及算法设计、数据结构应用以及复杂度优化等问题。对于题目I,其核心在于判断给定数组是否存在某种特定条件下的排列方式,并输出相应的结果。如果无法满足条件,则返回`impossible`。 根据已知样例输入 `4 1 2 3 4` 和对应的输出 `impossible 1 0 1 0 2 impossible` 可推测该问题可能与数组的奇偶性分布或者某些约束条件有关[^1]。 --- #### 关键概念解析 此问题的核心可以分解为以下几个方面: 1. **输入解读**: 输入的第一部分是一个整数N表示数组长度,随后是一组由N个正整数组成的序列。目标是对这些数字进行处理并验证是否符合条件。 2. **条件判定**: 如果存在一种合法的方式使得数组满足某项性质(具体需通过进一步推导),则按照指定格式输出;否则输出`impossible`。这里的合法性可能是基于奇偶性的分配或其他数学特性。 3. **实现方法**: 基于上述背景,可以通过模拟法逐一尝试每种可能性来解决问题。以下是具体的逻辑框架: - 初始化变量用于记录当前状态。 - 对数组中的每个元素依次遍历,依据预设规则更新状态。 - 若发现任何冲突情况立即终止计算并标记为非法情形。 - 完成全部扫描后确认最终结论。 --- #### 实现代码示例 以下提供了一段Python代码作为参考解决方案之一: ```python def solve_problem(n, nums): result = [] # 判断初始可行性 if not check_initial_condition(nums): # 自定义函数检测基本约束 return ["impossible"] # 主循环过程 for i in range(len(nums)): current_value = nums[i] # 执行操作... updated_result = perform_operation(current_value) # 更新局部状态 if is_conflict(updated_result): # 检查是否有矛盾发生 result.append("impossible") break result.extend(updated_result) return result # 辅助功能模块 def check_initial_condition(array): """自定义初始化校验""" pass # 替代实际业务逻辑 def perform_operation(value): """执行单步转换""" pass # 替代实际业务逻辑 def is_conflict(state): """评估当前状态是否违反规定""" pass # 替代实际业务逻辑 ``` 注意:以上仅为伪代码示意,真实场景下需要补充完整的辅助函数定义以适配具体需求。 --- #### 总结说明 综上所述,在解决此类竞赛类编程挑战时应注重理解命题意图,合理运用基础理论知识构建高效求解路径。同时也要关注边界测试用例覆盖范围确保程序鲁棒性良好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值