← Back to topics
Topic

DivInts

kinezizbosne
Probably there are some tricky test cases. I can't pass 4 test cases.

http://www.z-trening.com/submit.php?submit=7100334579&subm_code=1
demjan0001
Read text description more carefully "If there are multiple longest sequences, you have to find the one that is lexicographically smallest."

Try this test example:
4
2 3 9 16

Your output: 3 9
Correct output: 2 16
kinezizbosne
Thanks!
But how to solve it? Only possible solution is to find all sequences with maximum length and then find which is lexicographically smallest.
demjan0001
That is not only possible solution :)

You are doing dynamic programming where brd[i] means what is the longest sequence such that last number is i. Right?

Well try to think about to reverse things :)
So brd[i] means what is the longest sequence such that first number is i.

I hope this was helpful :)
If you need additional help, just ask.
kinezizbosne
Yes, I am doing DP but brd[i] means how much divisors have a[i] and ind[i] means index of a such that

a(i) mod a(ind(i)) = 0 and a(ind(i)) is smallest and brd(ind(i)) is max length.
So, if I want to reverse things I must do everything from the beginning. :(

What is correct output for:
7
1 2 3 9 16 27 48?
demjan0001
This is not correct "a(i) mod a(ind(i)) = 0 and a(ind(i)) is smallest and brd(ind(i)) is max length", because take for example test:
5
2 3 9 16 144
So in this case output should be 2 16 144, and your output is 3 9 144
When you compare 2 sequences you need to compare them first by 1st element, than by 2nd, and so on, but you are comparing 2 sequences just by last element.

Well it's easier to solve it when you reverse things because you don't have problem when you compare 2 sequences lexicographically, but you can solve it this way also.

Let's make a matrix better[i][j] which is true if sequence which ends with a[i] is better than sequence which ends with a[j], false otherwise.

So better[i][j] is defined:

if brd[i] > brd[j] then better[i][j] = true
if brd[i] < brd[j] then better[i][j] = false
if brd[i] = brd[j] then
if ( ind[i] != ind[j] ) then better[i][j] = better[ ind[i] ][ ind[j] ]
if ( ind[i] == ind[j] ) then better[i][j] = a[i] > a[j]


So now you can compare 2 sequences with this matrix better[][].
I hope I helped, if you have more question just ask...
demjan0001
and output for you test is:
4
1 2 16 48
kinezizbosne
I solved it :D

I didn't use matrix as you told me.
Here is code: http://z-trening.com/submit.php?submit=7100335610&subm_code=1

Pay attention on line:
if(brd[i]==mmax) ...

Thanks for help :)
kinezizbosne
And you are right. ;) It is easier to reverse things.
demjan0001
Your solution is still wrong, even it passed all test cases... Test cases are bad :)

Try again this test case:
5
2 3 9 16 144

Your output: 3 9 144
Correct output: 2 16 144
kinezizbosne
Well, I 'll try to reverse things....:)