poj #3734 Blocks(矩阵快速幂)

本文介绍了一道关于矩阵快速幂的应用题目,通过构建特定的矩阵形式并利用快速幂算法来高效解决组合计数问题。文章详细展示了状态转移方程及矩阵乘法、快速幂的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这题也是属于“dp优化”的一类。本题解法有矩阵快速幂和组合数学两种(后者写起来会简单很多,但是推导过程繁琐),所以选用了矩阵快速幂的方法。
标签:矩阵快速幂
Blocks

Time Limit: 1000MS Memory Limit: 65536K
Description

Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.

Input

The first line of the input contains an integer T (1≤ T ≤100), the number of test cases. Each of the next T lines contains an integer N (1≤ N ≤10^9) indicating the number of blocks.

Output

For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007.


题意大概就是有红、黄、蓝、绿四种块,要求红、绿两种块的数量必须为偶数,求有多少种放块的方案数。
对于这道题不难想到用dp的方法,如果我们令
f[i][1] 表示前i个块,红、绿色块均为偶数个的方案数
f[i][2] 表示前i个块,红色块为偶数个、绿色块为奇数个的方案数
f[i][3] 表示前i个块,红色块为奇数个、绿色块为偶数个的方案数
f[i][4] 表示前i个块,红、绿色块均为奇数个的方案数
那么对于n>=2,转移方程为
f[i][1]=f[i1][1]2+f[i1][2]1+f[i1][3]1+f[i1][4]0
f[i][2]=f[i1][1]1+f[i1][2]2+f[i1][3]0+f[i1][4]1
f[i][3]=f[i1][1]1+f[i1][2]0+f[i1][3]2+f[i1][4]1
f[i][4]=f[i1][1]0+f[i1][2]1+f[i1][3]1+f[i1][4]2
(很容易推导出来)
如果我们直接用一个for循环暴力去算,时间复杂度是O(4n),对于N ≤10^9,这显然是不能接受的。
如果将其表示为矩阵,就是

2110120110210112

这时候我们可以利用矩阵快速幂去解决这道题,需要注意的一个细节问题,就是算快速幂的时候,矩阵初值要赋为单位矩阵(不懂自己问度娘),下面是代码。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define maxn 5
#define ha 10007
#define clear(x) x.w=x.u=0,memset(x.a,0,sizeof(x.a));
using namespace std;
typedef long long ll;
struct matrix
{
    int w,u; //length,width
    ll a[maxn][maxn];
};
matrix multi(matrix x,matrix y) //matrix multiplication 
{
    int i,j,k;
    matrix tmp; clear(tmp); tmp.w=tmp.u=4;
    for (i=1;i<=4;i++)
        for (j=1;j<=4;j++)
        {
            if (!x.a[i][j]) continue;
            for (k=1;k<=4;k++)
            {
                tmp.a[i][k]+=x.a[i][j]*y.a[j][k];
                tmp.a[i][k]%=ha;
            } 
        }
    return tmp;
}
matrix power(matrix s,int p) //matrix fast power 
{
    int i;
    matrix res; clear(res); res.w=res.u=4;
    for (i=1;i<=4;i++) res.a[i][i]=1;
    while (p)
    {
        if (p&1) res=multi(res,s);
        p>>=1;
        s=multi(s,s);
    }
    return res;
}
int main()
{
    int t,n;
    matrix k; clear(k); k.w=k.u=4;
    k.a[1][1]=2; k.a[1][2]=1; k.a[1][3]=1; k.a[1][4]=0;
    k.a[2][1]=1; k.a[2][2]=2; k.a[2][3]=0; k.a[2][4]=1;
    k.a[3][1]=1; k.a[3][2]=0; k.a[3][3]=2; k.a[3][4]=1;
    k.a[4][1]=0; k.a[4][2]=1; k.a[4][3]=1; k.a[4][4]=2;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d",&n); matrix ans=power(k,n);
        printf("%d\n",ans.a[1][1]);
    }
    return 0;
}
### 关于POJ 1995问题的快速幂C++实现 对于POJ 1995问题,其核心在于通过矩阵快速幂算法高效解决大规模数据下的指数运算。以下是基于引用材料中的相关内容构建的一个完整的解决方案。 #### 矩阵快速幂的核心逻辑 矩阵快速幂是一种高效的计算方式,在处理线性递推关系时尤为有效。例如斐波那契数列可以通过构造特定的转移矩阵来加速计算[^4]。具体来说,给定一个初始状态向量和一个转移矩阵,经过若干次幂运算后可获得目标状态。 以下是一个通用的矩阵快速幂模板: ```cpp #include <iostream> using namespace std; const int N = 2; // 定义矩阵大小 struct Matrix { long long m[N][N]; }; // 矩阵乘法函数 Matrix multiply(const Matrix& a, const Matrix& b) { Matrix c; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { c.m[i][j] = 0; for (int k = 0; k < N; ++k) { c.m[i][j] += a.m[i][k] * b.m[k][j]; c.m[i][j] %= 10000; // 取模操作 } } } return c; } // 快速幂函数 Matrix fastPower(Matrix base, long long exp) { Matrix result; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { result.m[i][j] = (i == j); } } while (exp > 0) { if (exp % 2 == 1) { result = multiply(result, base); } base = multiply(base, base); exp /= 2; } return result; } int main() { long long n; cin >> n; // 初始化转移矩阵 Matrix trans; trans.m[0][0] = 0; trans.m[0][1] = 1; trans.m[1][0] = 1; trans.m[1][1] = 1; // 计算结果矩阵 if (n == 0 || n == 1) { cout << 1 << endl; } else { Matrix res = fastPower(trans, n - 1); // 初始状态向量 long long fib_prev = 1; long long fib_curr = 1; // 输出结果 cout << (res.m[0][0] * fib_prev + res.m[0][1] * fib_curr) % 10000 << endl; } return 0; } ``` 此代码实现了针对斐波那契数列的大规模项求解功能,并采用了取模`%10000`的操作以满足题目需求。其中的关键部分包括矩阵乘法、快速幂以及状态转移的设计[^3]。 #### 特殊注意点 在实际提交过程中需要注意以下几个方面: - **大数组定义**:如果涉及更大的矩阵或者更复杂的动态规划表,则需特别留意内存分配的位置及其范围限制[^2]。 - **时间复杂度控制**:尽管快速幂本身具有较低的时间复杂度O(log n),但在极端情况下仍需验证是否存在进一步优化空间。 - **边界条件处理**:如输入为较小数值时直接返回已知答案而非进入循环计算流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值