https://www.hackerrank.com/contests/w27/challenges/hackonacci-matrix-rotations

一开始是没想到观察题的。只想到直接矩阵快速幂。

但是超时了,因为我的矩阵快速幂是应对稀疏矩阵的,

http://www.cnblogs.com/liuweimingcprogram/p/5942591.html

因为当时做这题的时候,以前的矩阵快速幂模板超时,所以就换了个,但是这题用稀疏矩阵的模板会超时,所以就把两个模板都收录算了。但是时间是1.58s,现在是pertest。所以后面的可能会超时,然而这题应该直接观察,其实也不算观察,算数学吧。鸽笼原理

前3项固定,最多1/2 * 1/2 * 1/2 项后,就会和前3项重复。

然后算一算,就发现了重复了。

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <assert.h>
  7. #define IOS ios::sync_with_stdio(false)
  8. using namespace std;
  9. #define inf (0x3f3f3f3f)
  10. typedef long long int LL;
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. #include <vector>
  15. #include <set>
  16. #include <map>
  17. #include <queue>
  18. #include <string>
  19. int n;
  20. const int maxn = + ;
  21. char str[maxn][maxn];
  22. int ans[];
  23. int f[] = {, , , , , , , };
  24. void init() {
  25. for (int i = ; i <= n; ++i) {
  26. for (int j = i; j <= n; ++j) {
  27. if (i == && j == ) continue;
  28. LL val = (1LL * i * j) * (1LL * i * j);
  29. val %= ;
  30. if (val == ) val = ;
  31. if (f[val] == ) {
  32. str[i][j] = 'Y';
  33. } else str[i][j] = 'X';
  34. }
  35. // cout << endl;
  36. }
  37. for (int i = ; i <= n; ++i) {
  38. for (int j = ; j <= i - ; ++j) {
  39. str[i][j] = str[j][i];
  40. }
  41. }
  42. // for (int i = 1; i <= n; ++i) {
  43. // for (int j = 1; j <= n; ++j) {
  44. // cout << str[i][j];
  45. // }
  46. // cout << endl;
  47. // }
  48. //
  49. int p1 = n, p2 = ;
  50. for (int i = ; i <= n; ++i) {
  51. p1 = n;
  52. p2 = i;
  53. for (int j = ; j <= n; ++j) {
  54. if (str[i][j] != str[p1--][p2]) {
  55. ans[]++;
  56. }
  57. }
  58. p1 = n - i + ;
  59. p2 = n;
  60. for (int j = ; j <= n; ++j) {
  61. if (str[i][j] != str[p1][p2--]) {
  62. ans[]++;
  63. }
  64. }
  65. p1 = n - i + ;
  66. for (int j = ; j <= n; ++j) {
  67. if (str[i][j] != str[p1][j]) {
  68. ans[]++;
  69. }
  70. }
  71. }
  72.  
  73. }
  74. void work() {
  75. int q;
  76. scanf("%d%d", &n, &q);
  77. init();
  78. while (q--) {
  79. int x;
  80. scanf("%d", &x);
  81. // cin >> x;
  82. x %= ;
  83. x /= ;
  84. // cout << ans[x] << endl;
  85. printf("%d\n", ans[x]);
  86. }
  87. }
  88.  
  89. int main() {
  90. #ifdef local
  91. freopen("data.txt", "r", stdin);
  92. // freopen("data.txt", "w", stdout);
  93. #endif
  94. work();
  95. return ;
  96. }

