Codeforces Round #843 (Div. 2)A-D
A.Gardener and the Capybaras
题解:将只有ab组成的字符串分为三部分,要求中间的这部分要么是三者中最大的,要么是最小的。如果该字符串中间有a出现,则直接分解为左边部分+a+右边部分,若中间没有a出现,则分解为左端一个+中间部分+右端一个。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6;
int a[N],b[N],n,m,x,y,z,t,k,q;
int ans,res;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
cin>>T;
while(T--)
{
string s;
cin>>s;
bool ok=false;
for(int i=1; i<s.size()-1; i++)
{
if(s[i]=='a')
{
ok=true;
break;
}
}
if(ok)
{
for(int i=0; i<s.size(); i++)
{
if(i!=0&&i!=s.size()-1&&s[i]=='a'&&ok)
{
cout<<" "<<s[i]<<" ";
ok=false;
}
else
cout<<s[i];
}
}
else
{
cout<<s[0]<<" ";
for(int i=1; i<s.size()-1; i++)
{
cout<<s[i];
}
cout<<" "<<s[s.size()-1];
}
cout<<"\n";
}
return 0;
}
B. Gardener and the Array
题解:统计所有数出现的次数,依次检查某一个数删去之后是否有数的出现次数等于0。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6;
int a[N],b[N],c[N],n,m,x,y,z,t,k,q;
int ans,res;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
cin>>T;
while(T--)
{
cin>>n;
vector<int>v[n+10];
map<int,int>mp;
for(int i=1;i<=n;i++)
{
cin>>x;
while(x--)
{
cin>>y;
v[i].push_back(y);
mp[y]++;
}
}
bool ok=true;
for(int i=1;i<=n;i++)
{
ok=true;
for(auto x:v[i])
{
mp[x]--;
if(mp[x]==0)
ok=false;
}
if(ok)
break;
for(auto x:v[i])
{
mp[x]++;
}
}
if(ok)
cout<<"Yes"<<"\n";
else
cout<<"No"<<"\n";
}
return 0;
}
C. Interesting Sequence
题解:每次&上n的lowbit,暴力往上加。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5;
int n,x;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
cin>>T;
while(T--)
{
cin>>n>>x;
int sum=n;
while(sum!=x&&sum)
{
n+=n&(n^(n-1));
sum&=n;
}
if(sum==0&&x)cout<<-1<<"\n";
else cout<<n<<"\n";
}
return 0;
}
D. Friendly Spiders
题解:将每个数进行质因数分解,然后对所有质因数存储所拥有的值的编号,进行BFS,用pre记录BFS的路径,最后在t到s的路径中记录编号值和个数,个数即为最短路,依次编号为路径。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6;
int n,x,m,a[N],s,t;
int ans,res;
vector<int>v[N];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
//cin>>T;
while(T--)
{
cin>>n;
m=0;
for(int i=1; i<=n; i++)
{
cin>>a[i];
m=max(m,a[i]);
}
vector<int>minp(m+10),primes;
for(int i=2;i<=m;i++)
{
if(!minp[i])
{
minp[i]=i;
primes.push_back(i);
}
for(auto p:primes)
{
if(i*p>m)break;
minp[i*p]=p;
if(p==minp[i])break;
}
}
cin>>s>>t;
if(s==t)
{
cout<<1<<"\n";
cout<<s;
return 0;
}
if(a[s]==1)
{
cout<<-1<<"\n";
return 0;
}
if(a[s]==a[t])
{
cout<<2<<"\n";
cout<<s<<" "<<t;
return 0;
}
vector<vector<int> >v(m+1);
for(int i=1;i<=n;i++)
{
for(int x=a[i];x>1;x/=minp[x])
{
v[minp[x]].push_back(i);
}
}
vector<int>dis(n+m+10,-1),pre(n+m+10);
queue<tuple<int,int,int> >q;
q.push({s,0,-1});
while(!q.empty())
{
auto [u,d,p]=q.front();
q.pop();
if(dis[u]!=-1)continue;
dis[u]=d;
pre[u]=p;
if(u<=n)
{
for(int x=a[u];x>1;x/=minp[x])
{
q.push({n+minp[x],d+1,u});
}
}
else
{
for(auto x:v[u-n])
{
q.push({x,d+1,u});
}
}
}
if(dis[t]==-1)
{
cout<<-1;
return 0;
}
vector<int>ans;
for(int i=t;i!=-1;i=pre[i])
{
if(i<=n)
ans.push_back(i);
}
cout<<ans.size()<<"\n";
for(int i=ans.size()-1;i>=0;i--)
{
cout<<ans[i]<<" ";
}
}
return 0;
}