hdu 5411 CRB and Puzzle 矩阵高速幂

链接

题解链接:http://www.cygmasot.com/index.php/2015/08/20/hdu_5411/

给定n个点 常数m

以下n行第i行第一个数字表示i点的出边数。后面给出这些出边。

问:图里存在多少条路径使得路径长度<=m。路径上的点能够反复。

思路:

首先能得到一个m*n*n的dp。dp[i][j]表示路径长度为i 路径的结尾为j的路径个数 。

答案就是sigma(dp[i][j]) for every i from 1 to m, j from 1 to n;

我们先计算 路径长度恰好为 i 的方法数。

用矩阵高速幂,会发现是

当中B矩阵是一个n*n的矩阵。也就是输入的邻接矩阵。

A是一个n行1列的矩阵 A[i][1]表示长度为1且以i结尾的路径个数,所以A矩阵是全1矩阵。

相乘得到的n*1 的矩阵求和就是路径长度恰好为i的条数。

那么<=m的路径就是:

把A提出来,里面就是一个关于B的矩阵等比数列。

B的求发主要是二分。详见POJ3233

模板不大好,交G++能过

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<ctime>  

using namespace std;
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != ‘-‘ && (c<‘0‘ || c>‘9‘)) c = getchar();
    sgn = (c == ‘-‘) ? -1 : 1;
    ret = (c == ‘-‘) ? 0 : (c - ‘0‘);
    while (c = getchar(), c >= ‘0‘&&c <= ‘9‘) ret = ret * 10 + (c - ‘0‘);
    ret *= sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar(‘-‘);
        x = -x;
    }
    if (x>9) pt(x / 10);
    putchar(x % 10 + ‘0‘);
}
const int mod = 2015;
const int N = 51;

struct Matrix
{
    int m[N][N];
}G[2000];
int top;
Matrix I;
int n, k;
const int M = 2015;
Matrix add(Matrix a, Matrix b)
{
    Matrix &c = G[top++];
    for (int i = 0; i<n; i++)
    {
        for (int j = 0; j<n; j++)
        {
            c.m[i][j] = a.m[i][j] + b.m[i][j];
            c.m[i][j] %= M;
        }
    }
    top--;
    return c;
}

Matrix multi(Matrix a, Matrix b)
{
    Matrix &c = G[top++];
    for (int i = 0; i<n; i++)
    {
        for (int j = 0; j<n; j++)
        {
            c.m[i][j] = 0;
            for (int k = 0; k<n; k++)
                c.m[i][j] += a.m[i][k] * b.m[k][j];
            c.m[i][j] %= M;
        }
    }
    top--;
    return c;
}

Matrix power(Matrix A, int n)
{
    Matrix &ans = G[top++], &p = G[top++];
    ans = I; p = A;
    while (n)
    {
        if (n & 1)
        {
            ans = multi(ans, p);
            n--;
        }
        n >>= 1;
        p = multi(p, p);
    }
    top -= 2;
    return ans;
}

Matrix sum(Matrix A, int k)
{
    if (k == 1) return A;
    Matrix &t = G[top++];
    t = sum(A, k / 2);
    if (k & 1)
    {
        Matrix &cur = G[top++];
        cur = power(A, k / 2 + 1);
        t = add(t, multi(t, cur));
        t = add(t, cur);
        top--;
    }
    else
    {
        Matrix &cur = G[top++];
        cur = power(A, k / 2);
        t = add(t, multi(t, cur));
        top--;
    }
    top--;
    return t;
}

