【HDU 2157】 How Many Ways??
【题目链接】
【算法】
设A[i][j]为走一条边,从i走到j的方案数
C[i][j]为走两条边,从i走到j的方案数,显然有 : C = A * A = A^2
C'[i][j]为走三条边,从i走到j的方案数,那么 : C' = C * A = (A * A) * A = A^3
.......
因此,要求走n条边的方案数,只需通过矩阵乘法快速幂,计算A^n就可以了
【代码】
注意,一个n阶矩阵的零次幂是n阶单位阵(对角线上的都是1,其余都是0)
#include<bits/stdc++.h>
using namespace std;
#define MAXN 25
const int MOD = ; int n,m,s,t,a,b,k;
struct Matrix
{
int mat[MAXN][MAXN];
} c,ans; inline void multipy(Matrix &a,Matrix b)
{
int i,j,k;
Matrix ans;
memset(ans.mat,,sizeof(ans.mat));
for (i = ; i < n; i++)
{
for (j = ; j < n; j++)
{
for (k = ; k < n; k++)
{
ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
}
}
}
a = ans;
} inline Matrix power(Matrix a,int n,int s)
{
int i,j;
Matrix ans,p = a;
for (i = ; i < s; i++)
{
for (j = ; j < s; j++)
{
ans.mat[i][j] = (i == j);
}
}
while (n > )
{
if (n & ) multipy(ans,p);
multipy(p,p);
n >>= ;
}
return ans;
} int main()
{ while (scanf("%d%d",&n,&m) != EOF)
{
if (!n && !m) break;
memset(c.mat,,sizeof(c.mat));
while (m--)
{
scanf("%d%d",&s,&t);
c.mat[s][t] = ;
}
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d",&a,&b,&k);
ans = power(c,k,n);
printf("%d\n",ans.mat[a][b]);
}
} return ;
}
【HDU 2157】 How Many Ways??的更多相关文章
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
- 【HDU 4699】 Editor
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...
随机推荐
- tomcat时间与系统时间不一致问题
我在部署应用到centos系统上的tomcat服务器中运行,发现操作系统的时间和tomcat中的访问日志的时间与系统时间不一致,但是查看当前操作系统的时区也是CST时区(中国标准时区). 查看系统的时 ...
- lucene-5.3.1配置(win7x64)
lucene下载地址:http://www.us.apache.org/dist/lucene/java/5.3.1/lucene-5.3.1.zip 下载之后解压 控制台应用程序下配置: 找到luc ...
- 23Spring使用JdbcTemplate和JdbcDaoSupport
首先需要添加c3p0包和jdbc包 数据库: CREATE DATABASE IF NOT EXISTS `spring` /*!40100 DEFAULT CHARACTER SET utf8 */ ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
- Spring 和 Hibernate的整合
问题 ,spring 和 hibernate 整合 如何整合 1. Spring 使用Hibernate的的SessionFactory 2. Hibernate使用Spring提供的声明式事务
- java手工从键盘输入数字存放到数组并将其输出
package suanfafenxi; import java.util.Scanner; public class shiyan { static int number=10; static in ...
- HDU 4035 期望dp
这道题站在每个位置上都会有三种状态 死亡回到起点:k[i] 找到出口结束 e[i] 原地不动 p[i] k[i]+e[i]+p[i] =1; 因为只给了n-1条路把所有都连接在一起,那么我们可以自然的 ...
- abs 暴力
Given a number x, ask positive integer y≥2y≥2, that satisfy the following conditions: 1. The absolut ...
- Spring Cloud体系实现标签路由
如果你正在使用Spring Cloud体系,在实际使用过程中正遇到以下问题,可以阅读本文章的内容作为后续你解决这些问题的参考,文章内容不保证无错,请务必仔细思考之后再进行实践. 问题: 1,本地连上开 ...
- Elasticsearch学习系列之配置文件详解
################################### Cluster ################################### #定义集群名称,默认是elasticse ...