【CF补题】【AB】Codeforces Round #779 (Div. 2) C++代码

A. Marin and Photoshoot

【问题】对于每个片段,1 的数量都要大于0,

【解答】所以两个0之间必须有2个以上的1。

#include<bits/stdc++.h>
using namespace std;
int n;
string s;

//找到下一个0
int Find(int now)
{
	int nex = now + 1;
	while (s[nex] != '0'&& nex<n)
	{
		nex++;
	}
	return nex;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int ans = 0;
		cin >> n;
		cin >> s;
		int j = 0;
		while (j < n)
		{
			if (s[j] == '0')
			{
				int k = Find(j);
				if (k != n)
				{
					if (k - j <= 2)ans += 3 - (k - j);
					j = k - 1;//下一个
				}
			}
			j++;
		}
		cout << ans << '\n';
	}
}

--------------------------------------------------------------------------------------------------------------------------------

B. Marin and Anti-coprime Permutation

【问题】从1~n的n个数,放到1~n个位置,

求最大公约数大于1 的排列方式

【解答】首先考虑到有1在,所以必须奇数*偶数的组合才能有公约数2,公约数最小为3(or其他奇数)是不能满足的。所以首先n是偶数。接下来只要求1~n全排列偶数对奇数的组合数就可以了。也就是A(N/2,N/2) * A(N/2,N/2);(奇数全排列*偶数全排列)

#include<bits/stdc++.h>
using namespace std;
const int mo = 998244353;
int n;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		if (n % 2 == 1)
		{
			cout << 0 << '\n';
			continue;
		}
		long long ans = 1;
		for (int i = 1; i <= n / 2; i++)
		{
			ans = (ans * i) % mo;
		}
		cout << (ans * ans) % mo << '\n';
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值