Knowee
Questions
Features
Study Tools

Polycarp was given an array a๐‘Ž of n๐‘› integers. He really likes triples of numbers, so for each j๐‘— (1โ‰คjโ‰คnโˆ’21โ‰ค๐‘—โ‰ค๐‘›โˆ’2) he wrote down a triple of elements [aj,aj+1,aj+2][๐‘Ž๐‘—,๐‘Ž๐‘—+1,๐‘Ž๐‘—+2].Polycarp considers a pair of triples b๐‘ and c๐‘ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:b1โ‰ c1๐‘1โ‰ ๐‘1 and b2=c2๐‘2=๐‘2 and b3=c3๐‘3=๐‘3;b1=c1๐‘1=๐‘1 and b2โ‰ c2๐‘2โ‰ ๐‘2 and b3=c3๐‘3=๐‘3;b1=c1๐‘1=๐‘1 and b2=c2๐‘2=๐‘2 and b3โ‰ c3๐‘3โ‰ ๐‘3.Find the number of beautiful pairs of triples among the written triples [aj,aj+1,aj+2][๐‘Ž๐‘—,๐‘Ž๐‘—+1,๐‘Ž๐‘—+2].InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases.The first line of each test case contains a single integer n๐‘› (3โ‰คnโ‰ค2โ‹…1053โ‰ค๐‘›โ‰ค2โ‹…105)ย โ€” the length of the array a๐‘Ž.The second line of each test case contains n๐‘› integers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1061โ‰ค๐‘Ž๐‘–โ‰ค106)ย โ€” the elements of the array.It is guaranteed that the sum of the values of n๐‘› for all test cases in the test does not exceed 2โ‹…1052โ‹…105.OutputFor each test case, output a single integerย โ€” the number of beautiful pairs of triples among the pairs of the form [aj,aj+1,aj+2][๐‘Ž๐‘—,๐‘Ž๐‘—+1,๐‘Ž๐‘—+2].Note that the answer may not fit into 32-bit data types.

Question

Polycarp was given an array a๐‘Ž of n๐‘› integers. He really likes triples of numbers, so for each j๐‘— (1โ‰คjโ‰คnโˆ’21โ‰ค๐‘—โ‰ค๐‘›โˆ’2) he wrote down a triple of elements [aj,aj+1,aj+2][๐‘Ž๐‘—,๐‘Ž๐‘—+1,๐‘Ž๐‘—+2].Polycarp considers a pair of triples b๐‘ and c๐‘ beautiful if they differ in exactly one position, that is, one of the following conditions is satisfied:b1โ‰ c1๐‘1โ‰ ๐‘1 and b2=c2๐‘2=๐‘2 and b3=c3๐‘3=๐‘3;b1=c1๐‘1=๐‘1 and b2โ‰ c2๐‘2โ‰ ๐‘2 and b3=c3๐‘3=๐‘3;b1=c1๐‘1=๐‘1 and b2=c2๐‘2=๐‘2 and b3โ‰ c3๐‘3โ‰ ๐‘3.Find the number of beautiful pairs of triples among the written triples [aj,aj+1,aj+2][๐‘Ž๐‘—,๐‘Ž๐‘—+1,๐‘Ž๐‘—+2].InputThe first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases.The first line of each test case contains a single integer n๐‘› (3โ‰คnโ‰ค2โ‹…1053โ‰ค๐‘›โ‰ค2โ‹…105)ย โ€” the length of the array a๐‘Ž.The second line of each test case contains n๐‘› integers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1061โ‰ค๐‘Ž๐‘–โ‰ค106)ย โ€” the elements of the array.It is guaranteed that the sum of the values of n๐‘› for all test cases in the test does not exceed 2โ‹…1052โ‹…105.OutputFor each test case, output a single integerย โ€” the number of beautiful pairs of triples among the pairs of the form [aj,aj+1,aj+2][๐‘Ž๐‘—,๐‘Ž๐‘—+1,๐‘Ž๐‘—+2].Note that the answer may not fit into 32-bit data types.

...expand
๐Ÿง Not the exact question you are looking for?Go ask a question

Solution 1

