hdu 5015 233 Matrix 矩阵快速幂 2014 ACM/ICPC Asia Regional Xi'an Online

来源:互联网 发布:并行算法的设计与分析 编辑:程序博客网 时间:2024/05/01 06:40

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015


Problem Description
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 2333, 23333... (it means a0,1 = 233,a0,2 = 2333,a0,3 = 23333...) Besides, in 233 matrix, we got ai,j = ai-1,j +ai,j-1( i,j ≠ 0). Now you have known a1,0,a2,0,...,an,0, could you tell me an,m in the 233 matrix?
 

Input
There are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n,m(n ≤ 10,m ≤ 109). The second line contains n integers, a1,0,a2,0,...,an,0(0 ≤ ai,0 < 231).
 

Output
For each case, output an,m mod 10000007.
 

Sample Input
1 112 20 03 723 47 16
 

Sample Output
234279972937
Hint
 

Source
2014 ACM/ICPC Asia Regional Xi'an Online

要先构造矩阵,然后矩阵快速幂求解....太菜了,自己不会构造,贴一个讲解...http://www.cnblogs.com/whatbeg/p/3971994.html

代码:
#include <algorithm>#include <cstdlib>#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <cctype>#include <cmath>#include <stack>#include <queue>#include <list>#include <map>#include <set>using namespace std;#define min2(x, y)     min(x, y)#define max2(x, y)     max(x, y)#define min3(x, y, z)  min(x, min(y, z))#define max3(x, y, z)  max3(x, max(y, z))#define clr(x, y)      memset(x, y, sizeof(x))#define fr(i,n)        for(int i = 0; i < n; i++)#define fr1(i,n)       for(int i = 1; i < n; i++)#define upfr(i,j,n)    for(int i = j; i <= n; i++)#define dowfr(i,j,n)   for(int i = n; i >= j; i--)#define scf(n)         scanf("%d", &n)#define scf2(n,m)      scanf("%d %d",&n,&m)#define ptf(n)         printf("%d",n)#define ptf64(n)       printf("%I64d",n)#define ptfs(s)        printf("%s",s)#define ptln()         printf("\n")#define ptk()          printf(" ")#define ptc()         printf("*")#define srt(a,n)       sort(a,n)#define LL long long#define pi acos(-1.0)#define inf 1 << 31-1#define eps 0.00001#define maxn 13#define mod 10000007int n, m;struct matrix{    __int64 arr[maxn][maxn];    matrix()    {        clr(arr, 0);        upfr(i, 1, n+2)         arr[i][i] = 1LL;    }};__int64 a[maxn], ans[maxn];matrix mult(matrix a, matrix b){    matrix c;    upfr(i,1,n+2)    {        upfr(j,1,n+2)        {            c.arr[i][j] = 0;            upfr(k,1,n+2)            c.arr[i][j] = (c.arr[i][j] + (a.arr[i][k] * b.arr[k][j]) % mod ) % mod;        }    }    return c;}matrix pow_mod(matrix a,int b){    matrix res;    while(b)    {        if(b & 1)            res = mult(res,a);        a = mult(a,a);        b >>= 1;    }    return res;}int main(){    //freopen("in.txt","r", stdin);    while(scf2(n, m) == 2)    {        clr(ans, 0);        clr(a, 0);        ans[0] = 0;        upfr(i, 1, n)        {            scanf("%I64d", &a[i]);            ans[i] = ans[i-1] + a[i];        }        __int64 sum = ans[n];        if(m == 1)        {            printf("%I64d\n", 233LL + sum);            continue;        }        matrix p;        clr(p.arr, 0);        upfr(i, 1, n+1)        p.arr[i][1] = 10LL;        upfr(i, 2, n+1)        {            upfr(j, 2, n+1)            if(i >= j)                p.arr[i][j] = 1LL;        }        upfr(i, 1, n+2)        p.arr[i][n+2] = 1LL;        matrix I;        clr(I.arr, 0);        I.arr[1][1] = 233LL;        upfr(i, 2, n+1)        I.arr[i][1] = (233LL + ans[i-1]) % mod;        I.arr[n+2][1] = 3LL;//        upfr(i,1,n+2)//        ptc(),ptf64(I.arr[i][1]),ptln();        matrix ret = pow_mod(p, m-1);//        upfr(i, 1, n+2)//        ptc(),ptf64(ret.arr[i][1]),ptk();        ret = mult(ret, I);        printf("%I64d\n",ret.arr[n+1][1] % mod);    }    return 0;}


0 0
原创粉丝点击