首页 > 其他分享 >HDU-5015

HDU-5015

时间:2022-11-18 09:45:23浏览次数:50  
标签:HDU 5015 int cin ++ mat

HDU-5015

思路

\(n \leq 10\)是一个较为明显的提示,容易想到一列一列的转移,通过对\(a_{n,m}\)的寻找,发现\(a_{n,m} = \sum_1^{n} a_{i,m-1} + a_{0,m}\),其中\(\sum_1^{n} a_{i,m-1}\)全都是前一列的元素,\(a_{0,m}\)是可以推导出的常数,因此,使用矩阵快速幂就可以求出答案。

矩阵推导

image

Code

#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}

using ll = long long;
#define int long long
const int mod = 10000007;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
int n, m;

struct mat {
    vector<vector<int>> m;
    mat (){}
    mat(int n) {
        m.resize(n + 2, vector<int> (n + 2, 0));
    }
};

mat operator * (const mat & a, const mat & b) {
    mat c(n);
    for (int i = 0;i < n + 2;i++) {
        for (int j = 0;j < n + 2;j ++) {
            for (int k = 0;k < n + 2;k++) {
                c.m[i][j] = (c.m[i][j] + a.m[i][k] * b.m[k][j]) % mod;
            }
        }
    }
    return c;
}

mat qpow(mat a, int b) {
    mat c(n);
    for (int i = 0;i < n+2;i++) c.m[i][i] = 1;
    while (b) {
        if (b & 1) {
            c = c * a;
        }
        a = a * a;
        b >>= 1;
    }
    return c;
}

inline void _A_A_() {
    #ifdef LOCAL
    freopen("in.in", "r", stdin);
    #endif
    _u_u_;
    while (cin >> n >> m) {
        vector<int> v(n + 2);
        for (int i = n - 1;i >= 0;i -- ) cin >> v[i];
        v[n] = 233, v[n + 1] = 1;
        mat s(n);
        for (int i = 0;i < n + 1;i++) {
            for (int j = 0;j <= i;j++) {
                s.m[i][j] = 1;
            }
        }
        s.m[n + 1][n + 1] = 1;
        s.m[n][n] = 10;
        s.m[n + 1][n] = 3;
        s = qpow(s, m);
        int ans = 0;
        for (int i = 0;i < n + 2;i++) {
            ans = (ans + v[i] * s.m[i][0]) % mod;
        }
        cout << ans << "\n";
    }
}

标签:HDU,5015,int,cin,++,mat
From: https://www.cnblogs.com/FanWQ/p/16902170.html

相关文章

  • HDU-4549
    HDU-4549思路列出数列前几项,发现\(a\)和\(b\)的次数符合斐波那契数列。问题就转化为了求出\(1e9\)项斐波那契数列,可以使用矩阵快速幂加速。但是求出的第\(1e9\)个斐波那......
  • HDU-4565
    HDU-4565思路这里太难想了wwwCode#include<bits/stdc++.h>usingnamespacestd;#define_u_u_ios::sync_with_stdio(false),cin.tie(nullptr)#definecfint_o......
  • HDU 2191:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 (多重背包)
    悼念512汶川大地震遇难同胞——珍惜现在,感恩生活TimeLimit:1000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):2529......
  • 题解 HDU4035 【Maze】
    postedon2022-08-1712:33:51|under题解|sourceproblemhttps://vjudge.net/problem/HDU-4035SHY在一棵树上随机游走,从根节点出发,每次有\(k_u\)的几率回到根节......
  • HDU 3726 Graph and Queries
    DescriptionYouaregivenanundirectedgraphwithNvertexesandMedges.Everyvertexinthisgraphhasanintegervalueassignedtoitatthebeginni......
  • HDU 3468 Treasure Hunting
    DescriptionDoyouliketreasurehunting?Today,withoneofhisfriend,iSeaisonaventuretripagain.Asmostmoviesaid,theyfindsomanygoldhid......
  • HDU 5446 Unknown Treasure
    ProblemDescriptionOnthewaytothenextsecrettreasurehidingplace,themathematiciandiscoveredacaveunknowntothemap.Themathematicianenter......
  • HDU 5434 Peace small elephant
    ProblemDescriptionXiaoMinglikesplayingtheinternationalchess,especiallyliketheelephant(itcanmovebyslantwhilenobarrier),buthethink......
  • HDU 5442 Favorite Donut
    ProblemDescriptionLuluhasasweettooth.Herfavoritefoodisringdonut.Everydayshebuysaringdonutfromthesamebakery.Aringdonutisconsis......
  • HDU 3518 Boring counting
    Description035nowfacedatoughproblem,hisenglishteachergiveshimastring,whichconsistswithnlowercaseletter,hemustfigureouthowmanysub......