C. Construct a Matrix

Time Limit: 1000ms
Case Time Limit: 1000ms
Memory Limit: 32768KB
Special Judge
 
64-bit integer IO format:  %I64d      Java class name:  Main
Font Size:  +   -
There is a set of matrixes that are constructed subject to the following constraints:

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

The first line of the input contains an integer T (T <= 25), indicating the number of cases. Each case begins with a line containing two integers n and m (2 <= n <= 1,000,000,000, 2 <= m <= 200).
 

Output

For each test case, print a line containing the test case number (beginning with 1) and whether we could construct the matrix. If we could construct the matrix, please output “Yes”, otherwise output “No” instead. If there are multiple solutions, any one is accepted and then output the S(n)×S(n) matrix, separate each integer with an blank space (as the format in sample).
 

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的更多相关文章

  1. fzu 1911 Construct a Matrix(矩阵快速幂+规律)

    题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...

  2. FZU 1911 Construct a Matrix

    题目链接:Construct a Matrix 题意:构造一个矩阵,要求矩阵的每行每列的和都不相同.矩阵的边长是前n项斐波那契的和. 思路:由sn = 2*(fn-1)+(fn-2)-1,只要知道第n ...

  3. Construct a Matrix (矩阵快速幂+构造)

    There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...

  4. <转载> OpenGL Projection Matrix

    原文 OpenGL Projection Matrix Related Topics: OpenGL Transformation Overview Perspective Projection Or ...

  5. Palindromic Matrix

    Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  7. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  8. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

  9. ACM--[kuangbin带你飞]--专题1-23

    专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...

随机推荐

  1. BZOJ 2435: [Noi2011]道路修建( dfs )

    NOI的水题...直接一遍DFS即可 ------------------------------------------------------------------------- #includ ...

  2. POJ 2208 Pyramids 欧拉四面体

    给出边长,直接就可以求出体积咯 关于欧拉四面体公式的推导及证明过程 2010-08-16 14:18 1,建议x,y,z直角坐标系.设A.B.C少拿点的坐标分别为(a1,b1,c1),(a2,b2,c ...

  3. 平衡二叉树算法实现 c语言版 插入 删除

    #include <stdio.h>#include <malloc.h>#include<stdlib.h> #define EQ(a,b) ((a)==(b)) ...

  4. POJ 3261 Milk Patterns(后缀数组+二分答案+离散化)

    题意:给定一个字符串,求至少出现k 次的最长重复子串,这k 个子串可以重叠. 分析:经典的后缀数组求解题:先二分答案,然后将后缀分成若干组.这里要判断的是有没有一个组的符合要求的后缀个数(height ...

  5. android的animator

    3.0 以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这 ...

  6. Linux下安装yum工具

    Linux下安装yum工具 http://blog.csdn.net/caoshichaocaoshichao/article/details/13171919

  7. 17-UIKit(UIView的动画)

    2. UIView的动画 UIView类本身具有动画的功能 2.1 概念 由UI对底层Core Animation框架的封装 可以轻松简单的实现动画效果 2.2 两种使用方式 1> Block ...

  8. 模拟QQ系统设置面板实现功能

    业务需求: 基于网盘客户端的实现,原有网盘的设置面板无论从界面显示还是从业务需求都不能满足我们的正常需求.当前的要求是,模拟QQ系统设置的面板实现当前我们网盘中的基本配置功能.在完成这篇文章时已将基本 ...

  9. Spring MVC视图层:thymeleaf vs. JSP

    本文对比了同一Spring MVC工程中相同页面(一个订阅表单)分别采用Thymeleaf和JSP(包括JSP.JSTL.Spring tag lib)两种方式的实现. 本文的所有代码来自一个可运行的 ...

  10. Linux 命令整理

    一.文件目录命令 1.建立目录:mkdir 目录名 2.删除空目录:rmdir 目录名 3.无条件删除子目录: rm -rf 目录名 4.改变当前目录:cd 目录名 (进入用户home目录:cd ~; ...