hdu 2254(矩阵)
题意:指定v1,v2,要求计算出在t1,t2天内从v1->v2的走法
思路:可以知道由矩阵求,即将其建图A,求矩阵A^t1 + ...... + A^t2. A^n后,/*A.xmap[v1][v2]即是从v1到v2要n步
所以先预处理出A^1 -A^10000的情况,后面再注意下细节,计算即可.
(每条道路走需要花一天的时间,且不能在某个城市停留,且t1=0时的走法数为0)
开始以为只要t1 = 0就输出0,结果不停WA,一直对照别人的代码- -
结果偶然发现这个特例,它喵的我也是醉了,才发现是题意理解错了,好惨...Orz
- 特例:
- Input:
- 1
- 1 1
- 1
- 1 1 0 1
- Ouput:
- 1
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<functional>
using namespace std;
typedef long long ll;
const int mod=2008;
const int maxn=5e5;
map<int,int>has;
struct Maxtri
{
int xmap[30][30];
};
int siz;
Maxtri mat[10005]; Maxtri Mul(Maxtri &a,Maxtri &b)
{
Maxtri c;
for(int i=0; i<siz; i++)
{
for(int j=0; j<siz; j++)
{
c.xmap[i][j]=0;
for(int k=0; k<siz; k++)
{
c.xmap[i][j]+=a.xmap[i][k]*b.xmap[k][j];
c.xmap[i][j]%=mod;
}
}
}
return c;
} int main()
{
int n,u,v,k; int v1,v2,t1,t2;
while(scanf("%d",&n) != EOF)
{
siz = 0;
memset(mat[0].xmap,0,sizeof(mat[0].xmap));
has.clear();
for(int i = 1; i <= n; i++)
{
scanf("%d%d",&u,&v);
if(has.find(u)==has.end())
{
has[u]=siz++;
}
if(has.find(v)==has.end())
{
has[v]=siz++;
}
mat[0].xmap[has[u]][has[v]] ++;
} for(int i=1; i<10001; i++)
mat[i]=Mul(mat[i-1],mat[0]);
scanf("%d",&k);
while(k--)
{
scanf("%d%d%d%d",&v1,&v2,&t1,&t2);
if(has.find(v1)==has.end()||has.find(v2)==has.end() || (!t1 && !t2))
{
printf("0\n");
continue;
}
if(t1 > t2)
swap(t1,t2); int ans=0;
for(int i=t1-1; i<t2; i++)
{
if(i == -1)
continue;
ans= (ans + mat[i].xmap[has[v1]][has[v2]])%mod;
}
printf("%d\n",ans%mod);
}
}
return 0;
}
hdu 2254(矩阵)的更多相关文章
- HDU 2254 奥运(矩阵高速幂+二分等比序列求和)
HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 依据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k ...
- HDU 2254
http://acm.hdu.edu.cn/showproblem.php?pid=2254 矩阵乘法两个经典问题的综合题,还要离散化和处理边界,好题啊好题 题意容易理解错,每一天是独立的,所以根据加 ...
- hdu 2254 奥运
点击打开hdu 2254 思路: 矩阵乘法 分析: 1 题目给定一个有向图,要求t1-t2天内v1-v2的路径的个数 2 根据离散数学里面的可达矩阵的性质,我们知道一个有向图的邻接矩阵的前n次幂的和即 ...
- hdu 4291 矩阵幂 循环节
http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109 ...
- HDU 2254 奥运(矩阵+二分等比求和)
奥运 [题目链接]奥运 [题目类型]矩阵+二分等比求和 &题解: 首先离散化城市,之后就是矩阵快速幂了,但让求的是A^(t1)+A^(t1+1)+...+A^(t2),我先想的是打表,但时间真 ...
- 【矩阵快速幂】之奥运 hdu 2254
1.城市的编号不是从0到n-1,而是随便的一个数字,需要离散化否则不能存相关信息 2.城市数不超过30,也就是说我的方法开矩阵不超过60,但是我残念的一开始以为最多可能有20000个不同城市 血 ...
- HDU 2254 奥运(数论+矩阵)
题目中文的不解释啊. .. 须要注意的就是:离散数学中,有向图的邻接矩阵A表示全部点之间路径长度为1的路径数量,A^n则表示路径长度为n的路径数量.故须要求某两点在(A^t1)~(A^t2)的路径数量 ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
随机推荐
- css变化代码2
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> ...
- 从Nest到Nesk -- 模块化Node框架的实践
文: 达孚(沪江Web前端架构师) 本文原创,转至沪江技术 首先上一下项目地址(:>): Nest:https://github.com/nestjs/nest Nesk:https://git ...
- 在wamp集成环境下安装laravel5.2.*框架
虽然官方一直强烈推荐使用homestead,但是这个相对麻烦一点,所以我还是选择使用wamp集成开发环境.还有这里我只讲解windows系统下的安装,其他例如mac或linux就不写了,此文章是面向刚 ...
- Gson解析Json数组
需求:从steam官网获取英雄数据,即为Json数据,并导入到本地数据库 Json数据是这样的 { "result": { "heroes": [ { &quo ...
- JAVA_SE基础——37.main方法的详解
主函数 大家都会写吧. 大家一直都不知道为何这样设计,这样设计有什么好处呢? 白话解释: main函数的修饰符是public: 公共的 为何不用private 等等的修饰符 而规定只用public呢? ...
- Linux "零拷贝" sendfile函数中文说明及实际操作
Sendfile函数说明 #include ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); sendfile ...
- SpringCloud用户自定义配置信息的定义和查看
一.概念 在SpringCloud项目中,用户自己定义的配置信息也可以放在application.*,需要以 info打头,以便使用公用基础设施 /info 查看! 本文讲解基于 ConfigServ ...
- Docker学习笔记 - Docker数据卷的备份和还原
学习目标: 备份数据卷 还原数据卷 # 通过容器备份数据卷容器中的数据卷 docker run --volumes-from dvt5 -v ~/backup:/backup --name dvt10 ...
- python网络爬虫与信息提取 学习笔记day3
Day3: 只需两行代码解析html或xml信息 具体代码实现:day3_1 注意BeautifulSoup的B和S需要大写,因为python大小写敏感 import requests r ...
- Python之格式化输出,初始编码以及运算符
一.题型 1.使用while循环输入 1 2 3 4 5 6 8 9 10 count = 0 while count < 10: count += 1 #count = count + ...