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. Eric Pement的单行awk命令收集

    超实用的. awk1line_zh-CN.txt 可惜中文版的网址好像不能访问了.. HANDY ONE-LINE SCRIPTS FOR AWK 30 April 2008 Compiled by ...

  2. python常用数据结构的常用操作

    作为基础练习吧.列表LIST,元组TUPLE,集合SET,字符串STRING等等,显示,增删,合并... #===========List===================== shoplist ...

  3. 天底下最简单的QT画图板,就一个类,60行代码

    简单直观.但是我有个问题是,这实际上不是在绘制直线,而是几千几万个超级短的“直线”,这样会不会效率很低呢? 注意,每次绘制的时候,需要一支笔,这支笔需要设置颜色和宽度(就像我们平时写字也要稍微挑一下笔 ...

  4. #pragma execution_character_set的意义

    就是设置执行字符集,指示char的执行字符集是UTF-8编码.如果源文件中出现中文,必须要设置为 #if _MSC_VER >= 1600        #pragma execution_ch ...

  5. GS1011无线模块的使用简介。

    一.硬件说明: 只是用电脑的串口助手与之通信,利用了max232进行电平转换.是用模块的UART0作为通信接口. 模块引脚 接点 说明   1.17.32.48 GND 模块地   9 3.3V VB ...

  6. mysql System Tablespace

    System Tablespace 数据文件配置: mysql> show variables like '%innodb_data_file_path%'; +---------------- ...

  7. Android 5.0 之SwipeRefreshLayout

    金田 下拉刷新是一种比较常用的效果,Android 5.0之前官方并未提供类似的控件,App中主要是用的第三方库,例如PullToRefresh,ActionBar-PullToRefresh等.刚好 ...

  8. C#中判断字符串是否中文的方法

    public bool IsChinaString(string CString) { bool BoolValue = false; ; i < CString.Length; i++) { ...

  9. (总结)工作中常用的js自定义函数——日期时间类

    //设置时间类 var Wsdatatime = function(){ this.today = (new Date()).getTime(); //当前时间 } Wsdatatime.protot ...

  10. [LeetCode] 160. Intersection of Two Linked Lists 解题思路

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...