题目链接:fzu 1911 Construct a Matrix

题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和。r = s[n] % m。构造一个r * r的矩阵,只能使用-1、0、1。使得矩阵的每行每列的和都不相同,输出方案,不行的话输出No。

解题思路:求r的话用矩阵快速幂求,每次模掉m,

{ {1, 1, 0}, {1, 0, 0}, {1, 1, 1} } * { f[i], f[i -1], s[i] } = { f[i + 1], f[i], s[i + 1] }.

然后求出r后,若r是奇数或0,则矩阵不存在;r为偶数时,只要按照规律建立矩阵就可以了。

#include <stdio.h>
#include <string.h> const int M = 10;
const int N = 205; int n, m, r; struct Mul {
int s[M][M];
Mul() { memset(s, 0, sizeof(s)); }
Mul operator * (const Mul& c) {
Mul ans; for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
ans.s[i][j] = 0;
for (int k = 0; k < 3; k++)
ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j] ) % m;
}
}
return ans;
} void put() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
printf("%d ", s[i][j]);
printf("\n");
}
}
}; Mul MulPow(Mul a, int t) {
if (t == 1) return a; Mul x = MulPow(a, t / 2); x = x * x; if (t % 2) x = x * a; return x;
} void init() {
if (n > 2) {
Mul a;
a.s[0][0] = a.s[0][1] = a.s[1][0] = a.s[2][0] = a.s[2][1] = a.s[2][2] = 1; Mul ans = MulPow(a, n - 2); r = (ans.s[2][0] + ans.s[2][1] + ans.s[2][2] * 2) % m;
} else if (n == 2) {
r = 2 % m;
} else if (n == 1) {
r = 1;
}
} void solve() {
if (r == 0 || r % 2)
printf("No\n");
else {
int v[N][N];
memset(v, -1, sizeof(v));
printf("Yes\n"); for (int i = 1; i <= r; i++) {
int tmp;
if (i % 2) {
tmp = (r + i + 1) / 2;
v[tmp][i] = 0;
} else
tmp = (r - i) / 2;
for (int j = tmp + 1; j <= r; j++)
v[j][i] = 1;
} for (int i = 1; i <= r; i++) {
for (int j = 1; j < r; j++)
printf("%d ", v[i][j]);
printf("%d\n", v[i][r]);
}
}
} int main () {
int cas;
scanf("%d", &cas);
for (int i = 1; i <= cas; i++) {
scanf("%d%d", &n, &m);
printf("Case %d: ", i); init(); solve();
}
return 0;
}

fzu 1911 Construct a Matrix(矩阵快速幂+规律)的更多相关文章

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

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

  2. FZU 1911 Construct a Matrix

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

  3. 233 Matrix 矩阵快速幂

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...

  4. HDU - 5015 233 Matrix (矩阵快速幂)

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...

  5. UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)

    题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...

  6. HDU 5015 233 Matrix --矩阵快速幂

    题意:给出矩阵的第0行(233,2333,23333,...)和第0列a1,a2,...an(n<=10,m<=10^9),给出式子: A[i][j] = A[i-1][j] + A[i] ...

  7. 233 Matrix(矩阵快速幂+思维)

    In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...

  8. UVa 11149 Power of Matrix 矩阵快速幂

    题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...

  9. HDU5015 233 Matrix —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-5015 233 Matrix Time Limit: 10000/5000 MS (Java/Others)    Memor ...

随机推荐

  1. 一组开源 HTML5 Apps

    一组用"画app吧"开发的 HTML5 Apps,默认使用FirefoxOS设备,其实它们都可以在像Android/IPhone/WindowsPhone8/BlackBerry/ ...

  2. 最全的LBS手机定位技术说明

    随着手机技术的发展定位方式也发生了非常大的变化.获取手机位置有非常多种方式. 第一种:CELL-ID定位原理 通过移动网络获取设备当前所在的Cell信息来获取设备当前位置.当设备位置更新设备会向当前服 ...

  3. float 覆盖元素的问题

    <span class="right-span"><a href="/xxx/" class="btn">增加Ser ...

  4. BZOJ 1096: [ZJOI2007]仓库建设( dp + 斜率优化 )

    dp(v) = min(dp(p)+cost(p,v))+C(v) 设sum(v) = ∑pi(1≤i≤v), cnt(v) = ∑pi*xi(1≤i≤v), 则cost(p,v) = x(v)*(s ...

  5. 深入理解GCD ( 二 )

    转自@nixzhu的GitHub主页(译者:Riven.@nixzhu),原文<Grand Central Dispatch In-Depth: Part 2/2> 欢迎来到GCD深入理解 ...

  6. Codeforces 479D - Long Jumps

    479D - Long Jumps, 480B - Long Jumps It , or . If we can already measure both x and y, output . Then ...

  7. IE6/7/8 CSS兼容性问题和解决方法汇总

    断断续续的在开发过程中收集了好多的bug以及其解决的办法,都在这个文章里面记录下来了!希望以后解决类似问题的时候能够快速解决,也希望大家能在留言里面跟进自己发现的ie6 7 8bug和解决办法! 1: ...

  8. 运行计划之误区,为什么COST非常小,SQL却跑得非常慢?

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/38321477 2014.7.31就晚20:30 My Oracle Support组猫大师 ...

  9. pyfits例子

    下面是一个读入fits文件,画积分强度图,再把某个星表里的天体画到图上的python程序. ====================================================== ...

  10. SignalR系列教程:SignalR快速入门

    ---恢复内容开始--- 本篇是SignalR系列教程的第一篇,本篇内容介绍了如何创建SignalR应用,如何利用SignalR搭建简易的聊天室等,本篇内容参考自:http://www.asp.net ...