观察,0ms

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <assert.h>
  7. #define IOS ios::sync_with_stdio(false)
  8. using namespace std;
  9. #define inf (0x3f3f3f3f)
  10. typedef long long int LL;
  11.  
  12. #include <iostream>
  13. #include <sstream>
  14. #include <vector>
  15. #include <set>
  16. #include <map>
  17. #include <queue>
  18. #include <string>
  19. int n;
  20. const int maxn = + ;
  21. char str[maxn][maxn];
  22. struct Matrix {
  23. LL a[][];
  24. int row;
  25. int col;
  26. };
  27. //struct Matrix matrix_mul(struct Matrix a, struct Matrix b, int MOD) {
  28. // struct Matrix c = {0};
  29. // c.row = a.row;
  30. // c.col = b.col;
  31. // for (int i = 1; i <= a.row; ++i) {
  32. // for (int k = 1; k <= a.col; ++k) {
  33. // if (a.a[i][k]) {
  34. // for (int j = 1; j <= b.col; ++j) {
  35. // c.a[i][j] += a.a[i][k] * b.a[k][j];
  36. //// c.a[i][j] = (c.a[i][j] + MOD) % MOD;
  37. // }
  38. // c.a[i][j] %= MOD;
  39. // }
  40. // }
  41. // }
  42. // return c;
  43. //}
  44. struct Matrix matrix_mul (struct Matrix a, struct Matrix b, int MOD) {
  45. struct Matrix c = {};
  46.  
  47. c.row = a.row;
  48. c.col = b.col;
  49. for (int i = ; i <= a.row; i++) {
  50. for (int j = ; j <= b.col; j++) {
  51. for (int k = ; k <= b.row; k++) {
  52. c.a[i][j] += a.a[i][k] * b.a[k][j];
  53. }
  54. c.a[i][j] = (c.a[i][j] + MOD) % MOD;
  55. }
  56. }
  57. return c;
  58. }
  59.  
  60. struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, LL n, int MOD) {
  61. while (n) {
  62. if (n & ) {
  63. ans = matrix_mul(ans, base, MOD);
  64. }
  65. n >>= ;
  66. base = matrix_mul(base, base, MOD);
  67. }
  68. return ans;
  69. }
  70. int ans[];
  71. void init() {
  72. struct Matrix A;
  73. A.row = ;
  74. A.col = ;
  75. A.a[][] = ;
  76. A.a[][] = ;
  77. A.a[][] = ;
  78. struct Matrix B;
  79. B.col = B.row = ;
  80. B.a[][] = ;
  81. B.a[][] = ;
  82. B.a[][] = ;
  83. B.a[][] = ;
  84. B.a[][] = ;
  85. B.a[][] = ;
  86. B.a[][] = ;
  87. B.a[][] = ;
  88. B.a[][] = ;
  89. str[][] = 'Y';
  90. // cout << "1" << " ";
  91. for (int i = ; i <= n; ++i) {
  92. for (int j = i; j <= n; ++j) {
  93. if (i == && j == ) continue;
  94. LL val = (1LL * i * j) * (1LL * i * j);
  95. // cout << val << " ";
  96. struct Matrix t = quick_matrix_pow(A, B, val - , );
  97. // cout << B.a[2][3] << " ";
  98. if (t.a[][] & ) {
  99. str[i][j] = 'Y';
  100. } else str[i][j] = 'X';
  101. // cout << t.a[1][3] << " ";
  102. }
  103. // cout << endl;
  104. }
  105. for (int i = ; i <= n; ++i) {
  106. for (int j = ; j <= i - ; ++j) {
  107. str[i][j] = str[j][i];
  108. }
  109. }
  110. // for (int i = 1; i <= n; ++i) {
  111. // for (int j = 1; j <= n; ++j) {
  112. // cout << str[i][j];
  113. // }
  114. // cout << endl;
  115. // }
  116. //
  117. int p1 = n, p2 = ;
  118. for (int i = ; i <= n; ++i) {
  119. p1 = n;
  120. p2 = i;
  121. for (int j = ; j <= n; ++j) {
  122. if (str[i][j] != str[p1--][p2]) {
  123. ans[]++;
  124. }
  125. }
  126. p1 = n - i + ;
  127. p2 = n;
  128. for (int j = ; j <= n; ++j) {
  129. if (str[i][j] != str[p1][p2--]) {
  130. ans[]++;
  131. }
  132. }
  133. p1 = n - i + ;
  134. for (int j = ; j <= n; ++j) {
  135. if (str[i][j] != str[p1][j]) {
  136. ans[]++;
  137. }
  138. }
  139. }
  140.  
  141. }
  142. void work() {
  143. int q;
  144. cin >> n >> q;
  145. init();
  146. while (q--) {
  147. int x;
  148. cin >> x;
  149. x %= ;
  150. x /= ;
  151. cout << ans[x] << endl;
  152. }
  153. }
  154.  
  155. int main() {
  156. #ifdef local
  157. freopen("data.txt", "r", stdin);
  158. // freopen("data.txt", "w", stdout);
  159. #endif
  160. IOS;
  161. work();
  162. return ;
  163. }

直接矩阵快速幂,1580ms

