fzu 1911 C. Construct a Matrix
C. Construct a Matrix
1. The matrix is a S(n)×S(n) matrix;
2. S(n) is the sum of the first n Fibonacci numbers modulus m, that is S(n) = (F1 + F2 + … + Fn) % m;
3. The matrix contains only three kinds of integers ‘0’, ‘1’ or ‘-1’;
4. The sum of each row and each column in the matrix are all different.
Here, the Fibonacci numbers are the numbers in the following sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
By definition, the first two Fibonacci numbers are 1 and 1, and each remaining number is the sum of the previous two.
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2, with seed values F1 = F2 = 1.
Given two integers n and m, your task is to construct the matrix.
Input
Output
Sample Input
2
2 3
5 2
Sample Output
Case 1: Yes
-1 1
0 1
Case 2: No
题意:求fib数列前n项和,以这个和%m作为矩阵的r,然后构造矩阵满足所有值为0,1 或 -1,并且每行每列和都不相等。
思路:第一步矩阵快速幂,第二步找规律。
代码:
#include <stdio.h>
#include <string.h> const int N = 205;
int t, n, m, r; struct mat {
int v[3][3];
mat() {
memset(v, 0, sizeof(v));
}
mat operator * (mat &b) {
mat c;
for (int i = 0; i < 3; i ++)
for (int j = 0; j < 3; j ++)
for (int k = 0; k < 3; k ++)
c.v[i][j] = (v[i][k] * b.v[k][j] + c.v[i][j]) % m;
return c;
}
}; mat pow_mod(mat a, int k) {
if (k == 1 || k == 0)
return a;
mat c = pow_mod(a * a, k / 2);
if (k & 1)
c = c * a;
return c;
} void init() {
scanf("%d%d", &n, &m);
mat start;
start.v[0][0] = start.v[0][1] = start.v[1][0] = start.v[2][0] = start.v[2][1] = start.v[2][2] = 1;
if (n == 1)
r = 1;
else if (n == 2)
r = 2;
else {
mat end = pow_mod(start, n - 2);
r = (end.v[2][0] + end.v[2][1] + end.v[2][2] * 2) % m;
}
} void solve() {
int s[N][N];
memset(s, -1, sizeof(s)); if (r == 0 || r % 2)
printf("No\n");
else {
printf("Yes\n");
for (int i = 1; i <= r; i++) { if (i % 2) {
int tmp = r / 2 + (i + 1) / 2;
s[tmp][i] = 0;
for (int j = tmp + 1; j <= r; j++)
s[j][i] = 1;
} else {
int tmp = (r - i) / 2;
for (int j = tmp + 1; j <= r; j++)
s[j][i] = 1;
}
} for (int i = 1; i <= r; i++) {
// int sum = 0;
for (int j = 1; j < r; j++) {
printf("%d ", s[i][j]);
// sum += s[i][j];
}
printf("%d\n", s[i][r]);
} /*
for (int j = 1; j <= r; j++) {
int sum = 0;
for (int i = 1; i <= r; i++)
sum += s[i][j];
printf("%d ", sum);
}
printf("\n");
*/
}
} int main() {
int cas = 0;
scanf("%d", &t);
while (t --) {
init();
printf("Case %d: ", ++cas);
solve();
}
return 0;
}
fzu 1911 C. Construct a Matrix的更多相关文章
- fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...
- FZU 1911 Construct a Matrix
题目链接:Construct a Matrix 题意:构造一个矩阵,要求矩阵的每行每列的和都不相同.矩阵的边长是前n项斐波那契的和. 思路:由sn = 2*(fn-1)+(fn-2)-1,只要知道第n ...
- Construct a Matrix (矩阵快速幂+构造)
There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...
- <转载> OpenGL Projection Matrix
原文 OpenGL Projection Matrix Related Topics: OpenGL Transformation Overview Perspective Projection Or ...
- Palindromic Matrix
Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
- [kuangbin带你飞]专题1-23题目清单总结
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...
- ACM--[kuangbin带你飞]--专题1-23
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...
随机推荐
- CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问
参考文献: 1. NginxV1.8.0安装与配置 2. CentOS下在Nginx中添加SSL证书以支持HTTPS协议访问 3. nginx配置ssl证书的方法 4.nginx强制使用https访问 ...
- 设计模式(七)组合模式Composite(结构型)
设计模式(七)组合模式Composite(结构型) 1. 概述 在数据结构里面,树结构是很重要,我们可以把树的结构应用到设计模式里面. 例子1:就是多级树形菜单. 例子2:文件和文件夹目录 2.问题 ...
- [置顶] C++ sizeof实例详解
在C++中使用sizeof要比C复杂很多,因为C++类中有static静态变量,virtual虚函数,还有继承.派生等.sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是 ...
- block 解析 - 静态变量
静态变量 上一篇 我们了解了block全局变量的使用,静态变量和全局变量一样,可以直接在block内部使用,也可以在block内部修改 引用官方文档: Global variables are acc ...
- 用shell脚本爬取网页信息
有个小需求,就是爬取一个小网站一些网页里的某些信息,url是带序号的类似的,不需要写真正的spider,网页内容也是差不多的 需要取出网页中<h1></h1>中间的字符串,而且 ...
- MSSQL奇技淫巧
MSSQL:获得库每个表的记录数和容量 sp_msforeachtable是MS未公开的存储过程: exec sp_msforeachtable @command1="print '?'&q ...
- QSerialPort
(草稿) qt5提供QSerialPort类,封装了串口的api, 可以用这个类写串口通信程序.
- BZOJ 3196: Tyvj 1730 二逼平衡树( 树套树 )
这道题做法应该很多吧.... 我用了线段树套treap.... -------------------------------------------------------------------- ...
- BZOJ 1475: 方格取数( 网络流 )
本来想写道水题....结果调了这么久!就是一个 define 里面少加了个括号 ! 二分图最大点权独立集...黑白染色一下 , 然后建图 : S -> black_node , white_no ...
- jsp截取字符串
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> < ...