经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
BZOJ1898: [Zjoi2005]Swamp 沼泽鳄鱼(矩阵快速幂)
来源:cnblogs  作者:自为风月马前卒  时间:2018/11/28 9:54:24  对本文有异议

题意

题目链接

Sol

不难发现吃人鱼的运动每\(12s\)一个周期

所以暴力建12个矩阵,放在一起快速幂即可

最后余下的部分暴力乘

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int MAXN = 52, mod = 10000;
  4. inline int read() {
  5. char c = getchar(); int x = 0, f = 1;
  6. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  7. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  8. return x * f;
  9. }
  10. int N, M, S, T, K, mp[MAXN][MAXN], pos[MAXN];
  11. int add(int x, int y) {
  12. if(x + y < 0) return x + y + mod;
  13. return x + y >= mod ? x + y - mod : x + y;
  14. }
  15. int add2(int &x, int y) {
  16. if(x + y < 0) x = x + y + mod;
  17. else x = (x + y >= mod ? x + y - mod : x + y);
  18. }
  19. int mul(int x, int y) {
  20. return 1ll * x * y % mod;
  21. }
  22. struct Ma {
  23. int m[MAXN][MAXN];
  24. Ma() {
  25. memset(m, 0, sizeof(m));
  26. }
  27. Ma operator * (const Ma &rhs) const {
  28. Ma ans;
  29. for(int k = 1; k <= N; k++)
  30. for(int i = 1; i <= N; i++)
  31. for(int j = 1; j <= N; j++)
  32. add2(ans.m[i][j], mul(m[i][k], rhs.m[k][j]));
  33. return ans;
  34. }
  35. }a[15], bg;
  36. Ma MatrixFp(Ma a, int p) {
  37. Ma base;
  38. for(int i = 1; i <= N; i++) base.m[i][i] = 1;
  39. while(p) {
  40. if(p & 1) base = base * a;
  41. a = a * a; p >>= 1;
  42. }
  43. return base;
  44. }
  45. int main() {
  46. N = read(); M = read(); S = read() + 1; T = read() + 1; K = read();
  47. for(int i = 1; i <= M; i++) {int x = read() + 1, y = read() + 1; bg.m[x][y]++; bg.m[y][x]++;}
  48. int fn = read();
  49. for(int i = 1; i <= 12; i++) a[i] = bg;
  50. for(int i = 1; i <= fn; i++) {
  51. int num = read();
  52. for(int j = 1; j <= num; j++) pos[j] = read() + 1;
  53. for(int j = 1; j <= 12; j++)
  54. for(int k = 1; k <= N; k++)
  55. a[j].m[k][pos[j % num + 1]] = 0;
  56. }
  57. Ma res = a[1];
  58. //for(int i = 1; i <= N; i++) res.m[i][i] = 1;
  59. for(int i = 2; i <= 12; i++) res = res * a[i];
  60. res = MatrixFp(res, K / 12);
  61. for(int i = 1; i <= K % 12; i++) res = res * a[i];
  62. printf("%d\n", res.m[S][T]);
  63. return 0;
  64. }
  65. /*
  66. 6 8 1 5 333
  67. 0 2
  68. 2 1
  69. 1 0
  70. 0 5
  71. 5 1
  72. 1 4
  73. 4 3
  74. 3 5
  75. 3
  76. 3 0 5 1
  77. 2 1 2
  78. 4 1 2 3 4
  79. */
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号