There is a set of matrixes that are constructed subject to the following constraints:

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

The first line of the input contains an integer T (T <= 25), indicating the number of cases. Each case begins with a line containing two integers n and m (2 <= n <= 1,000,000,000, 2 <= m <= 200).

Output

For each test case, print a line containing the test case number (beginning with 1) and whether we could construct the matrix. If we could construct the matrix, please output “Yes”, otherwise output “No” instead. If there are multiple solutions, any one is accepted and then output the S(n)×S(n) matrix, separate each integer with an blank space (as the format in sample).

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 (矩阵快速幂+构造)的更多相关文章

  1. fzu 1911 Construct a Matrix(矩阵快速幂+规律)

    题目链接:fzu 1911 Construct a Matrix 题目大意:给出n和m,f[i]为斐波那契数列,s[i]为斐波那契数列前i项的和.r = s[n] % m.构造一个r * r的矩阵,只 ...

  2. 233 Matrix 矩阵快速幂

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

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

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

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

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

  5. 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] ...

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

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

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

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

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

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

  9. 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] 题 ...

随机推荐

  1. [COCOS2DX-LUA]0-006.cocos2dx中关于拖动屏幕物件,同时点击home键,返回后页面变黑的问题。

    基本信息介绍: 引擎框架: Quick-Cocos2dx-Community-3.6 测试机型: 魅族MX5 问题简介: 有拖动效果的物件,在拖动的工程中,手指不放,同时点击home键退到后台. 再返 ...

  2. 038_go语言中的状态协程

    代码演示: package main import ( "fmt" "math/rand" "sync/atomic" "time ...

  3. tomcat启动失败的解决办法

    初次安装tomcat启动失败的解决办法: 1.CATALINA_HOME    C:\Program Files\apache-tomcat-8.5.242.path  %CATALINA_HOME% ...

  4. Springboot调用Oracle存储过程的几种方式

    因工作需要将公司SSH项目改为Spingboot项目,将项目中部分需要调用存储过程的部分用entityManagerFactory.unwrap(SessionFactory.class).openS ...

  5. MySQL查看没有主键的表

    select table_schema, table_name from information_schema.tables where table_name not in (select disti ...

  6. Jmeter(二十一) - 从入门到精通 - JMeter断言 - 上篇(详解教程)

    1.简介 最近由于宏哥在搭建自己的个人博客可能更新的有点慢.断言组件用来对服务器的响应数据做验证,常用的断言是响应断言,其支持正则表达式.虽然我们的通过响应断言能够完成绝大多数的结果验证工作,但是JM ...

  7. Python-Opencv 轮廓常用操作

    Python-Opencv 轮廓常用操作 1.颜色空间转换 使用cv2.cvtColor(input_image ,flag),flag为转换类型 常用的转换类型有: BGR和灰度图的转换使用 cv2 ...

  8. Petya and Graph/最大权闭合子图、最小割

    原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...

  9. JavaScript 用七种方式教你判断一个变量是否为数组类型

    JavaScript 如何判断一个变量是否为数组类型 引言 正文 方法一 方法二 方法三 方法四 方法五 方法六 方法七 结束语 引言 我们如何判断一个变量是否为数组类型呢? 今天来给大家介绍七种方式 ...

  10. 使用hexo+github搭建博客

    https://blog.csdn.net/qq_36667170/article/details/105789610这一篇已经写得很详细了,下面的内容是我操作的时候遇到的问题及解决方法. 1.下载N ...