Codeforces Round #779 (Div. 2)
观察发现两个0之间至少有2个1才能满足题意
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define ll long long
int T;
vector<int>Q;
int main(){
cin>>T;
while(T--){
int n;string s;
cin>>n;
cin>>s;
int ans=0;Q.clear();
for(int i=0;i<n;i++)
if(s[i]=='0')
Q.push_back(i);
if(Q.size()==0){
cout<<0<<endl;
continue;
}
for(int i=0;i<Q.size()-1;i++){
int l=Q[i],r=Q[i+1];
if((r-l-1)==0)ans+=2;
else if((r-l-1)==1)ans+=1;
}
cout<<ans<<endl;
}
return 0;
}
观察发现 因为是连续的【1,n】所以 gcd只能是2
这样奇数有 (n/2)! 个摆放方式 偶数也有(n/2)!个摆放方式
最后相乘
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x&(-x)
#define ll long long
const int mod=998244353;
int T;
int main(){
cin>>T;
while(T--){
int n;cin>>n;
if(n&1){
cout<<0<<endl;
continue;
}
else {
ll ans=1;
for(int i=1;i<=n/2;i++)
ans=ans*i%mod;
cout<<ans*ans%mod<<endl;
}
}
return 0;
}
首先我们能找到1 那个位置一定是最大的n 不妨先把n放在第一个位置
当我们依次在它前面插入一个数 c值要么+1 要么≤上一个插入的数c值
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
if (count(a.begin(), a.end(), 1) != 1) {
cout << "NO\n";
return;
}
int p = find(a.begin(), a.end(), 1) - a.begin();
rotate(a.begin(), a.begin() + p, a.end());
for (int i = 1; i < n; ++i) {
if (a[i] - a[i - 1] > 1) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t; cin >> t;
while (t--) solve();
return 0;
}