FZU 1911 Construct a Matrix
题目链接:Construct a Matrix
题意:构造一个矩阵,要求矩阵的每行每列的和都不相同。矩阵的边长是前n项斐波那契的和。
思路:由sn = 2*(fn-1)+(fn-2)-1,只要知道第n-1和第n-2项即可,n的范围是10^9,可由矩阵快速幂求出第n项。然后,构造矩阵,上三角为1,下三角全为-1,对角线1和0交替。【真是个天才...!!!】矩阵快速幂求第n项时,构造的矩阵是a[0][0] = f2, a[1][0] = f1, a[0][1] = 1, a[1][1] = 0........【ACMer的脑洞真大...可怕....当然不包括我辣...】
知道思路当然就好实现了。附代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std; struct Mat{
int a[2][2];
void init1() { // 斐波那契初始矩阵
a[0][0] = a[0][1] = a[1][0] = 1;
a[1][1] = 0;
}
void init2() { //单位矩阵
a[0][0] = a[1][1] = 1;
a[0][1] = a[1][0] = 0;
}
}; Mat mul(Mat aa, Mat b, int m) { // 矩阵乘法mod m
Mat ans;
for (int i=0; i<2; ++i) {
for (int j=0; j<2; ++j) {
ans.a[i][j] = 0;
for (int k=0; k<2; ++k) {
ans.a[i][j] += ((aa.a[i][k]%m)*(b.a[k][j]%m))%m;
ans.a[i][j] %= m;
//ans.a[i][j] += aa.a[i][k]*b.a[k][j];
}
//cout << ans.a[i][j] << "....\n";
}
}
return ans;
} Mat mul_mat_quick(Mat a, int n, int m) { // 矩阵快速幂%m
Mat ans;
ans.init2();
while(n) {
if (n%2) ans = mul(ans, a, m);
a = mul(a, a, m);
n /= 2;
}
return ans;
} int num[210][210]; int main() {
int t;
int cas = 0;
scanf("%d", &t);
while(t--) {
int n, m;
scanf("%d%d", &n, &m);
Mat a;
a.init1();
Mat ans = mul_mat_quick(a, n-1, m);
int fn1 = ans.a[0][0]; // fn-1
int fn2 = ans.a[1][0]; // fn-2
//cout << fn1 << "++++=" << fn2 << endl;
int sn = (2*fn1 + fn2 - 1)%m; //矩阵边长
// cout << sn << "===\n"; if (sn%2 || sn==0) {
printf("Case %d: No\n", ++cas);
continue;
} printf("Case %d: Yes\n", ++cas);
memset(num, 0, sizeof(num));
for (int i=1; i<=sn; ++i) {
for (int j=1; j<=sn; ++j) {
if (i<j) num[i][j] = 1;
else num[i][j] = -1;
}
}
int pre = 1;
for (int i=1; i<=sn; ++i) {
num[i][i] = (pre^1);
pre ^= 1;
}
for (int i=1; i<=sn; ++i) {
for (int j=1; j<=sn; ++j) {
if (j==1) printf("%d", num[i][j]);
else printf(" %d", num[i][j]);
}
printf("\n");
}
}
return 0;
}
FZU 1911 Construct a Matrix的更多相关文章
- fzu 1911 Construct a Matrix(矩阵快速幂+规律)
题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...
- fzu 1911 C. Construct a Matrix
C. Construct a Matrix Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB Special Judge ...
- Construct a Matrix (矩阵快速幂+构造)
There is a set of matrixes that are constructed subject to the following constraints: 1. The matrix ...
- KUANGBIN带你飞
KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题 //201 ...
- [kuangbin带你飞]专题1-23题目清单总结
[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...
- ACM--[kuangbin带你飞]--专题1-23
专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 FliptilePOJ 1426 Find T ...
- <转载> OpenGL Projection Matrix
原文 OpenGL Projection Matrix Related Topics: OpenGL Transformation Overview Perspective Projection Or ...
- Palindromic Matrix
Palindromic Matrix time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】
任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...
随机推荐
- jQuery的.click,.bind,.unbind,.on,.off,.delegate,.undelegate
.click与.bind .click和.bind都是给每个元素绑定事件,对于只绑定一个click事件,.bind事件的简写就是.click那种方式. 这两种方式都会出现两个问题: 第一个问题,如果要 ...
- 49个jquery代码经典片段
49个jquery代码经典片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助你又快又好地 ...
- iOS - OC RunLoop 运行循环/消息循环
1.RunLoop 1)运行循环: 运行循环在 iOS 开发中几乎不用,但是概念的理解却非常重要. 同一个方法中的代码一般都在同一个运行循环中执行,运行循环监听 UI 界面的修改事件,待本次运行循环结 ...
- iOS - UIStepper
前言 NS_CLASS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED @interface UIStepper : UIControl @available(iOS 5.0 ...
- iOS - UIWebView
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIWebView : UIView <NSCoding, UIScrol ...
- IQ一个人的智力和对科学知识的理解掌握程度。 EQ对环境和个人情绪的掌控和对团队关系的运作能力。 AQ挫折商 一个人面对困境时减除自己的压力、渡过难关的能力。
IQ: Intelligence Quotient 智商 一个人的智力和对科学知识的理解掌握程度. EQ: Emotional Quotient 情商 一个人对环境和个人情绪的掌控和对团队关系的运作能 ...
- 树的计数 + prufer序列与Cayley公式 学习笔记
首先是 Martrix67 的博文:http://www.matrix67.com/blog/archives/682 然后是morejarphone同学的博文:http://blog.csdn.ne ...
- hdu4720Naive and Silly Muggles
链接 一直理解的最小覆盖圆就是外接圆..原来还要分钝角和锐角... 钝角的话就为最长边的中点,对于这题分别枚举一下外接圆以及中点的圆,判一下是不是在园外. #include <iostream& ...
- HTML字符实体
常用实体符号:
- Oracle的热备份
一. 什么是热备份 热备份也叫联机备份,它是指数据库处于open状态下,对数据库的数据文件.控制文件.参数文件.密码文件等进行一系列备份操作(其中数据文件是必须备份的). 它要求数据库处在归档模式下. ...