Codeforces Round 868 (Div. 2) 题解

A. A-characteristic

Consider an array a1,a2,…,anconsisting of numbers 1 and −1. Define A-characteristic of this array as a number of pairs of indices 1≤i<j≤n such that ai⋅aj=1

Find any array a with given length n with A-characteristic equal to the given value k.

Input

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

The only line of each test case contains two integers n and k (2≤n≤100; 0≤k≤(n−1)n2— the length of required array and required A-characteristic.

Output

For each test case, if there is no array a with given A-characteristic k, print NO.

Otherwise, print YES and n numbers 1 and −1, which form the required array a. If there are multiple answers, print any of them.

Example

input

7

2 0

2 1

3 1

3 2

3 3

5 4

5 5

output

YES
1 -1 
YES
1 1 
YES
1 -1 1 
NO
YES
1 1 1 
YES
-1 1 -1 1 1 
NO

Note

In the first test case, there is only one pair of different elements in the array, and their product is a1⋅a2=−1≠1, hence its A-characteristic is 0.

In the second test case, there is only one pair of different elements in the array, and their product is a1⋅a2=1, hence its A-characteristic is 11.

In the third test case, there are three pairs of different elements in the array, and their product are: a1⋅a2=−1, a1⋅a3=1, a2⋅a3=−1, hence its A-characteristic is 1.

In the fourth test case, we can show, that there is no array with length 33, which A-characteristic is 2.

题意:

给一个长度为n,由1和-1组成的数组,其中 有且仅有 k对数的乘积为1,找出这个数组

思路:

注意到n的范围很小,暴力枚举1的个数,剩下的就是-1,计算出当前情况下乘积为1的数对个数,比较即可

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int f(int x){
    return x*(x-1)/2;
}

void solve(){
    int n,k;
    cin>>n>>k;
    for(int i=0;i<=n;i++){    //枚举
        if(f(i)+f(n-i)==k){
            cout<<"YES"<<endl;
            for(int j=0;j<i;j++){
                cout<<1<<' ';
            }
            for(int j=0;j<n-i;j++){
                cout<<-1<<' ';
            }
            cout<<endl;
            return;
        }
    }
    cout<<"NO"<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }

    return 0;
}

B. Sort with Step

Let's define a permutation of length n as an array p of length n, which contains every number from 1 to n exactly once.

You are given a permutation p1,p2,…,pn and a number k. You need to sort this permutation in the ascending order. In order to do it, you can repeat the following operation any number of times (possibly, zero):

  • pick two elements of the permutation pi and pj such that |i−j|=k, and swap them.

Unfortunately, some permutations can't be sorted with some fixed numbers k. For example, it's impossible to sort [2,4,3,1] with k=2.

That's why, before starting the sorting, you can make at most one preliminary exchange:

  • choose any pair pi and pj and swap them.

Your task is to:

  1. check whether is it possible to sort the permutation without any preliminary exchanges,
  2. if it's not, check, whether is it possible to sort the permutation using exactly one preliminary exchange.

For example, if k=2 and permutation is [2,4,3,1], then you can make a preliminary exchange of p1 and p4, which will produce permutation [1,4,3,2], which is possible to sort with given k.

Input

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

The first line of each test case contains two integers n and k (2≤n≤2⋅105; 1≤k≤n−1) — length of the permutation, and a distance between elements that can be swapped.

The second line of each test case contains n integers p1,p2,…,pn (1≤pi≤n) — elements of the permutation p.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case print

  • 0, if it is possible to sort the permutation without preliminary exchange;
  • 1, if it is possible to sort the permutation with one preliminary exchange, but not possible without preliminary exchange;
  • -1, if it is not possible to sort the permutation with at most one preliminary exchange.

Example

input

6

4 1

3 1 2 4

4 2

3 4 1 2

4 2

3 1 4 2

10 3

4 5 9 1 8 6 10 2 3 7

10 3

4 6 9 1 8 5 10 2 3 7

10 3

4 6 9 1 8 5 10 3 2 7

output

0
0
1
0
1
-1

Note

In the first test case, there is no need in preliminary exchange, as it is possible to swap (p1,p2)and then (p2,p3)

In the second test case, there is no need in preliminary exchange, as it is possible to swap (p1,p3) and then (p2,p4).

In the third test case, you need to apply preliminary exchange to (p2,p3) After that the permutation becomes [3,4,1,2][3,4,1,2] and can be sorted with k=2.

