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';
}
}