A 题目链接:https://codeforces.com/contest/1658/problem/A
input:
9
3
000
3
001
3
010
3
011
3
100
3
101
3
110
3
111
19
1010110000100000101
output:
4
2
1
0
2
0
0
0
17
题意:
给一串长度为 n 的01串,先要向其中插入若干个1,保证该串的所有子序列的0的数量不超过1,问最少插入几个1可以满足要求
思路:
签到,只需要保证两个0之间至少有2个1即可(防止010非法串)。
代码如下:
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
int t,n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
string s;
cin>>s;
int cnt=0;
int ans=0;
int i;
for(i=0;i<n;i++)
{
if(s[i]=='0')
break;
}
i++;
for(i;i<n;i++)
{
if(s[i]=='0')
{
if(cnt<2)
{
ans+=2-cnt;
}
cnt=0;
}
else
{
cnt++;
}
}
cout<<ans<<endl;
}
return 0;
}
B 题目链接:https://codeforces.com/contest/1658/problem/B
input:
7
1
2
3
4
5
6
1000
output:
0
1
0
4
0
36
665702330
题意:
给一个数n,问有多少种 1 ~ n 排列方式满足 gcd(1⋅p1,2⋅p2,…,n⋅pn)>1
思路:
把所有奇数放在偶数为上,将其变为偶数,即可满足要求。
分类讨论:
如果 n 为奇数,则无论如何排列,都会有一个奇数放置在奇数位,无法满足要求,直接输出0
如果 n 为偶数,排列方式数量为 ( n / 2 ) ! ^ 2,注意取模
代码如下:
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
const int N=1010;
const int mod=998244353;
int t,n;
ll func(ll k)
{
ll ans=1;
for(int i=1;i<=k;i++)
{
ans=(ans*i)%mod;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
if(n&1)
cout<<"0"<<endl;
else
cout<<(func(n/2)*func(n/2))%mod<<endl;
}
return 0;
}
C 题目链接:https://codeforces.com/contest/1658/problem/B
input:
6
1
1
2
1 2
2
2 2
6
1 2 4 6 3 5
6
2 3 1 2 3 4
3
3 2 1
output:
YES
YES
NO
NO
YES
NO
题意:
一个排列 p 的 i circle 排列 指将排列 p 的后 i 个元素放到排列前面,形成的排列。
一个排列对应的 b 数组,其中 bi 代表前 i 个元素的最大值
排列 p 的 power指其对应 b 数组中不同元素的个数
现在给出 c 数组,ci 代表 排列 p 的 i circle 排列 的power,问是否存在合法的排列 p ,满足给定的 c
思路:
首先看数组 c ,找到等于1的位置(等于一代表此时的第一位是最大值n),如果数组c不存在1或存在多个1,即为非法
然后从1开始向后按环遍历,直到循环到1的位置,每次查看下一位与当前相比是否大超过1,如果是即非法,否则合法
代码如下:
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
const int N=1e5+10;
int a[N];
int t,n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
cin>>n;
int count=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==1)
{
count++;
}
}
int index=2;
int pos=-1;
int i=0;
for(i;i<n;i++)
{
if(a[i]==1)
{
pos=i;
break;
}
}
if(count!=1)
{
cout<<"NO"<<endl;
continue;
}
int k=pos;
pos=(pos+1)%n;
bool f=1;
while(pos!=k)
{
if(a[pos]-a[(pos-1+n)%n]>1)
{
f=0;
}
pos=(pos+1)%n;
}
if(f)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
D1 题目链接:https://codeforces.com/contest/1658/problem/D1
input:
3
0 3
3 2 1 0
0 3
4 7 6 5
0 2
1 2 3
output:
0
4
3
题意:
给定 l , r,取值在 [ l , r ] ,长度为 r - l + 1 的某个排列 a ,该排列 a 所有元素与某个 x 异或形成了新的排列,现在给出这个新的排列,求解 x ,可能有多种答案,输出任意一种。
easy version,l == 0
思路:
开两个桶,对 l <= i <= r 分别记录最终 a[i] 每一二进制位 1 的数量 和 i 每一二进制位 1 的数量。
最后比较每一位两个桶的值,如果不相等,则 x 该位为 1,否则为 0
代码如下:
#include<bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int N=2e5+10;
int t,n;
int a[N],cnt1[20],cnt2[20];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--)
{
int l,r;
cin>>l>>r;
memset(cnt1,0,sizeof cnt1);
memset(cnt2,0,sizeof cnt2);
int k;
for(int i=l;i<=r;i++)
{
cin>>k;
for(int j=0;j<=17;j++)
{
if(k&(1<<j))
cnt1[j]++;
if(i&(1<<j))
cnt2[j]++;
}
}
int ans=0;
for(int i=0;i<=17;i++)
{
if(cnt1[i]!=cnt2[i])
{
ans+=(1<<i);
}
}
cout<<ans<<endl;
}
return 0;
}