Codeforces Round #745 (Div. 2) (A - C)

A. CQXYM Count Permutations

观察所以全排列可以得到满足题意的排列反过来一定不满足题意 所以我们可以求2n的全排列除以2 这里要求逆元 费马小定理可以说一下有

(a + b) % P = a % P + b % P true
(a - b) % P = a % P - b % P true
(a * b) % P = a % P * b % P true
(a / b) % P = a % P / b % P false

在求除数相余的时候要用逆元

费马小定理  a^(p-1)1 (mod p) 所以有 a^(p-2)1/a(mod p)

用快速幂求2的逆元即可

代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 10, P = 1e9 + 7;

inline int read()
{
	register int x = 0, k = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}

ll n, f[N];

void init()
{
	f[0] = 1;
	for (int i = 1; i < N; i ++ )
	{
		f[i] = (i * f[i - 1]) % P;
	}
}

ll qmi(int a, int k)
{
	ll res = 1;
	while (k)
	{
		if (k & 1) res = (res * a) % P;
		k >>= 1;
		a = (ll)a * a % P;
	}
	return res;
}

void solve()
{
	n = read();
	ll res = (ll)f[n * 2] * qmi(2, P - 2) % P;
	cout << res << endl;
}

int main()
{
	int t;
	t = read();

	init();
	while (t -- )
	{
		solve();
	}

	return 0;
}

B. Diameter of Graph

分类讨论即可
1 n个点构成连通图所需最少的边为n-1 且这时候直径最短为2 不含重边和自环条件下最多边为 (n-1) * n / 2 与其相等的时候直径最短为1

代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 10, P = 1e9 + 7;

inline ll read()
{
	register ll x = 0, k = 1;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}

ll n, m, k;

void solve()
{
	n = read(), m = read(), k = read();
	if (n == 1)
	{
		if (m > 0 || k - 1 < 1) puts("NO");
		else puts("YES"); 
		return ;
	}

	if (m == n * (n - 1) / 2) 
	{
		if (k - 1 > 1) puts("YES");
		else puts("NO");
	}
	else if (m >= n - 1 && m < n * (n - 1) / 2)
	{
		if (k - 1 > 2) puts("YES");
		else puts("NO");
	}
	else puts("NO");
}

int main()
{
	int t;
	t = read();

	while (t -- )
	{
		solve();
	}

	return 0;
}

C. Portal

直接暴力需要n^4的复杂度 我们用一个数组维护当前上下区间均一定的左区间为1右区间为k的最小操作数 即这个形状
在这里插入图片描述
然后再遍历一次k(左区间) 来找最小值更新最小值 从而使得复杂度变为n^3

代码

#include <bits/stdc++.h>

using namespace std;

const int N = 410;

inline int read()
{
	register int x = 0, k = 1;
	char c = getchar();
	while (c > '9' || c < '0') 
	{
		if (c == '-') k = -1;
		c = getchar();
	}
	while (c <= '9' && c >= '0')
	{
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return x * k;
}

int n, m;
char s[N];
int sum[N][N], f[N];

inline int Getsum(int lx, int ly, int rx, int ry)
{
	return sum[rx][ry] - sum[lx - 1][ry] - sum[rx][ly - 1] + sum[lx - 1][ly - 1];
}

inline void solve()
{
	int ans = 1e8;
	n = read(), m = read();
	for (int i = 1; i <= n; i ++ )
	{
		scanf("%s", s + 1);
		for (int j = 1; j <= m; j ++ ) 
			sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (s[j] == '1');
	}
	for (int i = 1; i != n; i ++ )
		for (int j = i + 4; j <= n; j ++ )
		{
			for (int k = 4; k <= m; k ++ )
				f[k] = Getsum(i + 1, 1, j - 1, k - 1) + (k - 1) * 2 - Getsum(i, 1, i, k - 1) - Getsum(j, 1, j, k - 1)	
					+ j - i - 1 - Getsum(i + 1, k, j - 1, k);
			for (int k = m - 1; k >= 4; k -- )
				f[k] = min(f[k], f[k + 1]);
			for (int k = 1; k <= m - 3; k ++ )
			{
				int cur = f[k + 3] - Getsum(i + 1, 1, j - 1, k) - (k << 1) + Getsum(i, 1, i, k) + Getsum(j, 1, j, k) - Getsum(i + 1, k, j - 1, k) + j - i - 1;
				ans = min(ans, cur);
			}
		}
	printf("%d\n", ans);
}

int main()
{
	int t;
	t = read();

	while (t -- )
	{
		solve();
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值