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. time模块 random模块

    time模块 time.sys等模块是C语言实现的,内置到了python解释器的.而不是py文件. 导入模块的时候,优先到python解释器,然后才会找py文件. #时间戳 #计算 # print(t ...

  2. ThreadLocal刨根问底

    一.ThreadLocal使用场景 数据库连接connection对象使用,每个客户都能使用自己的connection对象.不会出现客户A操作关闭了客户B的connection 案例:https:// ...

  3. 一个简单的CPP处理框架

    好久没有在csdn上写过东西了,这么多年,一方面是工作忙,下班到家也没有开过电脑了,要陪小孩玩: 下面分享一段代码,是用CPP做的一个简单的消息(协议)处理框架: 是通过成员函数指针+map来实现的: ...

  4. 利用mybatis的Generator的插件生成代码

    1 在resources文件夹下创建generatorConfig.xml文件来做相关配置 <?xml version="1.0" encoding="UTF-8& ...

  5. 某大型企业ospf面试题分析(含路由策略和路由过滤,及双点双向重发布)

    面试问题背景 本面试题来自国内最大通信技术公司之一,央企,有很多金融网项目. 了解行业的同学,一定知道事哪个企业. 上面试问题(取自百哥收集整理的面试总结大全,关注百哥CSDN或知乎,不定期分享名企面 ...

  6. 用python爬取B站在线用户人数

    最近在自学Python爬虫,所以想练一下手,用python来爬取B站在线人数,应该可以拿来小小分析一下 设计思路 首先查看网页源代码,找到相应的html,然后利用各种工具(BeautifulSoup或 ...

  7. React 17 要来了,非常特别的一版

    写在前面 React 最近发布了v17.0.0-rc.0,距上一个大版本v16.0(发布于 2017/9/27)已经过去近 3 年了 与新特性云集的 React 16及先前的大版本相比,React 1 ...

  8. 【MySQL】记一次线上重大事故:二狗子竟然把线上数据库删了!!

    写在前面 估计二狗子这几天是大姨夫来了,心情很郁闷,情绪也很低落,工作的时候也有点心不在焉.让他发个版本,结果,一行命令下去把线上的数据库删了!你没听错:是删掉了线上的数据库!运营那边顿时炸了锅:怎么 ...

  9. idea github 上传项目

    1.创建本地仓库,VCS-->Import into Version Control-->Create Git Repository... 在弹框中选中项目所在的位置,点击OK,此时项目文 ...

  10. 第5章 JDBC/ODBC服务器

    第5章 JDBC/ODBC服务器 Spark SQL也提供JDBC连接支持,这对于让商业智能(BI)工具连接到Spark集群上以 及在多用户间共享一个集群的场景都非常有用.JDBC 服务器作为一个独立 ...