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. Android 检查是否安装SD卡

    /** * 检查是否安装SD卡 * @return */ public static boolean checkSaveLocationExists() { String sDCardStatus = ...

  2. RAW碰到的问题与功能的测试

    碰到的问题太多: 1. 没人告诉我,应该在CreateFile之前,先InstallDriver2. 签名死活不行(好像是反斜杠问题)3. 注册码错误4. 怀疑ANSI与Unicode注册码的问题5. ...

  3. git设置过滤忽略的文件或文件夹

    我们一般向代码仓库提交项目的时候,一般需要忽略编译生成的中间文件以及文件夹的提交,因为它们是无用的,而且也会占用仓库的空间.一般只用提交.pro,.sln,makefile,程序源文件等编译必须用到的 ...

  4. BZOJ3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 17[Sub ...

  5. Android ViewPager实现软件的第一次加载的滑动效果

    public class MainActivity extends Activity { private ViewPager viewPager; private List<View> V ...

  6. MapReduce优化一(改变切片大小和Shuffle过程Reduce占用堆大小)

    /*为防止处理超大作业时超时,将io时间设为1小时         *         <property>            <name>dfs.datanode.soc ...

  7. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  8. 一条sql

    包含全部:SELECT u.id,u.user_name,usa.fixed_amount,d.uloadtype,d.min_loan_money FROM fanwe_user_autobid u ...

  9. SecureCRT7.3和SecureFX7.3的MAC下破解

    破解脚本:http://files.cnblogs.com/files/jieyuefeng/SecureCRTFX_mac_crack.zip 破解方法: sudo perl ~/Downloads ...

  10. [深入react] 4.牛逼闪闪的虚拟DOM

    React.createElement嵌套后的结果就是虚拟dom,虚拟dom听着很高端,其实就是一个json,类似: { type:'div', props:{ className:"box ...