-
codeforces401C
给出0和1的个数,求出数列,1不能有三个连续,0不能有两个连续
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=200000+5;
int main()
{
int n,m,t;
scanf("%d%d",&n,&m);
if(m%2==0) t=m/2-1;
else t=m/2;
if(n>=t&&n<=m+1)
{
while(n>0||m>0)
{
if(n==m+1)
{
printf("0");
n--;
}
else if(n>0&&m>n&&m<=2*n+2)
{
printf("110");
m-=2;
n--;
}
else
{
printf("1");
m--;
}
}
printf("\n");
}
else
{
printf("-1\n");
}
return 0;
}
-
codeforces999B
For example, the above algorithm applied to the string ss ="codeforces" leads to the following changes: "codeforces" →→ "secrofedoc" →→ "orcesfedoc" →→ "rocesfedoc" →→ "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1).
每次除以二,除以二,即取字符串长度的因子,用reverse(s.begin(),s.end()),开始和结束的位置。
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=200000+5;
int main()
{
string s;
int n;
scanf("%d",&n);
cin>>s;
int j=1;
while(j<=n)
{
if(n%j==0)
{
reverse(s.begin(),s.begin()+j);
}
j++;
}
cout<<s<<endl;
return 0;
}