栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3734 Blocks

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3734 Blocks

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>using namespace std;#define maxn 4#define w 10007struct Matrix{    int a[maxn][maxn];    int x, y;}ans, f;int n;int matrix[maxn][maxn]={{2,1,1,0},{1,2,0,1},{1,0,2,1},{0,1,1,2}};Matrix mul(Matrix &a, const Matrix &b){    Matrix ret;    for (int i =0; i < a.x; i++)        for (int j =0; j < b.y; j++)        { int d =0; for (int k =0; k < a.y; k++)     d = (d + a.a[i][k] * b.a[k][j] % w) % w; ret.a[i][j] = d;        }    ret.x = a.x;    ret.y = b.y;    return ret;}Matrix power(Matrix m, int n){    Matrix ret;    memset(ret.a, 0, sizeof(ret.a));    for (int i =0; i <4; i++)        ret.a[i][i] =1;    ret.x = ret.y =4;    for (int k =1; k <= n; k <<=1)    {        if (k & n) ret = mul(ret, m);        m = mul(m, m);    }    return ret;}int main(){    f.x = f.y =4;    for (int i =0; i <4; i++)        for (int j =0; j <4; j++) f.a[i][j] = matrix[i][j];    int t;    scanf("%d", &t);    while (t--)    {        scanf("%d", &n);        f.x = f.y =4;        memset(ans.a, 0, sizeof(ans.a));        ans.a[0][0] =1;        ans.x =1;        ans.y =4;        ans = mul(ans, power(f, n));        printf("%dn", ans.a[0][0]);    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/375042.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号