Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

This problem is about finding the "loneliness" of an array of non-negative integers. The "loneliness" is defined as the smallest positive integer k such that for any two positive integers i and j, the bitwise OR of every k consecutive elements is the same.

The input consists of multiple test cases. The first line of the input contains a single integer t, which is the number of test cases. Each test case starts with an integer n, the length of the array. The next line contains n integers, the elements of the array.

The output should be the "loneliness" of the given array for each test case.

Here's how to solve this problem:

  1. For each test case, start by setting k to 1.
  2. For each pair of indices i and j in the array, calculate the bitwise OR of the k consecutive elements starting at these indices.
  3. If the bitwise ORs are the same for all pairs of indices, then k is the "loneliness" of the array. Print k and move on to the next test case.
  4. If the bitwise OR

This problem has been solved

Similar Questions

You are given a positive integer x๐‘ฅ. Find any array of integers a0,a1,โ€ฆ,anโˆ’1๐‘Ž0,๐‘Ž1,โ€ฆ,๐‘Ž๐‘›โˆ’1 for which the following holds:1โ‰คnโ‰ค321โ‰ค๐‘›โ‰ค32,ai๐‘Ž๐‘– is 11, 00, or โˆ’1โˆ’1 for all 0โ‰คiโ‰คnโˆ’10โ‰ค๐‘–โ‰ค๐‘›โˆ’1,x=โˆ‘i=0nโˆ’1aiโ‹…2i๐‘ฅ=โˆ‘๐‘–=0๐‘›โˆ’1๐‘Ž๐‘–โ‹…2๐‘–,There does not exist an index 0โ‰คiโ‰คnโˆ’20โ‰ค๐‘–โ‰ค๐‘›โˆ’2 such that both aiโ‰ 0๐‘Ž๐‘–โ‰ 0 and ai+1โ‰ 0๐‘Ž๐‘–+1โ‰ 0.It can be proven that under the constraints of the problem, a valid array always exists.InputEach test contains multiple test cases. The first line of input contains a single integer t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104)ย โ€” the number of test cases. The description of the test cases follows.The only line of each test case contains a single positive integer x๐‘ฅ (1โ‰คx<2301โ‰ค๐‘ฅ<230).OutputFor each test case, output two lines.On the first line, output an integer n๐‘› (1โ‰คnโ‰ค321โ‰ค๐‘›โ‰ค32)ย โ€” the length of the array a0,a1,โ€ฆ,anโˆ’1๐‘Ž0,๐‘Ž1,โ€ฆ,๐‘Ž๐‘›โˆ’1.On the second line, output the array a0,a1,โ€ฆ,anโˆ’1๐‘Ž0,๐‘Ž1,โ€ฆ,๐‘Ž๐‘›โˆ’1.If there are multiple valid arrays, you can output any of them.ExampleinputCopy71142415271119outputCopy1150 -1 0 0 160 0 0 -1 0 15-1 0 0 0 16-1 0 -1 0 0 15-1 0 -1 0 15-1 0 1 0 1NoteIn the first test case, one valid array is [1][1], since (1)โ‹…20=1(1)โ‹…20=1.In the second test case, one possible valid array is [0,โˆ’1,0,0,1][0,โˆ’1,0,0,1], since (0)โ‹…20+(โˆ’1)โ‹…21+(0)โ‹…22+(0)โ‹…23+(1)โ‹…24=โˆ’2+16=14(0)โ‹…20+(โˆ’1)โ‹…21+(0)โ‹…22+(0)โ‹…23+(1)โ‹…24=โˆ’2+16=14.

Two kittens, Max and Min, play with a pair of non-negative integers x and y. As you can guess from their names, kitten Max loves to maximize and kitten Min loves to minimize. As part of this game Min wants to make sure that both numbers, x and y became negative at the same time, and kitten Max tries to prevent him from doing so.Each kitten has a set of pairs of integers available to it. Kitten Max has n pairs of non-negative integers (ai,โ€‰bi) (1โ€‰โ‰คโ€‰iโ€‰โ‰คโ€‰n), and kitten Min has m pairs of non-negative integers (cj,โ€‰dj) (1โ€‰โ‰คโ€‰jโ€‰โ‰คโ€‰m). As kitten Max makes a move, it can take any available pair (ai,โ€‰bi) and add ai to x and bi to y, and kitten Min can take any available pair (cj,โ€‰dj) and subtract cj from x and dj from y. Each kitten can use each pair multiple times during distinct moves.Max moves first. Kitten Min is winning if at some moment both numbers a, b are negative simultaneously. Otherwise, the winner of the game is kitten Max. Determine which kitten wins if both of them play optimally.InputThe first line contains two integers, n and m (1โ€‰โ‰คโ€‰n,โ€‰mโ€‰โ‰คโ€‰100โ€‰000) โ€” the number of pairs of numbers available to Max and Min, correspondingly.The second line contains two integers x, y (1โ€‰โ‰คโ€‰x,โ€‰yโ€‰โ‰คโ€‰109) โ€” the initial values of numbers with which the kittens are playing.Next n lines contain the pairs of numbers ai,โ€‰bi (1โ€‰โ‰คโ€‰ai,โ€‰biโ€‰โ‰คโ€‰109) โ€” the pairs available to Max.The last m lines contain pairs of numbers cj,โ€‰dj (1โ€‰โ‰คโ€‰cj,โ€‰djโ€‰โ‰คโ€‰109) โ€” the pairs available to Min.OutputPrint ยซMaxยป (without the quotes), if kitten Max wins, or "Min" (without the quotes), if kitten Min wins.