题意:

给你一个排列,定义两种交换,交换1为交换两个相邻k个单位的数字,交换2为交换任意两个数字,问:能否只做一次交换2是排列变为递增排列

思路:

将排列分为k个 模k同余类,如果当前位置和该位置的数在一个同余类,不需要做交换2,否则需要

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

void solve(){
    int n,k;
    cin>>n>>k;
    int num=0;
    for(int i=1;i<=n;i++){
        int a;
        cin>>a;
        num+=(a%k!=i%k);
    }
    if(num==0) cout<<0<<endl;
    else if(num==2)cout<<1<<endl;
    else cout<<-1<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }

    return 0;
}

 C. Strongly Composite

A prime number is an integer greater than 1, which has exactly two divisors. For example, 7 is a prime, since it has two divisors {1,7}. A composite number is an integer greater than 1, which has more than two different divisors.

Note that the integer 1 is neither prime nor composite.

Let's look at some composite number v. It has several divisors: some divisors are prime, others are composite themselves. If the number of prime divisors of v is less or equal to the number of composite divisors, let's name v as strongly composite.

For example, number 12 has 6 divisors: {1,2,3,4,6,12}, two divisors 2 and 3 are prime, while three divisors4, 6 and 12 are composite. So, 12 is strongly composite. Other examples of strongly composite numbers are 4,8 9, 16 and so on.

On the other side, divisors of 15 are {1,3,5,15}: 3 and 5 are prime, 15 is composite. So, 15 is not a strongly composite. Other examples are: 2, 3, 5, 6, 7, 10 and so on.

You are given n integers a1,a2,…,an (ai>1). You have to build an array b1,b2,…,bksuch that following conditions are satisfied:

  • Product of all elements of array a is equal to product of all elements of array b: a1⋅a2⋅…⋅an=b1⋅b2⋅…⋅bk;
  • All elements of array bare integers greater than 1 and strongly composite;
  • The size kof array b is the maximum possible.

Find the size k of array b, or report, that there is no array b satisfying the conditions.

Input

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

The first line of each test case contains one integer n (1≤n≤1000) — the size of the array a.

The second line of each test case contains n integer a1,a2,…an (2≤ai≤107) — the array a itself.

It is guaranteed that the sum of n over all test cases does not exceed 1000.

Output

For each test case, print the size k of array b, or 0, if there is no array b satisfying the conditions.

Example

input

8

2

3 6

3

3 4 5

2

2 3

3

3 10 14

2

25 30

1

1080

9

3 3 3 5 5 5 7 7 7

20

12 15 2 2 2 2 2 3 3 3 17 21 21 21 30 6 6 33 31 39

output

1
1
0
2
2
3
4
15

Note

In the first test case, we can get array b=[18]: a1⋅a2=18=b1; 1818 is strongly composite number.

In the second test case, we can get array b=[60]: a1⋅a2⋅a3=60=b1; 6060 is strongly composite number.

In the third test case, there is no array b satisfying the conditions.

In the fourth test case, we can get array b=[4,105]: a1⋅a2⋅a3=420=b1⋅b2 4 and 105 are strongly composite numbers.

题意:

定义一种强合数,它的合数因数的个数大于质数因数的个数

给一个数列a,找出一个符合以下条件的最长的数列b,求b的长度(0表示不存在)

1.元素全部为强合数

2.所有元素乘积等于a中所有元素的乘积

思路:

找规律发现,质数的平方,以及三个质数的乘积 为强合数,为了使元素个数最大,我们使b中元素尽可能是这两种数

我们把a中元素拆成若干个质数的乘积,然后统计起来,计算b长度即可

