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 ...
随机推荐
- 【转】Shell编程
原文链接: Shell编程 打算有时间简单了解shell编程 1.shell结构 一个简单的例子: [root@localhost shell]# vi example #!/bin/sh #Thi ...
- (Problem 17)Number letter counts
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...
- Verilog中的标点
在Verilog中有时候会误用的上引号 1,define 中的 `define INITIAL 0 这个单引号用的是键盘左上角的那个单引号,其实就是一个小撇. 2,4'd0 这个 用的是才是叫真正的 ...
- mojo 默认use utf8;
my $endtime=strftime("%Y%m%d%H%M%S",localtime()); my $d=encode_utf8('验证'); if ($a3 =~/$d/) ...
- PHP通过Thrift操作Hbase
PHP通过Thrift操作Hbase HBase是一个开源的NoSQL产品,它是实现了Google BigTable论文的一个开源产品,和Hadoop和HDFS一起,可用来存储和处理海量col ...
- printk
printk的日志级别定义如下(在linux/kernel.h中): #define KERN_EMERG "<0>"/*紧急事件消息,系统崩溃之前提示,表示系统不可用 ...
- Spark Core源代码分析: Spark任务模型
概述 一个Spark的Job分为多个stage,最后一个stage会包含一个或多个ResultTask,前面的stages会包含一个或多个ShuffleMapTasks. ResultTask运行并将 ...
- ios创建画笔的样例(双笔画效果)
定义一个UIView:主要是在这个View里面加一个UIImageView,绘图都在这个UIImageView里面进行 @property(nonatomic) CGPoint prePoint; / ...
- 用php 把数组中偶数,选择出来
我有这种一个小算法,把数组中的全部的偶数或技术分别选择出来.非常多人可能,会循环这个数组,而我恰恰不循环数组就能做到这一点.代码例如以下. function odd($var) { // return ...
- phantomjs环境搭建已经运行
1.下载phantomjs http://phantomjs.org/ 2.运行 新建phantomjs.bat,记得改目录路径 里面内容为: D:\java\phantomjs\phantomjs. ...