题解归档 - cf2226B
本文最后由方少年更新于2026 年 6 月 28 日,已超过0天没有更新。如果文章内容或图片资源失效,请留言反馈,将会及时处理,谢谢!
题解归档 - cf2226B
本文由 cf-code 本地题解库自动归档;公开内容以本地 AC/验证版本为准。
- 本地编号:
cf2226B - 本地来源:
problems/cf2226B/idea.md - 题目链接:https://codeforces.com/contest/2226/problem/B
- 原始标题:cf2226B - Everything Everywhere
思路
cf2226B - Everything Everywhere
pattern: gcd / range width restriction.
claim: if a good subarray has width d=max-min, then every element is divisible by d. Inside [min,max] there are only two possible multiples of d, namely min and max, because max-min=d. Since the array is a permutation, any good subarray must have length exactly 2.
For adjacent pair (x,y), with mn=min(x,y) and d=|x-y|, gcd(x,y)=gcd(mn,d)=d iff d divides mn.
check: count adjacent pairs satisfying mn % d == 0.
代码
来源:problems/cf2226B/solution.cpp
/* Author: likely
* Time: 2026-06-27
**/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
vector<int>p(n);
for(int &x:p) cin>>x;
ll ans=0;
for(int i=0;i+1<n;i++){
int mn=min(p[i],p[i+1]);
int d=abs(p[i]-p[i+1]);
if(mn%d==0) ans++;
}
cout<<ans<<"\n";
}
return 0;
}
~ ~ The End ~ ~
文章标题:题解归档 - cf2226B
文章链接:https://www.fangshaonian.cn/archives/240/
最后编辑:2026 年 6 月 28 日 19:05 By 方少年
许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)