Construct a Matrix FZU - 1911 矩陣快速冪

題目鏈接:點我


 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

題意:

構造一個Sn * Sn 的矩陣,矩陣只能包含0, 1, -1 三個數字,並且矩陣的每一行每一列都是不一樣的.Sn是斐波那契數列前n項和 mod m, 問是否能構造這樣的矩陣

代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
using namespace std;

typedef long long LL;
LL n, m;

struct mat{
    LL a[3][3];
    mat(){memset(a, 0, sizeof(a)); }
    mat operator *(const mat q){
        mat c;
        for (int i = 1; i <= 2; ++i)
            for(int  k = 1;k <= 2; ++k)
            if(a[i][k])
        for(int  j = 1; j <= 2; ++j){
            c.a[i][j] += a[i][k] * q.a[k][j];
            if (c.a[i][j] >= m) c.a[i][j] %= m;
        }return c;
    }
};

mat qpow(mat x, LL n){
    mat ans;
    ans.a[1][1] = ans.a[2][2] = 1;
    while(n){
        if (n&1) ans =ans * x;
        x = x * x;
        n >>= 1;
    }return ans;
}

void construct(int r){
    printf("Yes\n");
    int ans[205][205];
    memset(ans,-1,sizeof(ans));
    int k = r / 2;
    for(int i = k + 1; i <= r; ++i)
        ans[i][i-k] = 0;
    for(int i = k+2; i <= r; ++i)
        for(int  j = 1; j <= i-k-1; ++j)
        ans[i][j] = 1;
    for(int i = 1; i <= k; ++i)
        for(int j = 1;j<=i; ++j)
        ans[i][r-j+1] = 1;
    for(int i = k+1; i <=r; ++i)
        for(int j = k+1; j<= r; ++j)
        ans[i][j] = 1;
    for(int i = 1; i <= r; ++i){
        for(int j = 1; j < r; ++j)
            printf("%d ",ans[i][j]);
        printf("%d\n",ans[i][r]);
    }
}

int main(){
    int t;
    scanf("%d", &t);
    int kase  = 0;
    while( t--){
        scanf("%lld %lld", &n, &m);
        printf("Case %d: ", ++kase);
        mat ans;
        ans.a[1][1] = ans.a[1][2] = ans.a[2][1] = 1;
        ans = qpow(ans,n);
        int r = (ans.a[1][1] - 1 + ans.a[1][2] ) % m;
        if (r & 1|| r == 0)
            printf("No\n");
        else
            construct(r);
    }return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章