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. 启用nginx status状态详解

    nginx和php-fpm一样内建了一个状态页,对于想了解nginx的状态以及监控nginx非常有帮助.为了后续的zabbix监控,我们需要先了解nginx状态页是怎么回事. 1. 启用nginx s ...

  2. BAAS

    http://blogs.embarcadero.com/sarinadupont/category/baas-tutorials/?cid=701G0000000vH0A&elq=51f98 ...

  3. Java--日期的使用

    Date 类: 最基础的日期时间类,返回一个相对日期的毫秒数.精确到毫秒,但不支持日期的国际化和分时区显示. Calender类: 相对于Date更加强大的时间类,是抽象类,提供了常规的日期修改功能和 ...

  4. Mojo 返回一维和二维数组

    这种情况不断的网数组@arr2里放入数据,返回的内容为: 这种情况是一维数组: while( $selStmt->fetch() ){ print "\$a1 is $a1\n&quo ...

  5. Hadoop集群配置(最全面总结)

    Hadoop集群配置(最全面总结) 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为DataNode也作为Ta ...

  6. WebFetch 是无依赖极简网页爬取组件

    WebFetch 是无依赖极简网页爬取组件,能在移动设备上运行的微型爬虫. WebFetch 要达到的目标: 没有第三方依赖jar包 减少内存使用 提高CPU利用率 加快网络爬取速度 简洁明了的api ...

  7. 【Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款/蓝绿色】玛萨玛索男装网购商城

    [Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款/蓝绿色]玛萨玛索男装网购商城 [特价商品] Dior风格/舒适防风面料/抗静电里衬/大身撞色拼接/精致平驳领/时尚便西款 ...

  8. 上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有 - V2EX

    上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工作看过来初级资深都有 - V2EX 上海投行需要一大群JAVA,C++,C#,UNIX.走过路过不要错过!过完年想换工 ...

  9. 使用Jquery+EasyUI 进行框架项目开发案例解说之二---用户管理源代码分享

    使用Jquery+EasyUI 进行框架项目开发案例解说之二 用户管理源代码分享  在上一篇文章<使用Jquery+EasyUI进行框架项目开发案例解说之中的一个---员工管理源代码分享> ...

  10. sencha touch笔记(6)——路由控制(1)

    做项目的时候在界面的跳转上遇到了挺大的问题,本来跳转不想通过路由来控制的,没办法,只能再去看一下路由的跳转方式了. 应用程序的界面发生改变后,可以通过路由让应用程序的界面返回到改变之前的状态,例如浏览 ...