bzoj 4563 [Haoi2016]放棋子 错位排列+高精度

题面

题目传送门

解法

可以直接将所有1全部放置在主对角线上,这样并不会影响答案

然后就是一个比较简单的错位排列了

需要高精度

代码

#include <bits/stdc++.h>
using namespace std;
template <typename node> void read(node &x) {
	x = 0; int f = 1; char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
	while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
int a[20010];
string operator ^ (string a, string b) {
	int l1 = a.size(), l2 = b.size();
	if (l1 > l2) swap(a, b), swap(l1, l2);
	for (int i = 1; i <= l2 - l1; i++) a = '0' + a;
	int k = 0; string ret = "";
	for (int i = l2 - 1; i >= 0; i--) {
		int t = (a[i] - '0') + (b[i] - '0') + k;
		if (t > 9) k = 1, t -= 10; else k = 0;
		ret = (char)(t + '0') + ret;
	}
	if (k) ret = '1' + ret;
	return ret;
}
string operator * (string x, int y) {
	int l = x.size();
	memset(a, 0, sizeof(a));
	for (int i = 0; i < l; i++) a[l - i - 1] = x[i] - '0';
	for (int i = 0; i < l + 10; i++) a[i] *= y;
	for (int i = 0; i < l + 10; i++) a[i + 1] += a[i] / 10, a[i] %= 10;
	string ret = "";
	for (int i = 0; i <= l + 10; i++) ret = (char)(a[i] + '0') + ret;
	while (ret.size() > 1 && ret[0] == '0') ret.erase(ret.begin());
	return ret;
}
int main() {
	int n; read(n);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			int x; read(x);
		}
	string x = "0", y = "1";
	for (int i = 3; i <= n; i++) {
		string tmp = (x ^ y) * (i - 1);
		x = y, y = tmp;
	}
	cout << y << "\n";
	return 0;
}

posted @ 2018-08-14 23:13  谜のNOIP  阅读(117)  评论(0编辑  收藏  举报