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;
    }
  • 相关阅读:
    >>> fout = open('output.txt', 'w') Traceback (most recent call last): File "<stdin>", line 1, in <module> PermissionError: [Errno 13] Permission denied: 'output.txt'
    Python元组术语
    Python元组与列表_元组与字典
    Python元组_参数长度可变
    Python元组_赋值与返回值
    Python元组_不可修改
    第二篇-bmob云端服务器的发现
    第一篇-关于语言与计划
    《JavaScript》JS中的常用方法attr(),splice()
    Java接口interface,匿名内部类
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10831791.html
Copyright © 2011-2022 走看看