Construct a Matrix (矩阵快速幂+构造)
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
Output
Sample Input
2
2 3
5 2
Sample Output
Case 1: Yes
-1 1
0 1
Case 2: No 难点在于构造:
构造方式 下三角为-1,上三角为 1,主对角-1 0 排列 ,主要是奇数和0的也不存在
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+;
typedef long long ll;
using namespace std;
struct Mat
{
ll a[][];
}; int mod;
Mat Mul(Mat a,Mat b)
{
Mat ans;
memset(ans.a,,sizeof(ans.a));
for(int t=;t<;t++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
ans.a[t][j]=(ans.a[t][j]+a.a[t][k]*b.a[k][j])%mod;
}
}
}
return ans;
}
Mat ans;
ll quickpow(int n)
{
Mat res;
res.a[][]=;
res.a[][]=;
res.a[][]=;
res.a[][]=;
res.a[][]=;
res.a[][]=;
res.a[][]=;
res.a[][]=;
res.a[][]=; while(n)
{
if(n&)
{
ans=Mul(res,ans);
}
res=Mul(res,res);
n>>=;
}
return ans.a[][];
}
int main()
{
int T;
cin>>T;
int n;
int cnt=;
while(T--)
{
scanf("%d%d",&n,&mod);
memset(ans.a,,sizeof(ans.a));
ans.a[][]=;
ans.a[][]=;
ans.a[][]=;
ll aa=quickpow(n-)%mod;
if(aa&||aa==)
{
printf("Case %d: No\n",cnt++);
}
else
{
printf("Case %d: Yes\n",cnt++);
for(int t=;t<aa;t++)
{
for(int j=;j<aa;j++)
{
if(t>j)
{
printf("-1 ");
}
if(t<j)
{
printf("1 ");
}
if(t==j&&t%==)
{
printf("-1 ");
}
if(t==j&&t%==)
{
printf("0 ");
}
}
printf("\n");
}
} }
return ;
}
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的矩阵,只 ...
- 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 ...
- HDU5015 233 Matrix —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5015 233 Matrix Time Limit: 10000/5000 MS (Java/Others) Memor ...
- UVa 11149 Power of Matrix 矩阵快速幂
题意: 给出一个\(n \times n\)的矩阵\(A\),求\(A+A^2+A^3+ \cdots + A^k\). 分析: 这题是有\(k=0\)的情况,我们一开始先特判一下,直接输出单位矩阵\ ...
- hdu6470 矩阵快速幂+构造矩阵
http://acm.hdu.edu.cn/showproblem.php?pid=6470 题意 \(f[n]=2f[n-2]+f[n-1]+n^3,n \leq 10^{18}\),求f[n] 题 ...
随机推荐
- 学学Viewbinding
Viewbinding 1.环境需求 环境上,需要Android Studio 3.6 Canary 11+ 同样的Gradle也需要升级(这年头都4.0了,应该没有还在用低版本的了吧...) 2.配 ...
- C# Thread.Name 的作用和意义
Thread.Name属性 C#的线程提供Thread.Name属性.这意味着每个线程可以设定一个Name属性来标志它们. Name属性的使用时特性 线程的Name属性默认情况下是null.该值只能被 ...
- ElasticSearch 7.8.1 从入门到精通
学前导读 ElasticSearch对电脑配置要求较高,内存至少4G以上,空闲2G内存,线程数4018+ 学习的时候,推荐将ElasticSearch安装到Linux或者mac上,极度不推荐装Wind ...
- 2020-07-22:你觉得使用redis的主从复制的时候有什么点需要注意的吗?
福哥答案2020-07-22: 1.主从同步缓冲区设定大小,如果进行全量复制耗时太长,进行部分复制时发现数据已经存在丢失的情况,必须进行第二次全量复制,致使slave陷入死循环状态.在全量复制的时候, ...
- C#图解教程(第四版)—01—类型,存储,变量
3.1 如何广泛的描述C#程序 可以说C程序是一组函数和数据类型,C++程序是一组函数和类,然而C#程序是一组类型声明 3.2 类型 可以把类型想象成一个用来创建数据结构的模板,模板本身并不是数据结构 ...
- myBatis源码解析-反射篇(4)
前沿 前文分析了mybatis的日志包,缓存包,数据源包.源码实在有点难顶,在分析反射包时,花费了较多时间.废话不多说,开始源码之路. 反射包feflection在mybatis路径如下: 源码解析 ...
- 复习 Array,重学 JavaScript
1 数组与对象 在 JavaScript 中,一个对象的键只能有两种类型:string 和 symbol.下文只考虑键为字符串的情况. 1.1 创建对象 在创建对象时,若对象的键为数字,或者由 字母+ ...
- Vue3 + Element ui 后台管理系统
Vue3 + Element ui 后台管理系统 概述:这是一个用vue3.0和element搭建的后台管理系统界面. 项目git地址: https://github.com/whiskyma/vu ...
- 【Flutter 实战】一文学会20多个动画组件
老孟导读:此篇文章是 Flutter 动画系列文章第三篇,后续还有动画序列.过度动画.转场动画.自定义动画等. Flutter 系统提供了20多个动画组件,只要你把前面[动画核心](文末有链接)的文章 ...
- 笔记:安装VM Tools、vim编辑器、压缩包、Linux用户管理
一.VM Tools安装 1.作用:方便我们在虚拟机和宿主机之间复制数据或移动文件等. 2.安装步骤: step1:在菜单栏找到虚拟机---->找到安装vm tools ,点击: step2:进 ...