Codeforces Round #827 (Div. 4) D - F

D. Coprime

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Given an array of nn positive integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000). Find the maximum value of i+ji+j such that aiai and ajaj are coprime,†† or −1−1 if no such ii, jj exist.

For example consider the array [1,3,5,2,4,7,7][1,3,5,2,4,7,7]. The maximum value of i+ji+j that can be obtained is 5+75+7, since a5=4a5=4 and a7=7a7=7 are coprime.

†† Two integers pp and qq are coprime if the only positive integer that is a divisor of both of them is 11 (that is, their greatest common divisor is 11).

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤101≤t≤10) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the array.

The following line contains nn space-separated positive integers a1a1, a2a2,..., anan (1≤ai≤10001≤ai≤1000) — the elements of the array.

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

Output

For each test case, output a single integer  — the maximum value of i+ji+j such that ii and jj satisfy the condition that aiai and ajaj are coprime, or output −1−1 in case no ii, jj satisfy the condition.

Example

input

Copy

 

6

3

3 2 1

7

1 3 5 2 4 7 7

5

1 2 3 4 5

3

2 2 4

6

5 4 3 15 12 16

5

1 2 2 3 6

output

Copy

6
12
9
-1
10
7

Note

For the first test case, we can choose i=j=3i=j=3, with sum of indices equal to 66, since 11 and 11 are coprime.

For the second test case, we can choose i=7i=7 and j=5j=5, with sum of indices equal to 7+5=127+5=12, since 77 and 44 are coprime.

题意:

给一个长度为n的数组,问其中是否有两个元素互质,输出满足元素的下表和的最大值,没有则输出-1。

思路:

由于n是2e5,所以暴力搜索一定会TL,但是数组元素是1-1000,所以可以对数组元素进行搜索。

