以
P
i
P_i
Pi 代表前
i
i
i个字符
r
r
r出现的次数为奇数或偶数。
从
i
i
i到
j
j
j区间如果对答案具有贡献,则
p
j
−
p
i
−
1
p_j-p_{i-1}
pj−pi−1应为奇数。
这样,
p
i
p_i
pi出现的奇数次和偶数次应该相差最多1,此时答案达到最大。
则
a
n
s
=
⌈
n
+
1
2
⌉
⋅
⌊
n
+
1
2
⌋
ans=\left\lceil\dfrac{n+1}{2}\right\rceil \cdot \left\lfloor\dfrac{n+1}{2}\right\rfloor
ans=⌈2n+1⌉⋅⌊2n+1⌋ 。
接下来暴搜+剪枝就行。
#include<bits/stdc++.h>
using namespace std;
// clock_t start, end;
// start = clock();
// end = clock();
// cout << (double) (end - start) / CLOCKS_PER_SEC << endl;
//ios::sync_with_stdio(false);
#define int long long
#define rep(i, x, y) for(int i=(x);i<=(y);++i)
#define dep(i, x, y) for(int i=(x);i>=(y);--i)
#define gcd(a, b) __gcd(a,b)
const long long mod = 1e9 + 7;
const int maxn = 1e6 + 10;
int lowbit(int x) { return x & -x; }
bool ispow(int n) { return (n & (n - 1)) == 0; }//O(1) 判断是否是 2^k(2的k次方)
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int a[maxn];
int n;
char s[maxn];
int cnt=0;
int flag=0;
int p[maxn];
int dfs(int pos,int n1,int n2){
if(n1>cnt||n2 > cnt)
return 0;
if(flag==100)return 1;
if(pos==n+1){
flag++;
for(int i=1;i<=n;i++)cout<<s[i];
cout<<endl;
return 0;
}
int res=0;
s[pos]='b';
p[pos]=p[pos-1];
p[pos] == 1 ? res=dfs(pos + 1, n1 + 1, n2) : res=dfs(pos + 1, n1, n2 + 1);
if(res==1)return 1;
s[pos]='r';
p[pos-1]==1?p[pos]=2:p[pos]=1;
p[pos] == 1 ? res = dfs(pos + 1, n1 + 1, n2) : res = dfs(pos + 1, n1, n2 + 1);
if(res)return 1;
}
signed main() {
p[0]=2;
cin>>n;
int z=ceil((double)(n+1)/2);
cout<<z*((n+1)/2)<<endl;
cnt= z;
dfs(1,0,1);
return 0;
}