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. 转 Jona Dany 一个20年架构师程序员的经验总结

    1.估算问题解决所需要的时间,为自己定一个时间限制,1小时,30分钟,15分钟.如果这期间不能解决问题,那就去寻求帮助.不要做超级堆码员. 2.编程语言是一种语言,只是一种语言.只要理解一种语言的原理 ...

  2. JAVA中,字符串STRING与STRINGBUILDER的效率差异

    如果可变字符串操作较多的话,用STRINGBUILDER显然优势得多. public class HelloJava { public static void main(String[] args) ...

  3. Linux企业级项目实践之网络爬虫(28)——爬虫socket处理

    Socket是进程之间交换数据的机制.这些进程即可以是同一台机器上的,也可以是通过网络连接起来的不同机器.一旦一个Socket连接建立,那么数据就能够双向传输,直到其中一端关闭连接. 通常,请求数据的 ...

  4. 就是一段程序,可以求出N个不等长列表中取N个元素形成的所有组合

    def get_result_in_vector(vector, N, tmp, tmp_result): """ :param vector:所有组合的拼接 :para ...

  5. Appnium+python实现手势密码为什么总是报错

    最近一直在尝试Appnium实现Android手机自动化测试,一直一直卡在一个点上,那就是手势密码,因为所测应用的手势密码使用的不是单个的imageview实现的手势密码解锁窗,所以只能靠坐标点来定位 ...

  6. win7配置简单的FTP服务器

    1.开启一些win7默认是关闭状态的功能 步骤:控制面板 -> 程序和功能 -> 打开或关闭Windows功能,然后勾选下图中圈起来的部分: 2.添加FTP站点 步骤:桌面的计算机图标 - ...

  7. 关于linux内存使用情况

    从操作系统角度看: 系统物理内存7.5g 目前已使用7.4g(包含14m的buffer和1.6g的cache) 剩余可用内存128m 从应用程序角度看: 目前已使用5.8g 剩余可用内存1.7g(因为 ...

  8. Filebeat中文指南

    Filebeat中文指南 翻译自:https://www.elastic.co/guide/en/beats/filebeat/current/index.html 译者:kerwin 鸣谢:tory ...

  9. linux权限及ntfs文件系统权限的知识

    关于ntfs权限的问题 文件的权限: [-dcbps][u:rwx][g:rwx][a:rwx] 当中: r=4, w=2, x=1,  u=owner, g=group, a=all user   ...

  10. android媒体--stagefright概述【一】

    近期杂七杂八的忙碌着,前几天看了下这部分主要是stagefright模块的,所以更改下名字 做了挺长时间的android平台的媒体开发,对之前的分析进行一个阶段性的总结. 一.android结构图(上 ...