Make My Array EqualYou are given an array ๐ดA of length ๐‘N.Determine if it is possible to make all the elements of ๐ดA equal by performing the following operation any number of times (possibly, zero):Choose two distinct indices ๐‘–i and ๐‘—j (1โ‰ค๐‘–,๐‘—โ‰ค๐‘1โ‰คi,jโ‰คN, ๐‘–โ‰ ๐‘—i๎€ =j); andUpdate the value at index ๐‘–i by setting ๐ด๐‘–A iโ€‹ to ๐ด๐‘–+๐ด๐‘—A iโ€‹ +A jโ€‹ .For example, if ๐ด=[1,5,3,5,6]A=[1,5,3,5,6],Choosing ๐‘–=5i=5 and ๐‘—=3j=3 would result in the array [1,5,3,5,9][1,5,3,5,9], since we replace ๐ด5A 5โ€‹ with ๐ด5+๐ด3=9A 5โ€‹ +A 3โ€‹ =9.Choosing ๐‘–=2i=2 and ๐‘—=4j=4 would instead result in the array [1,10,3,5,6][1,10,3,5,6].Output "YES" if it is possible to make all elements of the array equal after performing several (possibly, zero) of these operations, otherwise output "NO" (without quotes).Input FormatThe first line of input will contain a single integer ๐‘‡T, denoting the number of test cases.Each test case consists of two lines of input.The first line of each test case contains a single integer ๐‘N โ€” the length of array.The second line of each test case contains ๐‘N space-separated integers ๐ด1,๐ด2,โ€ฆ,๐ด๐‘A 1โ€‹ ,A 2โ€‹ ,โ€ฆ,A Nโ€‹ , denoting the initial array.Output FormatFor each test case, print Yes if it is possible to make all elements of array equal using any number of operations, otherwise print No.You may print each character of the output in either uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).Constraints1โ‰ค๐‘‡โ‰ค1051โ‰คTโ‰ค10 5 1โ‰ค๐‘โ‰ค2โ‹…1051โ‰คNโ‰ค2โ‹…10 5 0โ‰ค๐ด๐‘–โ‰ค1090โ‰คA iโ€‹ โ‰ค10 9 The sum of ๐‘N over all test cases won't exceed 2โ‹…1052โ‹…10 5 .Sample 1:InputOutput331 1 121 231 0 0YESNOYES

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.

Let lowbit(x)lowbitโก(๐‘ฅ) denote the value of the lowest binary bit of x๐‘ฅ, e.g. lowbit(12)=4lowbitโก(12)=4, lowbit(8)=8lowbitโก(8)=8.For an array a๐‘Ž of length n๐‘›, if an array s๐‘  of length n๐‘› satisfies sk=(โˆ‘i=kโˆ’lowbit(k)+1kai)mod998244353๐‘ ๐‘˜=(โˆ‘๐‘–=๐‘˜โˆ’lowbitโก(๐‘˜)+1๐‘˜๐‘Ž๐‘–)mod998244353 for all k๐‘˜, then s๐‘  is called the Fenwick Tree of a๐‘Ž. Let's denote it as s=f(a)๐‘ =๐‘“(๐‘Ž).For a positive integer k๐‘˜ and an array a๐‘Ž, fk(a)๐‘“๐‘˜(๐‘Ž) is defined as follows:fk(a)={f(a)f(fkโˆ’1(a))ifย k=1otherwise.๐‘“๐‘˜(๐‘Ž)={๐‘“(๐‘Ž)ifย ๐‘˜=1๐‘“(๐‘“๐‘˜โˆ’1(๐‘Ž))otherwise.You are given an array b๐‘ of length n๐‘› and a positive integer k๐‘˜. Find an array a๐‘Ž that satisfies 0โ‰คai<9982443530โ‰ค๐‘Ž๐‘–<998244353 and fk(a)=b๐‘“๐‘˜(๐‘Ž)=๐‘. It can be proved that an answer always exists. If there are multiple possible answers, you may print any of them.InputEach test contains multiple test cases. The first line contains the number of test cases t๐‘ก (1โ‰คtโ‰ค1041โ‰ค๐‘กโ‰ค104). The description of the test cases follows.The first line of each test case contains two positive integers n๐‘› (1โ‰คnโ‰ค2โ‹…1051โ‰ค๐‘›โ‰ค2โ‹…105) and k๐‘˜ (1โ‰คkโ‰ค1091โ‰ค๐‘˜โ‰ค109), representing the length of the array and the number of times the function f๐‘“ is performed.The second line of each test case contains an array b๐‘ of length n๐‘› (0โ‰คbi<9982443530โ‰ค๐‘๐‘–<998244353).It is guaranteed that the sum of n๐‘› over all test cases does not exceed 2โ‹…1052โ‹…105.OutputFor each test case, print a single line, containing a valid array a๐‘Ž of length n๐‘›.ExampleinputCopy28 11 2 1 4 1 2 1 86 21 4 3 17 5 16outputCopy1 1 1 1 1 1 1 11 2 3 4 5 6

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.