Hackonacci Matrix Rotations 观察题 ,更新了我的模板的更多相关文章

  1. E - Addition and Subtraction Hard AtCoder - 2273 思维观察题

    http://arc066.contest.atcoder.jp/tasks/arc066_c?lang=en 这类题目是我最怕的,没有什么算法,但是却很难想, 这题的题解是这样的,观察到,在+号里面 ...

  2. gym101964G Matrix Queries seerc2018k题 cdq分治

    题目传送门 题目大意: 二维平面上有q次操作,每次操作可以是添加一个点,也可以是添加一个矩形,问每次操作后,有多少  点-矩形  这样的pair,pair的条件是点被矩形覆盖(边缘覆盖也算). 思路: ...

  3. C. Timofey and a tree 观察题 + dfs模拟

    http://codeforces.com/contest/764/problem/C 题意:在n个顶点中随便删除一个,然后分成若干个连通子图,要求这若干个连通子图的颜色都只有一种. 记得边是双向的, ...

  4. gym101964G Matrix Queries seerc2018g题 数学归纳法+线段树(递归)

    题目传送门 题目大意: 给出2^k大小的白色矩形,q次操作,每次将一行或者一列颜色反转,问总体矩阵的价值,矩阵的价值定义是,如果整个矩阵颜色相同,价值为1,否则就把这个矩阵切成四份,价值为四个小矩阵的 ...

  5. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

  6. 最短路径Dijkstra算法模板题---洛谷P3371 【模板】单源最短路径(弱化版)

    题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入格式 第一行 ...

  7. 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板

    链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网  HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升.   ...

  8. POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)

    伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...

  9. 【线段树成段更新成段查询模板】【POJ3468】A Simple Problem with Integerst

    题目大意: 2个操作 A.区间a b 增加 c B 查询a b; 注意事项:1.记住要清除标记 2.查询时要下放标记,但没必要向上更新 线段:自带的,不用建模 区间和性质:sum: /* WA 1次 ...

随机推荐

  1. C++MFC编程笔记day06 MFC向导、MFC画图类使用

    MFC画图    MFC画图类包含画图设备类和画图对象类    1 画图设备类      CDC类-父类是CObject,封装的是一般的画图设备,比如:显示器,            打印机等.    ...

  2. for循环console输出结果的问题

    我想定时打印出一串数字,写好了如下代码 for (var i = 0; i < 5; i++) {   setTimeout(function () {     console.log(i); ...

  3. HDUJ 1203 I NEED A OFFER!

    I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. 查看android-support-v4.jar引出的问题

    1.前面博文里也写过如何关联android-support-v4.jar的源码 今天新项目用上述方法的时候,竟然不成功..来回反复试了很长时间,最后发现 新建项目,会自动引用一个类库(自动新建的..) ...

  5. java8--NIO(java疯狂讲义3复习笔记)

    NIO采用内存映射文件的方式处理输入输出,NIO将文件或文件的一段区域映射到内存中,这样就可以像访问内存一样来访问文件了(这种方式模拟了操作系统上的虚拟内存的概念),通过这种方式来进行输入输出比传统的 ...

  6. 在VS2010下使用AppFace

    AppFace的介绍网上一大堆,此文仅为自己作个记录,方便以后查看. 一.需要的文件:1.AppFace.h  2.appface.lib 3.appface.dll 4.macosx_af.urf ...

  7. YTU 2597: 编程题B-选拔飞行员

    2597: 编程题B-选拔飞行员 时间限制: 1 Sec  内存限制: 128 MB 提交: 131  解决: 35 题目描述 2100年空军选拔高中生飞行学员基本条件要求如下,年龄范围:16-19周 ...

  8. 最安全的api接口认证

    最安全的api接口认证 实现步骤: 1.客户端与服务器都存放着用于验证的Token字段,客户端在本地把自己的 用户名+时间戳+Token 组合进行MD5加密后生成一段新的md5-token. 2.客户 ...

  9. bzoj2038 小Z的袜子(hose)——莫队算法

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2038 就是莫队算法: 先写了个分块,惨WA: #include<iostream> ...

  10. pom.xml内容没有错,但一直报错红叉 解决办法

    转自:http://www.cnblogs.com/sxdcgaq8080/p/5590254.html [maven] pom.xml内容没有错,但一直报错红叉 解决办法 1.首先看一下下面的这两个 ...