This problem is about finding the number of beautiful pairs of triples in an array. A pair of triples is considered beautiful if they differ in exactly one position.

Here are the steps to solve this problem:

  1. First, we need to read the number of test cases, t. For each test case, we will perform the following steps.

  2. Read the length of the array, n, and the elements of the array.

  3. Initialize a counter to keep track of the number of beautiful pairs of triples.

  4. Iterate over the array. For each element at position j (1โ‰คjโ‰คnโˆ’2), create a triple [aj,aj+1,aj+2].

  5. Compare this triple with all other triples. If they differ in exactly one position, increment the counter.

  6. After iterating over the entire array, the counter will hold the number of beautiful pairs of triples.

  7. Print the counter.

This algorithm has a time complexity of O(n^2) because for each element, we are comparing it with all other elements. If the array is very large, this algorithm may be slow. To optimize it, we can use a data structure like a hash map to store the triples and their counts, reducing the time complexity to O(n).

This problem has been solved

Solution 2

This problem is about finding the number of beautiful pairs of triples in an array. A pair of triples is considered beautiful if they differ in exactly one position.

Here are the steps to solve this problem:

  1. First, we need to read the number of test cases, t. For each test case, we will perform the following steps.

  2. Read the length of the array, n, and the elements of the array.

  3. Initialize a counter to keep track of the number of beautiful pairs of triples.

  4. Iterate over the array. For each element at position j (1โ‰คjโ‰คnโˆ’2), create a triple [aj,aj+1,aj+2].

  5. Compare this triple with all other triples. If they differ in exactly one position, increment the counter.

  6. After iterating over the entire array, the counter will hold the number of beautiful pairs of triples.

  7. Print the counter.

This algorithm has a time complexity of O(n^2) because for each element, we are comparing it with all other elements. If the array is very large, this algorithm may be slow. To optimize it, we can use a data structure like a hash map to store the frequency of each triple, and then use

This problem has been solved

Similar Questions

>>polyfit (X, Y, 3) creates and evaluates a polynomial of order 3 where X and Y are arrays of the same length

Initially, we had one array, which was a permutation of size n๐‘› (an array of size n๐‘› where each integer from 11 to n๐‘› appears exactly once).We performed q๐‘ž operations. During the i๐‘–-th operation, we did the following:choose any array we have with at least 22 elements;split it into two non-empty arrays (prefix and suffix);write two integers li๐‘™๐‘– and ri๐‘Ÿ๐‘–, where li๐‘™๐‘– is the maximum element in the left part which we get after the split, and ri๐‘Ÿ๐‘– is the maximum element in the right part;remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool.For example, suppose the initial array was [6,3,4,1,2,5][6,3,4,1,2,5], and we performed the following operations:choose the array [6,3,4,1,2,5][6,3,4,1,2,5] and split it into [6,3][6,3] and [4,1,2,5][4,1,2,5]. Then we write l1=6๐‘™1=6 and r1=5๐‘Ÿ1=5, and the arrays we have are [6,3][6,3] and [4,1,2,5][4,1,2,5];choose the array [4,1,2,5][4,1,2,5] and split it into [4,1,2][4,1,2] and [5][5]. Then we write l2=4๐‘™2=4 and r2=5๐‘Ÿ2=5, and the arrays we have are [6,3][6,3], [4,1,2][4,1,2] and [5][5];choose the array [4,1,2][4,1,2] and split it into [4][4] and [1,2][1,2]. Then we write l3=4๐‘™3=4 and r3=2๐‘Ÿ3=2, and the arrays we have are [6,3][6,3], [4][4], [1,2][1,2] and [5][5].You are given two integers n๐‘› and q๐‘ž, and two sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž]. A permutation of size n๐‘› is called valid if we can perform q๐‘ž operations and produce the given sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž].Calculate the number of valid permutations.InputThe first line contains two integers n๐‘› and q๐‘ž (1โ‰คq<nโ‰ค3โ‹…1051โ‰ค๐‘ž<๐‘›โ‰ค3โ‹…105).The second line contains q๐‘ž integers l1,l2,โ€ฆ,lq๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž (1โ‰คliโ‰คn1โ‰ค๐‘™๐‘–โ‰ค๐‘›).The third line contains q๐‘ž integers r1,r2,โ€ฆ,rq๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž (1โ‰คriโ‰คn1โ‰ค๐‘Ÿ๐‘–โ‰ค๐‘›).Additional constraint on the input: there exists at least one permutation which can produce the given sequences [l1,l2,โ€ฆ,lq][๐‘™1,๐‘™2,โ€ฆ,๐‘™๐‘ž] and [r1,r2,โ€ฆ,rq][๐‘Ÿ1,๐‘Ÿ2,โ€ฆ,๐‘Ÿ๐‘ž].OutputPrint one integer โ€” the number of valid permutations, taken modulo 998244353998244353.ExamplesinputCopy6 36 4 45 5 2outputCopy30inputCopy10 1109outputCopy1814400inputCopy4 124outputCopy8

