fzu 1911 C. Construct a Matrix

原创 2013年12月04日 21:18:49

C. Construct a Matrix

1000ms
1000ms
32768KB
Special Judge
64-bit integer IO format: %I64d      Java class name: Main
Font Size:  
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

题意:求fib数列前n项和,以这个和%m作为矩阵的r,然后构造矩阵满足所有值为0,1 或 -1,并且每行每列和都不相等。

思路:第一步矩阵快速幂,第二步找规律。

代码:

#include <stdio.h>
#include <string.h>

const int N = 205;
int t, n, m, r;

struct mat {
    int v[3][3];
    mat() {
	memset(v, 0, sizeof(v));
    }
    mat operator * (mat &b) {
	mat c;
	for (int i = 0; i < 3; i ++)
	    for (int j = 0; j < 3; j ++)
		for (int k = 0; k < 3; k ++)
		    c.v[i][j] = (v[i][k] * b.v[k][j] + c.v[i][j]) % m;
	return c;
    }
};

mat pow_mod(mat a, int k) {
    if (k == 1 || k == 0)
	return a;
    mat c = pow_mod(a * a, k / 2);
    if (k & 1)
	c = c * a;
    return c;
}

void init() {
    scanf("%d%d", &n, &m);
    mat start;
    start.v[0][0] = start.v[0][1] = start.v[1][0] = start.v[2][0] = start.v[2][1] = start.v[2][2] = 1;
    if (n == 1)
	r = 1;
    else if (n == 2)
	r = 2;
    else {
	mat end = pow_mod(start, n - 2);
	r = (end.v[2][0] + end.v[2][1] + end.v[2][2] * 2) % m;
    }
}

void solve() {
    int s[N][N];
    memset(s, -1, sizeof(s));

    if (r == 0 || r % 2)
	printf("No\n");
    else {
	printf("Yes\n");
	for (int i = 1; i <= r; i++) {

	    if (i % 2) {
		int tmp = r / 2 + (i + 1) / 2;
		s[tmp][i] = 0;
		for (int j = tmp + 1; j <= r; j++)
		    s[j][i] = 1;
	    } else {
		int tmp = (r - i) / 2;
		for (int j = tmp + 1; j <= r; j++)
		    s[j][i] = 1;
	    }
	}

	for (int i = 1; i <= r; i++) {
	 //   int sum = 0;
	    for (int j = 1; j < r; j++) {
		printf("%d ", s[i][j]);
	//	sum += s[i][j];
	    }
	    printf("%d\n", s[i][r]);
	}

	/*
	for (int j = 1; j <= r; j++) {
	    int sum = 0;
	    for (int i = 1; i <= r; i++)
		sum += s[i][j];
	    printf("%d ", sum);
	}
	printf("\n");
	*/
    }
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t --) {
	init();
	printf("Case %d: ", ++cas);
	solve();
    }
    return 0;
}


相关文章推荐

fzu1911 (Construct a Matrix) 矩阵快速幂+构造

Construct a Matrix Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 32768KB Special Judge ...

FZU 1752 A^B mod C

Problem 1752 A^B mod C Accept: 840    Submit: 3625 Time Limit: 1000 mSec    Memory Limit : 32768...

Fzu1759 Super A^B mod C 题解

题目大意 以若干行的形式给定若干组a,b,c(你不知道有多少组),输出若干行,每行代表相应的abmodca^b \mod c的值。其中1≤a,c≤1000000000,1≤b≤1010000001 ...
  • t14t41t
  • t14t41t
  • 2015年05月09日 11:23
  • 617

fzu三月月赛C题。AC's code

 三点才知道有fzu月赛囧。。。一看别人都A了巨多题。。由于做了还没两小时,我只交了5道。。若爆了。。还好是中国人出的题目- -英语看下来都木有什么压力。这场比赛最后一题fft不会。 其余的除了C题之...
  • mznanan
  • mznanan
  • 2011年03月21日 15:55
  • 396

FZU 1759 Super A^B mod C (快速幂+指数循环节)

Problem 1759 Super A^B mod C Accept: 602 Submit: 2024 Time Limit: 1000 mSec Memory Limit : 3276...
  • steveyg
  • steveyg
  • 2014年08月13日 20:41
  • 607

数论 FZU 1759 Super A^B mod C

Super A^B mod C Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB   Problem ...

FZU 华容道(http://hi.baidu.com/chenwenwen0210/item/f084741a84c9dbee39cb3087)

FZU 华容道 题目描述:给定4*N的矩形格子。在里面填上1*2,2*1,1*1,2*2的块,其中2*2的真必需且并只能使用一次。其他的随意。 求使得4*N的格子全部填满的总方法数...
  • qianlv_
  • qianlv_
  • 2012年12月13日 13:12
  • 738

FZU 1759 Super A^B mod C (欧拉函数,快速幂,降幂公式)

题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 一道吓人的题。。 不禁再次感叹数学真伟大,使用下面的降幂公式很简单就写出来了。 phi是欧...

FZU 1759 Super A^B mod C

http://acm.fzu.edu.cn/problem.php?pid=1759 Problem 1759 Super A^B mod C  直接运用公式 //尼玛坑爹的_int64 C+...

FZU_1759 Super A^B mod C

http://acm.fzu.edu.cn/problem.php?pid=1759 题意: 求A^B % C的值,A,C 思路: 要解本题需要知道下面的公式:A^B = A^(B % ph...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:fzu 1911 C. Construct a Matrix
举报原因:
原因补充:

(最多只允许输入30个字)