http://acm.hdu.edu.cn/showproblem.php?pid=2157

题解:经典矩阵算法。把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,假设要求经过k步的路径数,我们仅仅须要二分求出A^k就可以。

参考文章:矩阵快速幂

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int s[25][25];
int b[25][25];
int a[25][25];
char str;
struct node{};
void Matrix(int a[25][25],int b[25][25]){int c[25][25];memset(c,0,sizeof(c));for(int i=0;i<n;i++){for(int j=0;j<n;j++){for(int k=0;k<n;k++){c[i][j]=(c[i][j]+a[i][k]*b[k][j])%1000;}}}for(int i=0;i<n;i++){for(int j=0;j<n;j++){a[i][j]=c[i][j];}}
}
int power(int A,int B,int k){for(int i=0;i<n;i++){for(int j=0;j<n;j++){a[i][j]=(i==j);b[i][j]=s[i][j];}}while(k){if(k&1)Matrix(a,b);Matrix(b,b);k>>=1;}return a[A][B];
}
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//while(~scanf("%d%d",&n,&m)&&n+m){memset(s,0,sizeof(s));for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);s[u][v]=1;}scanf("%d",&t);for(int i=1;i<=t;i++){scanf("%d%d%d",&u,&v,&k);cout<<power(u,v,k)<<endl;}}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif//cout << "Hello world!" << endl;return 0;
}

How many ways??相关推荐

  1. (转) 6 ways of mean-centering data in R

    6 ways of mean-centering data in R 怎么scale我们的数据? 还是要看我们自己数据的特征. 如何找到我们数据的中心? Cluster analysis with K ...

  2. 4 Ways to Make Sure You Always Have Something to Say Online

    4 Ways to Make Sure You Always Have Something to Say Online By Yasmin Bendror,Published November 9, ...

  3. HDOJ How many ways?? 2157【矩阵高速幂】

    How many ways? ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. Cocos2d:使用 CCCamera 做滚动效果 (Four Ways of Scrolling with Cocos2D)

    原版的:http://www.koboldtouch.com/display/IDCAR/Four+Ways+of+Scrolling+with+Cocos2D There are two class ...

  5. ASP.NET MVC:4 Ways To Prevent Duplicate Form Submission(转载)

    原文地址:http://technoesis.net/prevent-double-form-submission/. Double form submission in a multi-user w ...

  6. Ten ways to improve the performance of large tables in MySQL--转载

    原文地址:http://www.tocker.ca/2013/10/24/improving-the-performance-of-large-tables-in-mysql.html Today I ...

  7. Five ways to maximize Java NIO and NIO.2--转

    原文地址:http://www.javaworld.com/article/2078654/java-se/java-se-five-ways-to-maximize-java-nio-and-nio ...

  8. leetcode面试准备:Decode Ways

    1 题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

  9. [LeetCode] Decode Ways

    (Version 0.0) Decode Ways这道题从原理上说是一个比较简单的一维DP题目,用一个一维数组的元素dp[i] (i >= 1)来记录从头开始长度为i的substring有多少种 ...

  10. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

最新文章

  1. cocos2d-x中几种存储数据的方式
  2. js网页返回页面顶部的小方法
  3. 互联网公司为啥不使用mysql分区表?
  4. 数据结构+算法面试100题~~~摘自CSDN,作者July
  5. etag flag setting in metadata MPC_EXT
  6. 转:Redis 应用案例 - 在问题中不断成长
  7. mysql 测试快生产慢_兴奋!阿里三位扫地憎专家总结的Mysql性能优化金字塔法则,操作细节满分...
  8. mac 生成公钥和私钥
  9. Activiti7 + Spring Boot + mybatis Plus + Oracle 数据库整合-学习篇(一)
  10. deepin任务栏消失解决方法
  11. 【渝粤教育】电大中专营销策划原理与实务答案作业 题库
  12. JavaSE方法(构造方法)与方法重载基础练习题
  13. 汉字 计算机 坟墓,让汉字跨越数字化鸿沟
  14. 约瑟夫问题(小小算法,真不可笑)
  15. Android--高效地加载大图片
  16. 周易六十四卦——蹇卦
  17. 【成为架构师课程系列】架构师的核心能力地图
  18. 微金所案例总结——Bootstrap应用模板引擎的使用
  19. 知识产权创造美好生活
  20. python小练习--GUI基础

热门文章

  1. 软件工程与项目管理的关系_PM:软件项目管理概要
  2. python读取xlsx文件pandas_python-如何使用iPython中的pandas库读取.xlsx文件?
  3. 浅谈Java解决鸡兔同笼问题的思路
  4. wannacry作者捉到了吗_不会阅读空气是情商低吗?如何提升自己的情商?
  5. 惠普m180n故障码04_自动变速器挡位故障:挂前进挡或倒挡都不能行驶;不能升挡...
  6. 曲线均匀分布_R——概率统计与模拟(三) 变换均匀分布对特定分布进行抽样
  7. devc++源文件未编译_悬赏万元,重现「木兰」编程语言编译器
  8. Satwe楼板能用弹性模计算吗_现浇楼板淋水后却出现裂缝,还好老师傅有经验,多是这3点造成的...
  9. 【小白学习PyTorch教程】十五、BERT:通过PyTorch来创建一个文本分类的Bert模型
  10. 【机器学习算法专题(蓄力计划)】二十、实操代码MNIST 数据集