How many ways??

Sample Input

4 4  //n个点,m条路径
0 1  //s->t可通
0 2
1 3
2 3
2  //询问数
0 3 2  //从0到3走两条路可到的方案有多少种
0 3 3
3 6
0 1
1 0
0 2
2 0
1 2
2 1
2
1 2 1
0 1 3
0 0

Sample Output
2
0
1
3
 
矩阵快速幂,在离散数学课例听过类似的题目,n次幂就代表n条路方案。
 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
struct Matrix
{
LL mat[][];
}p;
int n,m;
int t,a,b,k;
Matrix mul(Matrix a,Matrix b)
{
Matrix c;
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
c.mat[i][j]=;
for(int k=;k<n;k++)
c.mat[i][j]=(c.mat[i][j]+a.mat[i][k]*b.mat[k][j])%;
}
}
return c;
}
Matrix mod_pow(Matrix x,int n)
{
Matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<;i++)
res.mat[i][i]=;
while(n)
{
if(n&)
res=mul(res,x);
x=mul(x,x);
n>>=;
}
return res;
}
int main()
{
freopen("in.txt","r",stdin);
while(cin>>n>>m)
{
if(n==&&m==)
break;
memset(p.mat,,sizeof(p.mat));
int s,t;
for(int i=;i<m;i++)
{
cin>>s>>t;
p.mat[s][t]=;
}
cin>>t;
while(t--)
{
cin>>a>>b>>k;
Matrix ans=mod_pow(p,k);
/* for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
cout<<ans.mat[i][j]<<" ";
cout<<endl;
}*/
cout<<ans.mat[a][b]<<endl;
}
}
return ;
}

How many ways??(HDU 2157)的更多相关文章

  1. 2道acm编程题(2014):1.编写一个浏览器输入输出(hdu acm1088);2.encoding(hdu1020)

    //1088(参考博客:http://blog.csdn.net/libin56842/article/details/8950688)//1.编写一个浏览器输入输出(hdu acm1088)://思 ...

  2. 2013 多校联合 F Magic Ball Game (hdu 4605)

    http://acm.hdu.edu.cn/showproblem.php?pid=4605 Magic Ball Game Time Limit: 10000/5000 MS (Java/Other ...

  3. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  4. (多线程dp)Matrix (hdu 2686)

    http://acm.hdu.edu.cn/showproblem.php?pid=2686     Problem Description Yifenfei very like play a num ...

  5. War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...

  6. 2012年长春网络赛(hdu命题)

    为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...

  7. BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

    Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  8. BestCoder Round #68 (div.2) geometry(hdu 5605)

    geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  9. 2013多校联合2 I Warm up 2(hdu 4619)

    Warm up 2 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

随机推荐

  1. XP系统显示文件夹选项属性被删除解决注册表

    Title:XP系统显示文件夹选项属性被删除解决注册表 -- 2010-11-18 13:17 昨天中了个毒窝,还有恶意软件,十分不爽. 开始恶意软件把显示所有文件夹给删除了,去找了个注册表,重新注册 ...

  2. CPU天梯图

    一幅图,了解CPU

  3. Effective C++笔记(一)——条款26-29

    条款26:尽可能延后变量定义式的出现时间 为何要尽量延后? 当程序中途跳出而导致变量未被使用,但是必须进行构造和析构. 最佳初始化变量 直接在构造时指定初值比构造之后再赋值效率高(条款4) ... s ...

  4. Delphi default属性

    Delphi default属性 Delphi控件指定属性的时候可以加上Default关键字,例如property Color default clBtnface.一直以为这个是构造类的时候的默认值, ...

  5. C# 同步/并发队列ConcurrentQueue (表示线程安全的先进先出 (FIFO) 集合)

    http://msdn.microsoft.com/zh-cn/library/dd267265(v=vs.110).aspx static void Main(string[] args) { // ...

  6. PC格局已改变 联想未来的短板在哪里?

    PC格局已改变 联想未来的短板在哪里? 作者:孙永杰2013-11-13 11:34评论:15   [PConline 品科技](原文标题:PC格局已改:联想未来的短板在哪里?)近日,联想集团公布了截 ...

  7. 《图解HTTP》 第11章 web的攻击技术

    11.1 针对Web的攻击技术 简单的HTTP协议本身并不存在安全性问题,所以协议本身几乎不会成为攻击的对象. 11.1.1 HTTP不具备必要的安全功能 11.1.2 在客户端即可篡改请求 在HTT ...

  8. ik分词

    Ik中文分词的引入 ik版本号:IK Analyzer 2012FF_hf1 下载地址: http://code.google.com/p/ik-analyzer/downloads/list 解压 ...

  9. Gradle version 2.10 is required. Current version is 2.8.

    今天在网上下载了EasyPusher-master文件,日期是20160527版本的,但是在我的Android Studio上运行时报错: Error:(1, 1) A problem occurre ...

  10. poj 1089 Intervals

    http://poj.org/problem?id=1089 Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...