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;
}

Codeforces Round #827 (Div. 4) D - F相关推荐

  1. Codeforces Round #827 (Div. 4) A~G

     比赛链接:Dashboard - Codeforces Round #827 (Div. 4) - Codeforces 目录 A Sum B Increasing C Stripes D. Cop ...

  2. Codeforces Round #827 (Div. 4) 题解记录

    Codeforces Round #827 (Div. 4) A 水题 #include<bits/stdc++.h> #define sc(x) scanf("%lld&quo ...

  3. 10.16打卡 Codeforces Round #827 (Div. 4) A~E

    Dashboard - Codeforces Round #827 (Div. 4) - Codeforces 开摆F和G懒得写 A题 排个序就好了 /* ⣿⣿⣿⣿⣿⣿⡷⣯⢿⣿⣷⣻⢯⣿⡽⣻⢿⣿⣿⣿⣿⣿ ...

  4. Codeforces Round #827 (Div. 4) E. Scuza

    Codeforces Round #827 (Div. 4) E. Scuza 前缀和 前缀和 前缀和 + 二分 二分 二分 Let's compute the prefix sums of the ...

  5. Codeforces Round #797 (Div. 3)无F

    Codeforces Round #797 (Div. 3)无F 这打的也太屎了,白天把G补了才知道简单的很,但f还是没头绪呜呜呜 Problem - A - Codeforces Given the ...

  6. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  7. Codeforces Round #827 (Div. 4)(A~G)(F已更新)

    "爷爷,你关注的up主要更新拉!" 今天起开始CF/At不定期更新 A. Sum(暴力) 只需判断a,b,c是否为和关系即可 #include <bits/stdc++.h& ...

  8. Codeforces Round #506 (Div. 3) 1029 F. Multicolored Markers

    CF-1029F 题意: a,b个小正方形构造一个矩形,大小为(a+b),并且要求其中要么a个小正方形是矩形,要么b个小正方形是矩形. 思路: 之前在想要分a,b是否为奇数讨论,后来发现根本不需要.只 ...

  9. Codeforces Round #827 (Div. 4)(A-G)

    目录 A.Sum 1.题意 2.解题思路 3.Ac_code B.Increasing 1.题意 2.解题思路 3.Ac_code C.Stripes 1.题意 2.解题思路 3.Ac_code D. ...

最新文章

  1. 码农技术炒股之路——配置管理器、日志管理器
  2. 用Hook的方式实现防抖
  3. 算法与数据结构(一)
  4. 数据库开发——MySQL——函数与流程控制
  5. 技术创作101训练营:从0到1教你搭建自己的技术品牌
  6. Vue 适配移动端 使用 postcss-pxtorem lib-flexible 插件 转px为vw rem
  7. Coding and Paper Letter(三十九)
  8. 一个SAP加拿大实习生在当地观察到的美景
  9. Android使用C/C++来保存密钥
  10. 豆瓣9.6分!这部BBC的纪录片太让人震撼!
  11. java基础代码下载_Java基础(一)(示例代码)
  12. 使用instantclient_11_2和PL/SQL Developer工具包连接oracle 11g远程数据库
  13. R|ggplot2(七)|自定义主题
  14. rust代练吧_代练也配名正言顺?电竞协会被狂喷,只因颁发游戏代练师证书!...
  15. 【操作系统原理-陈渝老师】第七章 进程与线程
  16. 无法加载计算机管理,电脑中无法打开Internet选项中的管理加载项如何解决
  17. 富龙热电:望眼欲穿矿难拿
  18. php13 质粒 cm erm 抗生素,PHP13 会话控制 - osc_c0g7cjrk的个人空间 - OSCHINA - 中文开源技术交流社区...
  19. 动态规划——斐波那契数列(70. 爬楼梯、198. 打家劫舍、213. 打家劫舍II、信件错排、母牛生产)
  20. IPUS SQPI PSRAM为STM32单片机提供RAM扩展方案

热门文章

  1. 什么是fail safe IO
  2. 《用户至上:用户研究方法与实践(原书第2版)》一2.1 概述
  3. 微信公众号开发-----实现模板、图文、文本、音乐、图片推送
  4. 反问疑问句的一些用法
  5. mysql条件增量同步命令_DataX3 Mysql增量同步ES
  6. 电子邮件注册网站是什么,163电子邮件注册流程详解
  7. 一种改进的教与学优化算法
  8. 【最小栈c++】设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈
  9. Uva 1616 Caravan Robbers (商队抢劫者)
  10. java中实现工厂日历_Java实现的日历功能完整示例