int m;
void add(int &x, int y){
    x += y;
    if (x >= mod)x -= mod;
}
int B[N][N];
int main(){
    memset(I.m, 0, sizeof I.m);
    for (int i = 0; i < N; i++)I.m[i][i] = 1;
    int T; rd(T);
    while (T--){
        rd(n); rd(m);
        Matrix A;
        top = 0;
        memset(A.m, 0, sizeof A.m);
        for (int i = 1; i <= n; i++) {
            int tmp; rd(tmp); while (tmp--) { int u; rd(u); A.m[i-1][u-1] = 1; }
        }
        if (m == 0) { puts("1"); continue; }
        if (m == 1){ pt(n + 1); puts(""); continue; }
        Matrix ans = sum(A, m-1);
        for (int i = 0; i<n; i++)
            for (int j = 0; j<n; j++)
                B[i][j] = ans.m[i][j];

        for (int i = 0; i < n; i++)B[i][i] ++;
        int hehe = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
                add(hehe, B[i][j]);
        }
        pt(1 + hehe); puts("");
    }
    return 0;
}
/*
99
1 10
1 1

3 100000
3 1 2 3
3 1 2 3
3 1 2 3

5 3
5 1 2 3 4 5
4 2 3 4 5
3 1 3 5
5 1 2 3 4 5
3 1 2 3

*/
时间: 2024-11-08 19:40:33

hdu 5411 CRB and Puzzle 矩阵高速幂的相关文章

HDOJ 5411 CRB and Puzzle 矩阵高速幂

直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 133    Accepted Submission(s): 63 Problem Description CRB is now playing Jigsaw Puzzle. There

HDU - 5411 CRB and Puzzle 矩阵快速幂

HDU - 5411 考虑直接dp会T, 用矩阵优化一下就好了. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int>

hdu 5411 CRB and Puzzle (矩阵快速幂优化dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2.....M步的方案数. 分析:这题和 hdu5318 The Goddess Of The Moon差不多,就是多了一个等比数列求和. 代码: #include <cstdio> #include <iostream> #include <cstring> using name

HDOJ 5411 CRB and Puzzle 矩阵快速幂

直接构造矩阵,最上面一行加一排1.快速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 133    Accepted Submission(s): 63 Problem Description CRB is now playing Jigsaw Puzzle. There

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)

HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出k,b,n,M,问( f(g(0)) + f(g(1)) + ... + f(g(n)) ) % M的值. 分析: 把斐波那契的矩阵带进去,会发现这个是个等比序列. 推倒: S(g(i)) = F(b) + F(b+k) + F(b+2k) + .... + F(b+nk) // 设 A = {1,1,

HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)

CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 483    Accepted Submission(s): 198 Problem Description CRB is now playing Jigsaw Puzzle. There are  kinds of pieces with infinite

HDU 5411 CRB and Puzzle (2015年多校比赛第10场)

1.题目描写叙述:pid=5411">点击打开链接 2.解题思路:本题实际是是已知一张无向图.问长度小于等于m的路径一共同拥有多少条. 能够通过建立转移矩阵利用矩阵高速幂解决.当中,转移矩阵就是输入时候的邻接矩阵,同一时候多添加最后一列,都置为1.表示从i開始的,长度不超过M的路径的答案总数(最后一行的1~n列为全0行,能够理解为空集),那么把转移矩阵自乘M-1次后就是路径长度为M的转移矩阵(这里的路径长度指的是顶点的个数.顶点=边数+1,因此仅仅须要乘M-1次). 为何便于求和.能够设置

HDU 4965 Fast Matrix Calculation(矩阵高速幂)

题目大意:给你两个数字n和k,然后给你两个矩阵a是n*k的和b是k*n的,矩阵c = a*b,让你求c^(n*n). 直接求的话c是n*n的矩阵所以是1000*1000.会超时的啊. 能够转化一下:(a*b)^(n*n)=a*(b*a)^(n*n-1)*b.b*a能够得到一个k*k的矩阵,k非常小所以不会超时.高速幂一下就能够了啊. Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 13

HDU - 1588 Gauss Fibonacci (矩阵高速幂+二分求等比数列和)

Description Without expecting, Angel replied quickly.She says: "I'v heard that you'r a very clever boy. So if you wanna me be your GF, you should solve the problem called GF~. " How good an opportunity that Gardon can not give up! The "Prob