矩阵快速幂锦上添花小结

来源:互联网 发布:大数据人才缺口 编辑:程序博客网 时间:2024/05/17 01:46

又做了一个矩阵的专题。。表示又有更深的理解了。。

专题链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=60916#overview 密码nyist


剩下C、F、G、R表示接下来搞吧。。不想在写了。。

C题是一个组合数学+矩阵快速幂。F是一个不知道什么意思的题。。G是dp+矩阵快速幂。。R是一个Little Keng。。就想他的题目名称一样。小坑。。


其他题给一些简单的题解。。

A题是问你有多少个指向上方的三角形有多少个。。我们手动画几个可以发现。。上一个向上的三角形可以产生3个向上的三角形和1个向下的三角形。。同样一个向下的三角形可以产生1个向上的三角形和3个向下的三角形。。这样状态转移矩阵就可以出来了。。

A ={

 1, 3

 3, 1

}

水水的题目。。


B题就是广义斐波那契数列求第n项。。。水水更健康。。


D题题意:


The bear came out to walk across the field. At the beginning of the walk his speed is(dx, dy). Then the bear spends exactlyt seconds on the field. Each second the following takes place:

  • Let's suppose that at the current moment the bear is in cell(x, y).
  • First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry fromk bushes, he increases each component of his speed byk. In other words, if before eating the k bushes of raspberry his speed was(dx, dy), then after eating the berry his speed equals(dx + k, dy + k).
  • Let's denote the current speed of the bear (dx, dy) (it was increased after the previous step). Then the bear moves from cell(x, y) to cell(((x + dx - 1) mod n) + 1, ((y + dy - 1) mod n) + 1).
  • Then one additional raspberry bush grows in each cell of the field.

You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell(sx, sy). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.

翻译一下:

那个熊要穿过那个领域。一开始,他的速度为(dx, dy)。然后,熊度过了正好t秒在那个领域上。每秒发生下面的:

1.我们假设一开始熊在(x, y)这个位置。

2.那个熊先吃木莓现在在的那个更位置。当那个熊吃了k bushes,他的速度在每个方向增加了k。话句话说,如果他吃木莓之前的速度为(dx, dy),那么他吃完之后的速度为(dx + k, dy + k)。

3.我们用(dx, dy)代表现在的速度。那么熊要从(x, y) 到( (x + dx - 1) % n + 1, (y + dy - 1) % n + 1)。

4.然后,额外的一个木莓生生长出来在每个位置。。

你的任务是预测那个熊的行为。找出他的位置在最后的时候。假设初始的时候有足够多的木莓,并且那个熊不会吃完它。

自己理解的题意:

(x, y) 为现在的位置,下一秒会到达(x + dx , y + dy)。。这个(dx, dy)    为速度变化后的速度。。这一点还是需要注意的。。

如果连先后顺序都不知道的,还搞什么acm!!!!

当然,我们把领域的1 - n 可以映射到的 0 -  n - 1    就可以直接对n取模就好了。。。

状态转移矩阵也是比较好推的。。。

x = (2 * x + y + dx),

y = (x + 2 * y + dy),

dx = (dx + x + y + t),

dy = (dy + x + y + t).

简单写了一下推到状态矩阵的思路。。。矩阵就直接推了。。。


E题意给你a + b , ab。让你求a^n + b^n。。这个就需要推一下啦。。同过别人的提示,还是顺利的推出来了。。

设x = a + b, y = ab

f1 = x, f2 = x^2 - 2 * y, f3 = f2 * x - f1 * y, f4 = f3 * x - f2 *  y....具体怎么推出来的。。我们还是要手动模拟一下。。

我发现。- - 有些规律直接就可以手动推出来的。。在你推的过程中,规律也就出来了。。。

继续说这个东西。。规律也就看出来了fn = fn-1 * x - fn-2 * y

转移矩阵也就就出来了。。

A ={

   x, 1

   -y, 0

}

推出规律也就水水的出了。。


G题是一个dp+矩阵快速幂。。表示看了题解也是不太了解。。。dp造诣基本没有。。


H竟然不是矩阵,好吧。。欧拉函数。。模板上去,水水的就过了。


I 就是求广义斐波那契数列的后m为数。1<= m<= 4。。。水水的。。


J题看了很长时间的题意就是不知道什么意思。。上网搜了一下题目意思。。。

翻译:

The fibonacci number is defined by the following recurrence:

  • fib(0) = 0
  • fib(1) = 1
  • fib(n) = fib(n-1)+fib(n-2)

But we're not interested in the fibonacci numbers here. We would like to know how many calls does it take to evaluate then th fibonacci number if we follow the given recurrence. Since the numbers are going to be quite large, we'd like to make the job a bit easy for you. We'd only need the last digit of the number of calls, when this number is represented in baseb.


但是,我们这次对斐波那契数列是不感兴趣的。我们想要知道第n个斐波那契数他调用了多少次的函数。。因为次数可能是非常大的,我们想要使得这个任务更容易。。我们需要的这个次数在b进制下的最后一位。

然后就可以水水的过了。。

他调用的次数就是发fn = 2 * Fn - 1。。

水水的就过了。。

在期间WA了n多发后发现输出结果错了。。

printf("%lld %d %d\n", n, mod, (2 * ans.a[1][1] - 1) % mod);
这是错误的。。为什么? 因为 - 1 的存在。。。
cout << n << ' ' << mod << ' ' << (2 * ans.a[1][1] - 1 + mod) % mod << endl;
这就是对的。。。

PSSS:只要取模设计减法,必须+mod % mod。这样就是避免了取模出现负数的情况。。


K 广义斐波那契数列。。Fn = Fn-1 + Fn -2 + Fn-3。。。水水的。。。


L广义斐波那契数列。。。水水的。。


M题意n个人一排,m个数(1 - m),每个人选一个数,如果相邻人选的数相同的话,那么这个数必须> k。。

表示,这个是一个典型利用矩阵快速幂求解递推值的加速。。

可以推出来。。

dp[i][0]表示第i个人选择的是一个大于k的数。。dp[i][1]表示第i个人选择的是一个小于k的数。。

dp[i][0] = dp[i][0] * m - k + dp[i][1] * (m - k)。。

dp[i][1] = dp[i][0] * k + dp[i][1] * (k - 1)。。

ans = dp[n][0] + dp[n][1]。。

这样的递推就可以用矩阵快速幂来加速。。

A ={

    m - k, k

    m - k, k - 1

}。。

水水的。。


N以前写过。。


Ohttp://blog.csdn.net/u011394362/article/details/40370555      题解。。也是写过的。。


Pn个物种会进化。。一个物种到另一个物种的转化率为p。。如果一个物种没有的转化的话也就是说自身转化率为1。。否则为1 - p。。

比较有点小坑的就是。。Round your final result to an integer. 学到了一句话。。这句话的意思是四舍五入你的结果到一个整数。。学习了。。

这样,根据给的转化率推出来每两两的物种的转化率。然后就ok了。水水的。。。。


Q和P题很是类似。。


R一个逗比题。。不解释。。



总结:又一个专题的题目a完了。。对其有了更深的理解了。。

1.一般递推是可以用矩阵快速幂来进行加速的。。

2.对矩阵的认识不仅仅从数据范围上来进行判定了。。。更是对于状态转化的认识。。。

3.大型比赛应该不会出这种比较裸的了。。应该是算法柔和在一起来出的,有时间就可以练习一下这种题目。。





0 0
原创粉丝点击