Make PermutationChef has an array ๐ดA of size ๐‘N. He wants to make a permutationโ€ โ€  using this array.Find whether there exists an array ๐ตB consisting of ๐‘N non-negative integers, such that the array ๐ถC constructed as ๐ถ๐‘–=๐ด๐‘–+๐ต๐‘–C iโ€‹ =A iโ€‹ +B iโ€‹ is a permutation.โ€ โ€  A permutation of size ๐‘N is an array of ๐‘N distinct elements in the range [1,๐‘][1,N]. For example, [4,2,1,3][4,2,1,3] is a permutation of size 44, while [3,2,2,1][3,2,2,1] and [1,3,4][1,3,4] are not.Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of multiple lines of input.The first line of each test case consists of ๐‘N - the size of the array ๐ดAThe next line contains ๐‘N space-separated integers - ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ - the elements of array ๐ดA.Output FormatFor each test case, output YES if there exists an array ๐ตB such that array ๐ถC constructed as ๐ถ๐‘–=๐ด๐‘–+๐ต๐‘–C iโ€‹ =A iโ€‹ +B iโ€‹ is a permutation, otherwise output NO.You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).Constraints1โ‰ค๐‘‡โ‰ค1001โ‰คTโ‰ค1001โ‰ค๐‘โ‰ค1001โ‰คNโ‰ค1001โ‰ค๐ด๐‘–โ‰ค๐‘1โ‰คA iโ€‹ โ‰คNSample 1:InputOutput454 1 3 2 152 4 3 4 21161 1 1 1 6 6YESNOYESNOExplanation:Test case 11 : Consider ๐ต=[0,4,0,0,0]B=[0,4,0,0,0]. The corresponding array ๐ถC becomes [4,5,3,2,1][4,5,3,2,1], which is a permutation. Some other possible values of ๐ตB for which ๐ถC is a permutation are [1,0,1,1,1][1,0,1,1,1] and [1,3,0,0,0][1,3,0,0,0].Test case 22 : It can be proven that no valid array ๐ตB exists.

