【FOJ1911】 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;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/81604099