代码:

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int prime[10000005];
int visit[10000005];	//0表示质数
void Prime(){   //素数筛
    for (int i=2;i<=10000005;i++) {
        if(!visit[i]){
            prime[++prime[0]]=i;
        }
        for(int j=1;j<=prime[0]&&i*prime[j]<=10000005;j++){
            visit[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}

void solve(){
    int n;
    cin>>n;
    map<int,int>mm;
    while(n--){
        int a;
        cin>>a;
        for(int i=2;a>1;i++){
            while(a%i==0){
                if(mm.find(i)==mm.end())mm[i]=1;
                else mm[i]++;
                a/=i;
            }
            if(visit[a]==0){        //剩余部分为质数退出循环
                if(mm.find(a)==mm.end())mm[a]=1;
                else mm[a]++;
                break;
            }
        }
    }
    int ans=0,cnt=0;
    for(auto it:mm){
        ans+=it.second/2;
        cnt+=it.second%2;
    }
    ans+=cnt/3;
    cout<<ans<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    Prime();
    int T=1;
    cin>>T;
    while(T--){
        solve();
    }

    return 0;
}

D. Unique Palindromes

A palindrome is a string that reads the same backwards as forwards. For example, the string abcba is palindrome, while the string abca is not.

Let p(t)be the number of unique palindromic substrings of string t i. e. the number of substrings t[l…r] that are palindromes themselves. Even if some substring occurs in t several times, it's counted exactly once. (The whole string t is also counted as a substring of t).

For example, string t == abcbbcabcb has p(t)=6 unique palindromic substrings: a, b, c, bb, bcb and cbbc.

Now, let's define p(s,m)=p(t) where t=s[1…m]. In other words, p(s,m) is the number of palindromic substrings in the prefix of s of length m. For example, p(abcbbcabcb,5),5) == p.

You are given an integer n and k "conditions" (k≤20). Let's say that string s, consisting of n lowercase Latin letters, is good if all k conditions are satisfied at the same time. A condition is a pair (xi,ci) and have the following meaning:

  • p(s,xi)=ci, i. e. a prefix of s of length xi contains exactly ci unique palindromic substrings

Find a good string s or report that such s doesn't exist.

Look in Notes if you need further clarifications.

Input

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

The first line of each test case contains two integers n and k (3≤n≤2⋅105; 1≤k≤20) — length of good string s and number of conditions.

The second line of each test case contains k integers x1,x2,…,xk (3≤x1<x2<⋯<xk=n) where xi is the length of the prefix in the i-th condition.

The third line of each test case contains k integers c1,c2,…,ck (3≤c1≤c2≤⋯≤ck≤min(109,(n+1)n2) where ci is the number of palindromic substrings in the i-th condition.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case, if there is no good string s of length n that satisfies all conditions, print NO.

Otherwise, print YES and a string s of length n, consisting of lowercase Latin letters, that satisfies all conditions. If there are multiple answers, print any of them.

Example

input

7

10 2

5 10

5 6

3 1

3

3

4 2

3 4

3 3

4 2

3 4

3 4

4 1

4

5

10 3

4 6 10

4 5 8

10 4

4 6 7 10

4 5 7 8

output

YES
abcbbcabcb
YES
foo
YES
ayda
YES
wada
NO
YES
abcbcacbab
NO

题意:

找一个长度为n的字符串,满足k个约束条件:

前 bi 个字符能构成的所有子串中,回文串的个数为ci个

思路:

首先我们发现几个规律

1.长度为n的字符串的所有子串中中,回文串最大个数为n,n个连续字母时可以

2.字符串长度每增加1,回文串个数最多增加1

3.由abc三个字母依次重复构成的字符串,回文串个数为3,且 ci>=3

4.约束条件个数不超过20,小于小写字母的个数

基于这几个规律,我们发现如果违反前两条,一定不存在

否则对于每一个约束条件,我们增加一个字母来构造

从bi处往前填充上 比上一个约束条件增加的回文串个数,其他位置abc循环填充

代码:

#include<bits/stdc++.h>
using namespace std;

void solve(){
    int n,k;
    cin>>n>>k;
    string s="";
    for(int i=0;i<=n;i++){
        s+='.';
    }
    
    vector<int>b(k+1),c(k+1);
    for(int i=1;i<=k;i++)cin>>b[i];
    for(int i=1;i<=k;i++)cin>>c[i];
    
    int tmp=0;
    for(int i=1;i<=k;i++){  //判掉不符题意的串
        if(c[i]+tmp>b[i]){
            cout<<"NO\n";
            return;
        }
        tmp=b[i]-c[i];
    }
    
    c[0]=3;
    for(int i=1;i<=k;i++){  //填充新增回文串
        for(int j=0;j<c[i]-c[i-1];j++){
            s[b[i]-j]='d'+i;
        }
    }
    
    int cnt=0;
    for(int i=1;i<=n;i++){  //循环abc
        if(s[i]=='.')s[i]='a'+(cnt++)%3;
    }
    
    cout<<"YES"<<endl;
    cout<<s.substr(1)<<endl;
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Auroraaaaaaaaaaaaa

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

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

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

打赏作者

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

抵扣说明:

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

余额充值