Codeforces Round #853 (Div. 2)

A. Serval and Mocha's Array

Mocha likes arrays, and Serval gave her an array consisting of positive integers as a gift.

Mocha thinks that for an array of positive integers 𝑎, it is good iff the greatest common divisor of all the elements in 𝑎 is no more than its length. And for an array of at least 2positive integers, it is beautiful iff all of its prefixes whose length is no less than 2 are good.

For example:

  • [3,6] is not good, because gcd(3,6)=3 is greater than its length 2.
  • [1,2,4]is both good and beautiful, because all of its prefixes whose length is no less than 22, which are [1,2]and [1,2,4], are both good.
  • [3,6,1]is good but not beautiful, because [3,6]is not good.

Now Mocha gives you the gift array 𝑎 of 𝑛 positive integers, and she wants to know whether array 𝑎 could become beautiful by reordering the elements in 𝑎. It is allowed to keep the array 𝑎 unchanged.

Input

Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤500). The description of the test cases follows.

The first line of each test case contains a single integer 𝑛(2≤𝑛≤100) — the length of array 𝑎

The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎1,𝑎2,…,𝑎𝑛≤106 — the elements of array 𝑎.

Output

For each test case, print Yes if it is possible to reorder the elements in 𝑎 to make it beautiful, and print No if not.

You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

Example

input

Copy

 
 

6

2

3 6

3

1 2 4

3

3 6 1

3

15 35 21

4

35 10 35 14

5

1261 227821 143 4171 1941

output

Copy

No
Yes
Yes
No
Yes
Yes

Note

In the first test case, neither [3,6] nor [6,3] are beautiful, so it's impossible to obtain a beautiful array by reordering the elements in 𝑎.

In the second test case, [1,2,4] is already beautiful. Keeping the array 𝑎 unchanged can obtain a beautiful array.

题意就是求每个不小于2的前缀的最大公约数都小于他的本身前缀长度 ,所以这里思考当两个数的最大公约数小于等于2时,他后面的都是小于等于2,反之当等于2时就不成立了

#include <iostream>

using namespace std;
constexpr int N=1e5+7;
typedef long long ll;
int _gcd(int a,int b){
    return b? _gcd(b,a%b):a;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,a[N];
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        bool st= false;
        for(int i=2;i<=n;i++){
            for(int j=1;j<i;j++){
                if(_gcd(a[i],a[j])<=2) st= true;
            }
        }
        if(st) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

                                B. Serval and Inversion Magic

Serval has a string 𝑠 that only consists of 0 and 1 of length 𝑛. The 𝑖-th character of 𝑠 is denoted as 𝑠𝑖, where 1≤𝑖≤𝑛.

Serval can perform the following operation called Inversion Magic on the string 𝑠�:

  • Choose an segment [𝑙,𝑟] (1≤𝑙≤𝑟≤𝑛). For 𝑙≤𝑖≤𝑟, change 𝑠𝑖 into 1 if 𝑠𝑖 is 0, and change 𝑠𝑖 into 0 if 𝑠𝑖 is 1.

For example, let 𝑠 be 010100 and the segment [2,5] is chosen. The string 𝑠 will be 001010 after performing the Inversion Magic.

Serval wants to make 𝑠 a palindrome after performing Inversion Magic exactly once. Help him to determine whether it is possible.

A string is a palindrome iff it reads the same backwards as forwards. For example, 010010 is a palindrome but 10111 is not.

Input

Each test contains multiple test cases. The first line contains the number of test cases 𝑡 (1≤𝑡≤104). The description of the test cases follows.

The first line of each test case contains a single integer 𝑛 (2≤𝑛≤105) — the length of string 𝑠.

The second line of each test case contains a binary string 𝑠 of length 𝑛. Only characters 0 and 1 can appear in 𝑠

It's guaranteed that the sum of 𝑛 over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print Yes if 𝑠 can be a palindrome after performing Inversion Magic exactly once, and print No if not.

You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

Example

input

Copy

 
 

3

4

1001

5

10010

7

0111011

output

Copy

Yes
Yes
No

Note

In the first test case, Serval can perform Inversion Magic on the segment [1,4]. The string 𝑠 will be 0110 after the magic.

In the second test case, Serval can perform Inversion Magic on the segment [1,3]. The string 𝑠 will be 01110 after the magic.

In the third test case, Serval can't make 𝑠 a palindrome by performing Inversion Magic exactly once.

 

能否通过改变一段区间,使该区间的1变为0,0变为1,是的该字符串对称,其实就是看该对称区间不相等的部分有几块,如果是超过1块,那么就变不回来了 

#include <iostream>
#include <cstring>
using namespace std;
constexpr int N=1e5+7;
typedef long long ll;
int _gcd(int a,int b){
    return b? _gcd(b,a%b):a;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        char  s[N];
        scanf("%d",&n);
        scanf("%s",s+1);
        int st= 0;
        int ans=0;
       int len= strlen(s+1);
       for(int i=1;i<=len/2;i++){
           if(s[i]!=s[len-i+1]&&st==0) {
               ans++;
               st= true;
           }
           if(s[i]==s[len-i+1]) st= false;
       }
       if(ans<=1) printf("YES\n");
       else printf("NO\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值