代码如下:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
const int M = 2e5 + 10;
int a[M];
int f[M];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        cin >> n;
        memset(f,0,sizeof(f));
        int ans = -1;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&a[i]);
            f[a[i]] = i;
        }
        for(int i = 1; i <= N; i++)
        {
            if(f[i])
            {
                for(int j = 1; j <= N; j++)
                {
                    if(f[j] && __gcd(i,j) == 1)
                    {
                        ans = max(ans,f[i] + f[j]);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

-------------------------------------------------------------------------------------------------------------------------------- 

E. Scuza

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Timur has a stairway with nn steps. The ii-th step is aiai meters higher than its predecessor. The first step is a1a1 meters higher than the ground, and the ground starts at 00 meters.

The stairs for the first test case.

Timur has qq questions, each denoted by an integer k1,…,kqk1,…,kq. For each question kiki, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length kiki. Timur can only climb the jj-th step if his legs are of length at least ajaj. In other words, ki≥ajki≥aj for each step jj climbed.

Note that you should answer each question independently.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases.

The first line of each test case contains two integers n,qn,q (1≤n,q≤2⋅1051≤n,q≤2⋅105) — the number of steps and the number of questions, respectively.

The second line of each test case contains nn integers (1≤ai≤1091≤ai≤109) — the height of the steps.

The third line of each test case contains qq integers (0≤ki≤1090≤ki≤109) — the numbers for each question.

It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105, and the sum of qq does not exceed 2⋅1052⋅105.

Output

For each test case, output a single line containing qq integers, the answer for each question.

Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Example

input

3

4 5

1 2 1 5

1 2 4 9 10

2 2

1 1

0 1

3 1

1000000000 1000000000 1000000000

1000000000

output

1 4 4 9 9 
0 2 
3000000000 

Note

Consider the first test case, pictured in the statement.

  • If Timur's legs have length 11, then he can only climb stair 11, so the highest he can reach is 11 meter.
  • If Timur's legs have length 22 or 44, then he can only climb stairs 11, 22, and 33, so the highest he can reach is 1+2+1=41+2+1=4 meters.
  • If Timur's legs have length 99 or 1010, then he can climb the whole staircase, so the highest he can reach is 1+2+1+5=91+2+1+5=9 meters.

In the first question of the second test case, Timur has no legs, so he cannot go up even a single step. :(

 题意:

给你一个长度为n的数组,ai代表i台阶比i- 1台阶高了多少,最开始在地上,有q次提问,每次提问都有输入一个数x,表示腿长(自己能到达的高度),对于每次询问都输出能到达的最高高度。

思路:

创建一个sum数组,让sum数组作为数组a的前缀和,再创建一个b数组,b[i] = max(b[i - 1],a[i]),代表要上到台阶i所需要的腿长,对于每一个x,二分搜索b中第一个比x大的数b[i],则sum[i]就是答案。

代码如下:

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

const int N = 2e5 + 10;
long long int a[N],b[N],sum[N],c[N];

int main() {
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		memset(sum,0,sizeof(sum));
		memset(c,0,sizeof(c));
		for(int i = 1; i <= n; i++)
		{
			scanf("%d",&a[i]);
			sum[i] = sum[i - 1] + a[i];
			c[i] = max(c[i - 1],a[i]);
		}	
		for(int i = 1; i <= m; i++)
		{
			scanf("%d",&b[i]);
		}
		for(int i = 1; i <= m; i++)
		{
				int j = upper_bound(c + 1, c + n + 1, b[i]) - c;
				printf("%lld ",sum[j - 1]);
		}	
		printf("\n");
	}	
	return 0;
}

——————————————————————————————————————————— 

F. Smaller

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Alperen has two strings, ss and tt which are both initially equal to "a".

He will perform qq operations of two types on the given strings:

  • 1 k x — Append the string xx exactly kk times at the end of string ss. In other words, s:=s+x+⋯+xk timess:=s+x+⋯+x⏟k times.
  • 2 k x — Append the string xx exactly kk times at the end of string tt. In other words, t:=t+x+⋯+xk timest:=t+x+⋯+x⏟k times.

After each operation, determine if it is possible to rearrange the characters of ss and tt such that ss is lexicographically smaller†† than tt.

Note that the strings change after performing each operation and don't go back to their initial states.

†† Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string pp is lexicographically smaller than string qq if there exists a position ii such that pi<qipi<qi, and for all j<ij<i, pj=qjpj=qj. If no such ii exists, then pp is lexicographically smaller than qq if the length of pp is less than the length of qq. For example, abdc<abeabdc<abe and abc<abcdabc<abcd, where we write p<qp<q if pp is lexicographically smaller than qq.

Input

The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains an integer qq (1≤q≤105)(1≤q≤105) — the number of operations Alperen will perform.

Then qq lines follow, each containing two positive integers dd and kk (1≤d≤21≤d≤2; 1≤k≤1051≤k≤105) and a non-empty string xx consisting of lowercase English letters — the type of the operation, the number of times we will append string xx and the string we need to append respectively.

It is guaranteed that the sum of qq over all test cases doesn't exceed 105105 and that the sum of lengths of all strings xx in the input doesn't exceed 5⋅1055⋅105.

Output

For each operation, output "YES", if it is possible to arrange the elements in both strings in such a way that ss is lexicographically smaller than tt and "NO" otherwise.

Example

input

3

5

2 1 aa

1 2 a

2 3 a

1 2 b

2 3 abca

2

1 5 mihai

2 2 buiucani

3

1 5 b

2 3 a

2 4 paiu

output

YES
NO
YES
NO
YES
NO
YES
NO
NO
YES

Note

In the first test case, the strings are initially s=s= "a" and t=t= "a".

After the first operation the string tt becomes "aaa". Since "a" is already lexicographically smaller than "aaa", the answer for this operation should be "YES".

After the second operation string ss becomes "aaa", and since tt is also equal to "aaa", we can't arrange ss in any way such that it is lexicographically smaller than tt, so the answer is "NO".

After the third operation string tt becomes "aaaaaa" and ss is already lexicographically smaller than it so the answer is "YES".

After the fourth operation ss becomes "aaabb" and there is no way to make it lexicographically smaller than "aaaaaa" so the answer is "NO".

After the fifth operation the string tt becomes "aaaaaaabcaabcaabca", and we can rearrange the strings to: "bbaaa" and "caaaaaabcaabcaabaa" so that ss is lexicographically smaller than tt, so we should answer "YES".

题意:

字符串s和t开始都是只有一个字符a,一共有两种操作:

1 将字符串x加到s中,添加k次

2 将字符串x加到t中,添加k次

问每次操作后再对s和t任意排序,s是否能小于t,是输出YES,否输出NO

思路:

当t中有其他字符时,s一定能小于t

当t中没有其他字符时,

若s中有其他字符,那s不可能小于t

若s中没有其他字符,并且s的长度小于t,则s小于t

一共就这几种情况

代码如下:

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

int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        int n;
        cin >> n;
        long long int others = 0,othert = 0;
        long long int sizes = 1,sizet = 1;
        while(n--)
        {
            int a,b;
            cin >> a >> b;
            string s;
            cin >> s;
            int f = 0;
            for(int i = 0; i < s.size(); i++)
            {
                if(a == 1)
                {
                    if(s[i] != 'a')
                    {
                        others = 1;
                    }
                    else
                    {
                        sizes += b;
                    }
                }
                else
                {
                    if(s[i] != 'a')
                    {
                        othert = 1;
                    }
                    else
                    {
                        sizet += b;
                    }
                }
            }
            if(othert)
            {
                cout << "YES" << '\n';
            }
            else if(!others && sizes < sizet)
            {
                cout << "YES" << '\n';
            }
            else
            {
                cout << "NO" << '\n';
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值