【BestCoder】 HDOJ 5171 GTY's birthday gift

来源:互联网 发布:四柱算命软件 编辑:程序博客网 时间:2024/05/22 01:32

矩阵快速幂。。。。构造矩阵的方法有很多种,任选一种即可。。。

#include <iostream>#include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits>#include <cstdlib>#include <cmath>#include <time.h>#define maxn 405#define maxm 400005#define eps 1e-7#define mod 10000007#define INF 999999999#define PI (acos(-1.0))#define lowbit(x) (x&(-x))#define mp make_pair#define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R#define pii pair<int, int>#pragma comment(linker, "/STACK:16777216")typedef long long LL;typedef unsigned long long ULL;//typedef int LL;using namespace std;LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}// headLL mat[10][10];LL res[10][10];LL mid[10][10];int n, k;LL cal(int c){    memset(res, 0, sizeof res);    memset(mat, 0, sizeof mat);    res[1][1] = res[2][2] = res[3][3] = res[4][4] = 1;    mat[1][1] = mat[1][2] = mat[2][1] = 1;    mat[2][2] = 0;    mat[1][3] = mat[1][4] = mat[2][3] = 1;    mat[2][4] = 0;    mat[3][3] = mat[4][4] = 1;    while(c) {        if(c % 2) {            for(int i = 1; i <= 4; i++)                for(int j = 1;  j <= 4; j++) {                    LL t = 0;                    for(int k = 1; k <= 4; k++)                        t = (t + res[i][k] * mat[k][j]) % mod;                    mid[i][j] = t;                }            for(int i = 1; i <= 4; i++)                for(int j = 1; j <= 4; j++)                    res[i][j] = mid[i][j];        }        for(int i = 1; i <= 4; i++)            for(int j = 1; j <= 4; j++) {                LL t = 0;                for(int k = 1; k <= 4; k++)                    t = (t + mat[i][k] * mat[k][j]) % mod;                mid[i][j] = t;            }        for(int i = 1; i <= 4; i++)            for(int j = 1; j <= 4; j++)                mat[i][j] = mid[i][j];        c /= 2;    }    return res[1][4];}void work(){    LL a, b, x;    scanf("%I64d%I64d", &a, &b);    if(a < b) swap(a, b);    LL res = (a + b) % mod;    for(int i = 3; i <= n; i++) {        scanf("%I64d", &x);        if(x >= a) b = a, a = x;        else b = max(b, x);        res = (res + x) % mod;    }    res = (res + a * (cal(k + 1) - 1 + mod) % mod + b * cal(k) % mod) % mod;    printf("%I64d\n", res);}int main(){    //for(int i = 1; i <= 10; i++) printf("AAAAA %lld\n", cal(i));    while(scanf("%d%d", &n, &k)!=EOF) {        work();    }            return 0;}


0 0
原创粉丝点击