zoukankan      html  css  js  c++  java
  • AT4539 Walk

    • 题意:给一张有向简单图,给出邻接矩阵,求长度为 (K) 的路径条数,答案对 (10^9+7) 取模。
    • 题解:
    • 代码:
    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <bitset>
    #include <iostream>
    #include <unordered_map>
    #include <queue>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const ll N = 55;
    ll mod = 1e9 + 7;
    const ll maxn = 110;
    ll n;
    struct Matrix {
        ll a[N][N];
        Matrix (){memset(a, 0, sizeof a);}
        Matrix operator*(Matrix rhs) const{
            Matrix ret;
            for (int i = 0; i < n; i ++) {
                for (int j = 0; j < n; j ++) {
                    for (int k = 0; k < n; k ++) {
                        (ret.a[i][j] += a[i][k] * rhs.a[k][j]) %= mod;
                    }
                }
            }
            return ret;
        }
    };
    Matrix q_pow(Matrix A, ll k) {
        Matrix ret = A;
        k--;
        if (k <= 0)return ret;
        while (k) {
            if (k & 1) {
                ret = ret * A;
            }
            k >>= 1;
            A = A * A;
        }
        return ret;
    }
    
    void solve() {
        Matrix A;
        ll  k;
        cin >> n >> k;
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                cin >> A.a[i][j];
            }
        }
        A = q_pow(A, k);
        ll ans = 0;
        for (int i = 0; i < n; i ++) {
            for (int j = 0 ; j < n; j ++) {
                (ans += A.a[i][j]) %= mod;
            }
        }
        cout << ans << endl;
    }
    signed main() {
        ll t = 1;
        while (t--) solve();
        return 0;
    }
    
  • 相关阅读:
    红黑树数据结构剖析
    miniui表单验证守则总结
    常用的JS页面跳转代码调用大全
    Jsp页面跳转和js控制页面跳转的几种方法
    处理和引发事件
    HeaderHandler 委托
    序列化SoapFormatter
    Debug.Assert
    C#的Thread类
    再次学习线程概念
  • 原文地址:https://www.cnblogs.com/Xiao-yan/p/14609242.html
Copyright © 2011-2022 走看看