For k๐‘˜ positive integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜, the value gcd(x1,x2,โ€ฆ,xk)gcd(๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜) is the greatest common divisor of the integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜ย โ€” the largest integer z๐‘ง such that all the integers x1,x2,โ€ฆ,xk๐‘ฅ1,๐‘ฅ2,โ€ฆ,๐‘ฅ๐‘˜ are divisible by z๐‘ง.You are given three arrays a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›, b1,b2,โ€ฆ,bn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› and c1,c2,โ€ฆ,cn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› of length n๐‘›, containing positive integers.You also have a machine that allows you to swap ai๐‘Ž๐‘– and bi๐‘๐‘– for any i๐‘– (1โ‰คiโ‰คn1โ‰ค๐‘–โ‰ค๐‘›). Each swap costs you ci๐‘๐‘– coins.Find the maximum possible value ofgcd(a1,a2,โ€ฆ,an)+gcd(b1,b2,โ€ฆ,bn)gcd(๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘›)+gcd(๐‘1,๐‘2,โ€ฆ,๐‘๐‘›)that you can get by paying in total at most d๐‘‘ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the q๐‘ž possible values d1,d2,โ€ฆ,dq๐‘‘1,๐‘‘2,โ€ฆ,๐‘‘๐‘ž.InputThere are two integers on the first lineย โ€” the numbers n๐‘› and q๐‘ž (1โ‰คnโ‰ค5โ‹…1051โ‰ค๐‘›โ‰ค5โ‹…105, 1โ‰คqโ‰ค5โ‹…1051โ‰ค๐‘žโ‰ค5โ‹…105).On the second line, there are n๐‘› integersย โ€” the numbers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (1โ‰คaiโ‰ค1081โ‰ค๐‘Ž๐‘–โ‰ค108).On the third line, there are n๐‘› integersย โ€” the numbers b1,b2,โ€ฆ,bn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰คbiโ‰ค1081โ‰ค๐‘๐‘–โ‰ค108).On the fourth line, there are n๐‘› integersย โ€” the numbers c1,c2,โ€ฆ,cn๐‘1,๐‘2,โ€ฆ,๐‘๐‘› (1โ‰คciโ‰ค1091โ‰ค๐‘๐‘–โ‰ค109).On the fifth line, there are q๐‘ž integersย โ€” the numbers d1,d2,โ€ฆ,dq๐‘‘1,๐‘‘2,โ€ฆ,๐‘‘๐‘ž (0โ‰คdiโ‰ค10150โ‰ค๐‘‘๐‘–โ‰ค1015).OutputPrint q๐‘ž integersย โ€” the maximum value you can get for each of the q๐‘ž possible values d๐‘‘.ExamplesinputCopy3 41 2 34 5 61 1 10 1 2 3outputCopy2 3 3 3 inputCopy5 53 4 6 8 48 3 4 9 310 20 30 40 505 55 13 1000 113outputCopy2 7 3 7 7 inputCopy1 13450outputCopy7

Today, Cat and Fox found an array a๐‘Ž consisting of n๐‘› non-negative integers.Define the loneliness of a๐‘Ž as the smallest positive integer k๐‘˜ (1โ‰คkโ‰คn1โ‰ค๐‘˜โ‰ค๐‘›) such that for any two positive integers i๐‘– and j๐‘— (1โ‰คi,jโ‰คnโˆ’k+11โ‰ค๐‘–,๐‘—โ‰ค๐‘›โˆ’๐‘˜+1), the following holds:ai|ai+1|โ€ฆ|ai+kโˆ’1=aj|aj+1|โ€ฆ|aj+kโˆ’1,๐‘Ž๐‘–|๐‘Ž๐‘–+1|โ€ฆ|๐‘Ž๐‘–+๐‘˜โˆ’1=๐‘Ž๐‘—|๐‘Ž๐‘—+1|โ€ฆ|๐‘Ž๐‘—+๐‘˜โˆ’1,where x|y๐‘ฅ|๐‘ฆ denotes the bitwise OR of x๐‘ฅ and y๐‘ฆ. In other words, for every k๐‘˜ consecutive elements, their bitwise OR should be the same. Note that the loneliness of a๐‘Ž is well-defined, because for k=n๐‘˜=๐‘› the condition is satisfied.Cat and Fox want to know how lonely the array a๐‘Ž is. Help them calculate the loneliness of the found array.InputEach test consists of multiple test cases. The first line contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases. The description of the test cases follows.The first line of each test case contains one integer n๐‘› (1โ‰คnโ‰ค1051โ‰ค๐‘›โ‰ค105)ย โ€” the length of the array a๐‘Ž.The second line of each test case contains n๐‘› integers a1,a2,โ€ฆ,an๐‘Ž1,๐‘Ž2,โ€ฆ,๐‘Ž๐‘› (0โ‰คai<2200โ‰ค๐‘Ž๐‘–<220)ย โ€” the elements of the array.It is guaranteed that the sum of n๐‘› over all test cases doesn't exceed 105105.OutputFor each test case, print one integer ย โ€” the loneliness of the given array.ExampleinputCopy71032 2 231 0 253 0 1 4 252 0 4 0 270 0 0 0 1 2 480 1 3 2 2 1 0 3outputCopy1134473

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.