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项重复。

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n;
const int maxn = + ;
char str[maxn][maxn];
int ans[];
int f[] = {, , , , , , , };
void init() {
for (int i = ; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
if (i == && j == ) continue;
LL val = (1LL * i * j) * (1LL * i * j);
val %= ;
if (val == ) val = ;
if (f[val] == ) {
str[i][j] = 'Y';
} else str[i][j] = 'X';
}
// cout << endl;
}
for (int i = ; i <= n; ++i) {
for (int j = ; j <= i - ; ++j) {
str[i][j] = str[j][i];
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= n; ++j) {
// cout << str[i][j];
// }
// cout << endl;
// }
//
int p1 = n, p2 = ;
for (int i = ; i <= n; ++i) {
p1 = n;
p2 = i;
for (int j = ; j <= n; ++j) {
if (str[i][j] != str[p1--][p2]) {
ans[]++;
}
}
p1 = n - i + ;
p2 = n;
for (int j = ; j <= n; ++j) {
if (str[i][j] != str[p1][p2--]) {
ans[]++;
}
}
p1 = n - i + ;
for (int j = ; j <= n; ++j) {
if (str[i][j] != str[p1][j]) {
ans[]++;
}
}
} }
void work() {
int q;
scanf("%d%d", &n, &q);
init();
while (q--) {
int x;
scanf("%d", &x);
// cin >> x;
x %= ;
x /= ;
// cout << ans[x] << endl;
printf("%d\n", ans[x]);
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

观察,0ms

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n;
const int maxn = + ;
char str[maxn][maxn];
struct Matrix {
LL a[][];
int row;
int col;
};
//struct Matrix matrix_mul(struct Matrix a, struct Matrix b, int MOD) {
// struct Matrix c = {0};
// c.row = a.row;
// c.col = b.col;
// for (int i = 1; i <= a.row; ++i) {
// for (int k = 1; k <= a.col; ++k) {
// if (a.a[i][k]) {
// for (int j = 1; j <= b.col; ++j) {
// c.a[i][j] += a.a[i][k] * b.a[k][j];
//// c.a[i][j] = (c.a[i][j] + MOD) % MOD;
// }
// c.a[i][j] %= MOD;
// }
// }
// }
// return c;
//}
struct Matrix matrix_mul (struct Matrix a, struct Matrix b, int MOD) {
struct Matrix c = {}; c.row = a.row;
c.col = b.col;
for (int i = ; i <= a.row; i++) {
for (int j = ; j <= b.col; j++) {
for (int k = ; k <= b.row; k++) {
c.a[i][j] += a.a[i][k] * b.a[k][j];
}
c.a[i][j] = (c.a[i][j] + MOD) % MOD;
}
}
return c;
} struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, LL n, int MOD) {
while (n) {
if (n & ) {
ans = matrix_mul(ans, base, MOD);
}
n >>= ;
base = matrix_mul(base, base, MOD);
}
return ans;
}
int ans[];
void init() {
struct Matrix A;
A.row = ;
A.col = ;
A.a[][] = ;
A.a[][] = ;
A.a[][] = ;
struct Matrix B;
B.col = B.row = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
B.a[][] = ;
str[][] = 'Y';
// cout << "1" << " ";
for (int i = ; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
if (i == && j == ) continue;
LL val = (1LL * i * j) * (1LL * i * j);
// cout << val << " ";
struct Matrix t = quick_matrix_pow(A, B, val - , );
// cout << B.a[2][3] << " ";
if (t.a[][] & ) {
str[i][j] = 'Y';
} else str[i][j] = 'X';
// cout << t.a[1][3] << " ";
}
// cout << endl;
}
for (int i = ; i <= n; ++i) {
for (int j = ; j <= i - ; ++j) {
str[i][j] = str[j][i];
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= n; ++j) {
// cout << str[i][j];
// }
// cout << endl;
// }
//
int p1 = n, p2 = ;
for (int i = ; i <= n; ++i) {
p1 = n;
p2 = i;
for (int j = ; j <= n; ++j) {
if (str[i][j] != str[p1--][p2]) {
ans[]++;
}
}
p1 = n - i + ;
p2 = n;
for (int j = ; j <= n; ++j) {
if (str[i][j] != str[p1][p2--]) {
ans[]++;
}
}
p1 = n - i + ;
for (int j = ; j <= n; ++j) {
if (str[i][j] != str[p1][j]) {
ans[]++;
}
}
} }
void work() {
int q;
cin >> n >> q;
init();
while (q--) {
int x;
cin >> x;
x %= ;
x /= ;
cout << ans[x] << endl;
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}

直接矩阵快速幂,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. 非计算机专业的伟伯是怎样拿到阿里Offer的。求职励志!!!

    写在前面: 2015 年 7 月初.參加阿里巴巴校招内推, 8 月 15 日拿到研发project师 JAVA 的 offer .我的专业并不是计算机,也没有在互联网公司实习过,仅仅有一些学习和面试心 ...

  2. easyUI 验证控件应用、自己定义、扩展验证 手机号码或电话话码格式

    easyUI 验证控件应用.自己定义.扩展验证 手机号码或电话话码格式 在API中   发现给的demo 中没有这个验证,所以就研究了下. 相关介绍省略,直接上代码吧! watermark/2/tex ...

  3. InnoDB: Error: io_setup() failed with EAGAIN after 5 attempts

    在一台server中以各数据库的备份文件为数据文件启动多个MySQL实例供SQL Review使用. 之前执行一直没有问题(最多的时候有23个MySQL实例同一时候执行).后来新配置了一台server ...

  4. [MAT]使用MAT比較多个heap dump文件

    使用MAT比較多个heap dump文件 调试内存泄露时,有时候适时比較2个或多个heap dump文件是非常实用的.这时须要生成多个单独的HPROF文件. 以下是一些关于怎样在MAT里比較多个hea ...

  5. 项目实战之玩转div+css制作自己定义形状

    项目需求 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/diss ...

  6. Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeException

    使用MapReduce编写的中文分词程序出现了 Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeExcept ...

  7. 使用RNN解决句子对匹配问题的常见网络结构

    /* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 除了序列标注问题外,句子对匹配(Sentence Pair Matching)问题也是NLP中非经常见 ...

  8. noteexpress使用指南

    软件功能:在写论文时直接调用参考数据并输出正规的格式. (以下简称NE) A.下载安装 下载地址:Note-express - Bibliography Software  选择相应的学校进行下载,相 ...

  9. Delphi中accesss实现树形结构查询系统(一次性生成比较方便)

    主要是要读取数据库的信息,而delphi界面是一个树形结构. 例如有一个Ascess数据库:示例.MDB,内有一张表:“国家”,表的内容如下: 编号        名称  01             ...

  10. RK3288以太网的mac地址调试笔记【学习笔记】【原创】

    平台信息:内核:linux3.1.0系统:android/android6.0平台:RK3288 作者:庄泽彬(欢迎转载,请注明作者) 邮箱:2760715357@qq.com 说明:提供以太网mac ...