zoukankan      html  css  js  c++  java
  • Construct a Matrix (矩阵快速幂+构造)

    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+5;
    typedef long long ll;
    using namespace std;
    struct Mat 
    {
        ll a[4][4];
    };
    
    int  mod;
    Mat Mul(Mat a,Mat b)
    {
        Mat ans;
        memset(ans.a,0,sizeof(ans.a));
        for(int t=0;t<3;t++)
        {
            for(int j=0;j<3;j++)
            {
                for(int k=0;k<3;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[0][0]=1;
        res.a[0][1]=1;
        res.a[0][2]=1;
        res.a[1][0]=0;
        res.a[1][1]=1;
        res.a[1][2]=1;
        res.a[2][0]=0;
        res.a[2][1]=1;
        res.a[2][2]=0;
    
        while(n)
        {
           if(n&1)
           {
               ans=Mul(res,ans);
           }
               res=Mul(res,res);
               n>>=1;
        }
        return ans.a[0][0];
    }
    int main()
    {
       int T;
       cin>>T;
       int n;
       int cnt=1;
       while(T--)
       {
           scanf("%d%d",&n,&mod);
           memset(ans.a,0,sizeof(ans.a));
        ans.a[0][0]=2;
        ans.a[1][0]=1;
        ans.a[2][0]=1;
           ll aa=quickpow(n-2)%mod;
           if(aa&1||aa==0)
           {
               printf("Case %d: No
    ",cnt++);
        }
        else
        {
            printf("Case %d: Yes
    ",cnt++);
            for(int t=0;t<aa;t++)
            {
                for(int j=0;j<aa;j++)
                {
                    if(t>j)
                    {
                       printf("-1 ");
                    }
                    if(t<j)
                    {
                        printf("1 ");
                    }
                    if(t==j&&t%2==0)
                    {
                        printf("-1 ");
                    }
                    if(t==j&&t%2==1)
                    {
                        printf("0 ");
                    }
                }
                printf("
    ");
            }
        }
       
       }
       return 0;
    }
  • 相关阅读:
    从零开始学CSS-overflow
    vue 高度自适应的问题处理
    子div在父div里居中
    IEC104协议规约解析
    Arduino编译总结
    通过golang小案例,了解golang程序常见机制
    用go实现常见的数据结构
    常见面试题整理,金三银四全靠它了
    golang知识总结
    .NET Core 基于 Grafana Loki 日志初体验
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10831791.html
Copyright © 2011-2022 走看看