列表

详情


NC17134. Symmetric Matrix

描述

Count the number of n x n matrices A satisfying the following condition modulo m.
* Ai, j ∈ {0, 1, 2} for all 1 ≤ i, j ≤ n.
* Ai, j = Aj, i for all 1 ≤ i, j ≤ n.
* Ai, 1 + Ai, 2 + ... + Ai, n = 2 for all 1 ≤ i ≤ n.
* A1, 1 = A2, 2 = ... = An, n = 0.

输入描述

The input consists of several test cases and is terminated by end-of-file.
Each test case contains two integers n and m.

输出描述

For each test case, print an integer which denotes the result.

示例1

输入:

3 1000000000
100000 1000000000

输出:

1
507109376

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

pypy3 解法, 执行用时: 211ms, 内存消耗: 29384K, 提交时间: 2021-06-01 22:05:21

while True:
	try:
		n, m = map(int, input().split())
		if n <= 3:
			print(0 if n <= 1 else 1)
			continue
		f = [0] * (n + 1)
		f[2] = f[3] = 1
		for i in range(4, n + 1):
			f[i] = ((i - 1) * f[i - 1] + (i - 1) * f[i - 2] - (i - 1) * (i - 2) // 2 * f[i - 3]) % m
		print(f[n] % m)
	except:
		break

C++11(clang++ 3.9) 解法, 执行用时: 97ms, 内存消耗: 1144K, 提交时间: 2020-08-18 21:15:46

#include<stdio.h>
long long f[100010]={0,0,1,1},n,p;
int main(){
    while(~scanf("%d%d",&n,&p)){
        for(long long i=4;i<=n;++i) f[i]=((i-1)*(f[i-1]+f[i-2])-((i-1)*(i-2))/2*f[i-3])%p;
        printf("%lld\n",(f[n]+p)%p);
    }
}

上一题