E-Sequence
题意:
将给定数组的(l,r)分成k份,满足每一份都是偶数。
题解:
因为
偶数
+
偶数
=
偶数
偶数+偶数=偶数
偶数+偶数=偶数,显然
∑
i
=
l
−
1
r
a
i
\sum_{i=l-1}^ra_i
∑i=l−1rai 是偶数。
继续分析
s
l
−
1
s_{l-1}
sl−1,
s
b
1
s_{b_1}
sb1,
s
b
2
s_{b_2}
sb2,…
s
r
s_{r}
sr 奇偶性相同,可利用前缀和记录奇偶性,并判断在(l,r)区间内是否存在k个与
s
l
−
1
s_{l-1}
sl−1 奇偶性相同的
s
x
s_{x}
sx。
分析原理:若此区间满足为偶数,因为
偶数
+
偶数
=
偶数
偶数+偶数=偶数
偶数+偶数=偶数,
偶数
+
奇数
=
奇数
偶数+奇数=奇数
偶数+奇数=奇数,所以
s
l
−
1
s_{l-1}
sl−1,
s
b
1
s_{b_1}
sb1,
s
b
2
s_{b_2}
sb2,…
s
r
s_{r}
sr奇偶性相同。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5 + 5;
int n, q, sum[N], cnt[N][2];
void solve()
{
memset(sum,0,sizeof sum);
memset(cnt,0,sizeof cnt);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
{
long long x;
cin>>x;
sum[i]=sum[i-1]^(x&1);
cnt[i][0]=cnt[i-1][0]+(sum[i]==0);
cnt[i][1]=cnt[i-1][1]+(sum[i]==1);
}
while(q--)
{
int l,r,k;
cin>>l>>r>>k;
if(sum[r]!=sum[l-1]) puts("NO");
else if(cnt[r][sum[l-1]]-cnt[l-1][sum[l-1]]>=k) puts("YES");
else puts("NO");
}
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}
G-Gcd
题解:
给定一个数组
s
=
x
,
y
s={{x,y}}
s=x,y
可以有一下操作
1.从数组中任意选择两个元素
a
,
b
a,b
a,b,并插入
g
c
d
(
a
,
b
)
gcd(a,b)
gcd(a,b)。
2.从数组中任意选择两个元素
a
,
b
a,b
a,b,并插入
a
−
b
a-b
a−b到数组中
若任意次操作后数组中可出现z,就输出YES,否则输出NO。
题解:
由裴蜀定理得,两个数辗转相减,只能得出这两个数最大公约数的倍数。
所以只需要判断z是否是gcd(a,b)的整数倍
#include<iostream>
#include<algorithm>
#include<cstring>
#define int long long
using namespace std;
int gcd(int x,int y)
{
if(y==0)return x;
return gcd(y,x%y);
}
void solve()
{
int x,y,z;
cin>>x>>y>>z;
if(x&&y&&z==0)
{
cout<<"NO"<<endl;
return ;
}
if(z%gcd(x,y)==0) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
signed main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}