0x3B 233 Matrix (矩阵快速幂)

一开始我还想通过递推关系找规律呢,后来发现不是很好找,即使找到相应的东西也需要求 1 0 9 10^9 109 个元素的和。

想想可以用矩阵快速幂解决,对于这种有着递推关系且数量较大的递推式来说,但始终想不到如何构造矩阵。

后来看题解才知道,原来可以一列一列往右推构成递推关系。如果去掉第一行,我们很容易构造矩阵来将第一列推到第二列,而对于 233 233 233 2333 2333 2333 这些我们可以 ∗ 10 + 3 *10+3 10+3 递推到,然后如果要合一起的话这里需要再增加一行才能实现 233 233 233 推到 2333 2333 2333

首先第一行第一个数可以改为 23 23 23 这样更好满足 ∗ 10 + 3 *10+3 10+3 的关系且并不会影响最终结果,然后我们再增加 n + 1 n+1 n+1这行,全是 3 3 3,这样就很容易利用矩阵从一列推到另一列了。例如 n = 3 , m = 3 n=3,m=3 n=3,m=3 的情况,可以构造矩阵:

233 10 0 0 0 1 23
x1+233 10 1 0 0 1 x1
x1+x2+233 = 10 1 1 0 1 * x2
x1+x2+x3+233 10 1 1 1 1 x3
3 0 0 0 0 0 3

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 100010 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 10000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int n, m ;
struct Mat {
    ll mat[15][15] ;
} ;

Mat operator * (Mat a, Mat b) {
    Mat c ;
    clr(c.mat) ;
    rep(k, 0, n + 1)
	rep(i, 0, n + 1)
	rep(j, 0, n + 1)
	c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD ;
    return c ;
}

int main() {
    while (~scanf("%d%d", &n, &m)) {
        Mat res, a;
        rep(i, 0, n + 1)
        rep(j, 0, n + 1)
        res.mat[i][j] = (i == j) ;
        rep(i, 0, n + 1)
        rep(j, 0, n + 1) {
            if (j == 0 && i != n + 1) a.mat[i][j] = 10 ;
        	else if ((i >= j && i != n + 1) || j == n + 1) a.mat[i][j] = 1 ;
            else a.mat[i][j] = 0 ;
        }
        for (; m > 0; m >>= 1, a = a * a) if (m & 1) res = res * a ;
        ll x, ans = 23 * res.mat[n][0] ;
        rep(i, 1, n) {
            scanf("%lld", &x) ;
            ans = (ans + x * res.mat[n][i] % MOD) % MOD ;
        }
        ans = (ans + 3 * res.mat[n][n + 1] % MOD) % MOD ;
        printf("%lld\n", ans) ;
    }
    return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值