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 ...
随机推荐
- Robot Framework学习路线
0. 官方网站 http://robotframework.org/ 所有资料都来自这里,从这里找到必要的链接,从而深入其中的细节. 1. Quick Start Guide https://co ...
- Android 5.0五大安全特性
全盘加密(Full Disk Encryption, FDE) 对所有闪存数据加密.性能下降较大 Nexus 6,Nexus 9无法关闭FDE 对于其它设备.Google推荐开启 多用户支持 4.2中 ...
- 修改字符串 ToCharArray()
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- CodeForces 519B A and B and Compilation Errors【模拟】
题目意思还是蛮简单的,看 输入数据输出数据还是比较明显的 我用排序来写还是可以AC的 //#pragma comment(linker, "/STACK:16777216") // ...
- 如何在.Net中使用Redis
Redis是一个key-value存储系统.和Memcached类似,但是解决了断电后数据完全丢失的情况,而且她支持更多无化的value类型,除了和string外,还支持lists(链表).sets( ...
- Qt5程序开机自启动(windows)
简介 window下开机启动最简单的实现方式就是在注册表中添加启动项目 添加位置有两个 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVer ...
- windows无效字符名导致的错误及解决办法
今天用file_put_content($fileName,$data)产生错误:内容如下: Warning: file_put_contents(images/7d5636992a7395f9174 ...
- php 登陆动作详解
<?php class LoginAction extends Action { function index(){ $this->display(); } function do_log ...
- Event | Beijing Makerspace
Event | Beijing Makerspace CONTACT INFORMATION 4th Floor, Zhongguancun Dream Lab, Beijing, China Pho ...
- c-大量经典的c算法---ShinePans
经典的100个c算法 算法 题目:古典问题:有一对兔子.从出生后第3个月起每一个月都生一对兔子.小兔 子长到第三个月后每一个月又生一对兔子,假如兔子都不死,问每一个月的兔子总数 为多少? _____ ...