150iq's blog

By 150iq, history, 4 years ago, In English

Hello everybody,

I didn't completely understand the tutorial of Codeforces Round #649 Problem 1364A -XXXXX. Particularly, I am having trouble with this line of the tutorial. "So let the first non-multiple of x be at index l, and the last one be at index r. We must either remove the prefix all the way up to l or the suffix all the way up to r, and we'll clearly remove whichever shorter."

Could somebody please help me? I am new to codeforces and I really need help.

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
4 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Find the first and the last occurrence of numbers that number % x != 0, if there is no such number, print -1, else print maximum length of slicing from first occurrence till the end of array, and from start of array until last occurrence

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

So we start with the whole array, either all the numbers are divisible by x, then we output -1, or the sum of the n numbers are divisible by x, so we output n and the whole array. The third situation is that there are numbers not divisible by x but the total sum of the n numbers is divisible by x. In this scenario we need to cut off some elements from either the front of the array or the end. If we cut off a multiple of x, an element that is divisible by x, the remaining sum will still be divisible by x. So we need to find the earliest element that is not divisible by x from the left and right, since if we eliminate this element then the sum of the remaining sub array will not be divisible by x. Then we can compare the two sides. Which prefix or suffix that we need to cut off is the shortest. We look for the shortest of the two since the question asks us to maximize the length of the sub array that is our answer. Now that we have either eliminated the prefix or the suffix, the remaining sub array's sum will not be divisible by x.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Thinking like this may(or may not) help:

Let sum be sum of all element. If sum is not divisible by x (aka sum%x!=0) then answer is n But, What if we can't take all element? (aka sum%x==0) Let's try with a bruteforce approach

It is more understandable in paper or running code in mind

sum is divisible by x. Now, observe that sum-x, sum-2x, sum-3x... are also divisible by x.

But sum - any_other_value is not divisible by x.

So, in our bruteforce approach, for any left_pointer, the valid right_pointer will be most right positioned k-1 such that a[k]+a[k+1]+a[k+2]+...+a[n-1] is not divisible by x.

for every position i we can keep the valid k in map<int, int> similar data structure. And implement it. For reference you can look at my submission though it will not require I guess..