题目链接

C - Flippy Sequence(组合数学+分类讨论)

两区间异或一下,分段考虑,如果全为0则任选两相同区间,答案为$C_{n+1}^{2}=\frac{n(n+1)}{2}$,只有一段连续的1则两区间有一个公共边界,另外两个边界分别为连续1的左右边界,答案为$2C_{n-1}^{1}=2(n-1)$,有两段则两区间平分四个边界,答案为$C_{4}^{2}=6$,三段以上无解。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int N=1e6+10;
 5 char a[N],b[N];
 6 int n;
 7 ll ans;
 8 int main() {
 9     int T;
10     for(scanf("%d",&T); T--;) {
11         scanf("%d%s%s",&n,a,b);
12         for(int i=0; i<n; ++i)a[i]=a[i]==b[i]?'0':'1';
13         int t=0;
14         for(int i=0; i<n; ++i)if(a[i]=='1'&&a[i+1]!='1')++t;
15         if(t==0)ans=(ll)n*(n+1)/2;
16         else if(t==1)ans=(ll)(n-1)*2;
17         else if(t==2)ans=6;
18         else ans=0;
19         printf("%lld\n",ans);
20     }
21     return 0;
22 }
View Code

相关文章:

  • 2021-05-31
  • 2022-12-23
  • 2018-10-27
  • 2022-02-13
  • 2021-10-12
  • 2022-12-23
  • 2021-07-13
猜你喜欢
  • 2021-05-28
  • 2018-11-19
  • 2022-12-23
  • 2021-11-23
  • 2021-11-30
  • 2019-09-01
  • 2022-12-23
相关资源
相似解决方案