【FZU1911】 Construct a Matrix(快速矩阵幂+构造)

题目链接

Problem 1911 Construct a Matrix

Accept: 321    Submit: 900    Special Judge
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

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

Source

2010年全国大学生程序设计邀请赛(福州)

 

解题思路:

这里用到斐波那契数列求和的递推公式S(n)=F(n+2)-1,因为n比较大,所以要用矩阵快速幂求出S(n)。

接下来的问题就是如何构造矩阵了,手动模拟一下可以知道当w=0或w为奇数时是不存在这样的矩阵的,而当w为偶数时,题目要求矩阵每行每列的和不同,和一定在-w与w之间,因为是2*w个数,所以这里选用-w+1~w,刚好w*2个数,构造矩阵时巧妙利用了对角线,因为对角线刚好将一个矩阵平分,1都写在上三角,-1写在下三角,再考虑对角线上写什么。

 

代码:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
struct Mutrix
{
    LL a[3][3];
    void init()
    {
        memset(a,0,sizeof(a));
        for(int i=0;i<3;i++)
            a[i][i]=1;
    }
};
Mutrix f;
LL m;
void init()
{
	f.a[0][0]=f.a[0][1]=f.a[1][0]=1;
}
Mutrix mul(Mutrix a, Mutrix b)
{
    Mutrix ans;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            ans.a[i][j]=0;
            for(int k=0;k<3;k++)
            {
                ans.a[i][j]+=a.a[i][k]*b.a[k][j]%m;
                ans.a[i][j]%=m;
            }
        }
    }
    return ans;
}
Mutrix quick_pow(Mutrix a, LL x)
{
    Mutrix ans;
    ans.init();
    while(x)
    {
        if(x&1) ans=mul(ans,a);
        a=mul(a,a);
        x>>=1;
    }
    return ans;
}
int a[220][220];
int main()
{
    LL n;
    int k=0, T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d%I64d", &n, &m);
        init();
        LL w=(quick_pow(f,n).a[0][0]+quick_pow(f,n).a[0][1]-1)%m;
        //printf("%lld\n",w);
        printf("Case %d: ", ++k);
        if(w==0 || w&1)
        {
            printf("No\n");
            continue;
        }
        else printf("Yes\n");
        memset(a,0,sizeof(a));
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(i<w/2)
                {
                    if(j>=i)a[i][j]=1;
                }
                else
                {
                    if(j<i)a[i][j]=-1;
                }
            }
        }
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(j!=0)printf(" ");
                printf("%d",a[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 福州大学汇编语言期末考试历年来都是以实际操作为重点,考察学生对于汇编语言的运用能力和实践经验。考试内容主要包括单项选择、填空和编程题,其中单项选择和填空题主要考察学生对汇编语言基础知识的掌握程度,编程题则主要考察学生对实际问题的处理能力。 历年考试中,编程题主题涵盖了汇编语言的各个方面,如输入输出、数学运算、条件判断、循环控制和数组操作等,涉及到实际问题的解决和编程技巧的掌握。在编程题的设计中,难度逐年升高,难度适中,涵盖的知识点也更加广泛且实用。 此外,汇编语言期末考试还会考查学生对课程中实验的理解和应用,以及对常用工具集成开发环境MASM和汇编语言程序设计流程的掌握情况。这样的设计旨在促进学生在实际运用中理解和掌握汇编语言的基本原理,提高汇编语言程序设计的能力。 总之,福州大学汇编语言期末考试历年以实际操作为主要考核方式,通过提供各种实际问题设计编程题目,考察学生对汇编语言基础知识和实践经验的掌握情况,以此检验学生的实际运用能力和应用能力。 ### 回答2: 福州大学计算机系汇编语言是一门重要的计算机基础课程,教授学生如何理解计算机内部运行机制。该课程通常在每个学期末会进行考试,最终计入学生的总成绩中。 历年来,fzu汇编语言期末考试的难度相对较高,因此学生需要花费充分的时间和精力去复习,了解考试方向和内容。在考试中,学生需要熟悉汇编语言的基本概念和常用指令,能够写出程序并进行调试。 在考试内容上,历年来的考试题目都涵盖了汇编语言的总体知识点,例如CPU结构、数据存储方式、寻址方式、编程方法、中断和I/O等。考试题型包括选择题、填空题、简答题和编程题等多种形式。 考试中的复习重点包括:CPU基本结构和运行原理、汇编语言常用指令、寻址方式和程序调试方法等。由于该课程是计算机系的入门课程,因此对于学生后续学习计算机领域的课程和研究都至关重要。 总之,fzu汇编语言期末考试历年来难度较大,需要学生投入足够的时间和精力复习考试内容。但是学好这门课程对于学生通向计算机领域和进行相关领域研究具有极为重要的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值