fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接: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(矩阵快速幂+规律)的更多相关文章
- Construct a Matrix (矩阵快速幂+构造)
There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...
- FZU 1911 Construct a Matrix
题目链接:Construct a Matrix 题意:构造一个矩阵,要求矩阵的每行每列的和都不相同.矩阵的边长是前n项斐波那契的和. 思路:由sn = 2*(fn-1)+(fn-2)-1,只要知道第n ...
- 233 Matrix 矩阵快速幂
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- HDU - 5015 233 Matrix (矩阵快速幂)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- UVa 11149 Power of Matrix (矩阵快速幂,倍增法或构造矩阵)
题意:求A + A^2 + A^3 + ... + A^m. 析:主要是两种方式,第一种是倍增法,把A + A^2 + A^3 + ... + A^m,拆成两部分,一部分是(E + A^(m/2))( ...
- 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] ...
- 233 Matrix(矩阵快速幂+思维)
In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233 ...
- UVa 11149 Power of Matrix 矩阵快速幂
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...
- HDU5015 233 Matrix —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5015 233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memor ...
随机推荐
- BZOJ 1699: [Usaco2007 Jan]Balanced Lineup排队
1699: [Usaco2007 Jan]Balanced Lineup排队 Description 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. ...
- 1410 - Consistent Verdicts(规律)
1410 - Consistent Verdicts PDF (English) Statistics Forum Time Limit: 5 second(s) Memory Limit: 32 ...
- 《Android第一行代码》笔记
学习Android开发差点儿相同有两年时间了.期间也做了大大小小的一些项目.近来抽出闲暇想把Android基础强化一下,之前在网上看到了郭霖郭大神的几篇博客.从中受益不少.于是花了近一周时间看完了郭神 ...
- iOS_第3方类库_BlurAlertView_GPUImage
最终效果图: 先加入GPUImage.framework 导入BlurAlertView的类声明和类实现 // // BlurAlertView.h // 特效弹出框 // // Created by ...
- QT 入门 -QApplication QPushButton QDialog Ui类型的手工使用
QT 1.工具 assistant 帮助文档 qtconfig QT配置工具 qmake QT的make与项目文件智能创建工具 uic UI界面的设计文件的编译工具 mo ...
- hdu4597 Play Game(DFS)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4597 题意 Alic ...
- BZOJ 1552: [Cerc2007]robotic sort( splay )
kpm大神说可以用块状链表写...但是我不会...写了个splay.... 先离散化 , 然后splay结点加个min维护最小值 , 就可以了... ( ps BZOJ 3506 题意一样 , 双倍经 ...
- goahead 移植
1.网上下载goahead-3.1.-0-src.tgz包 2.解压 tar -zxvf goahead-3.1.-0-src.tgz 3.编译 cd goahead-3.1.-0 make CC=a ...
- Python之路 Day12
day12主要内容:html基础.CSS基础 HTML HTML概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标 ...
- DE1-SOC连接设定
将电源供应器插上电源接口. 使用白色的USB Type B线材将计算机与DE1-SoC上的USB-Blaster II接口连接.此接口主要负责FPGA配置以及HPS Debug使用. 使用Mini-U ...