Comments

As one of the comments already mentions, using the inverse modulo of the factorial in the denominator would work. I wrote code for this problem in which I had to find the sum of combinations, although not the same ones.

Code for calculating nCr in O(1) with O(n) precomputations
0

This is the accepted code, for your reference :)

0

$$$a_i \gt 0$$$, so every number has at least 1 bit set. So over a segment of length 61 or more, we are guaranteed to have a sum $$$S \geq 61$$$. Since the maximum value for $$$b_i$$$ is 61, we can be sure that this segment has $$$S \geq b_i$$$.

Do comment back if you didn't understand something :)

Can you post the code in a spoiler?

If k <= 50, it is $$$5k^2/3$$$
Else it is $$$3k^2/5$$$

In each case just calculate the numerator and see if it is divisible by the denominator.

  • if it is then print the quotient

  • otherwise print (num/denom) (num%denom) (denom)

For those who didn't understand why removing one of the ends gives the optimum length

  • If the whole sum is not divisible by x, then n is the answer.
  • If every number is divisible by x, then 0 is the answer.
  • Otherwise, let L be the index of the leftmost element not divisible by x and its counterpart R.

    • Let [l, r] be the optimal subarray.
    • If l <= L, then r < R because r >= R implies the whole array sum is not divisible by x and the answer is n. In this case, we can set l to 0 because all the elements from 0 to l are divisible by x and the sum is still not a multiple of x.
    • Similar case for r >= R.
    • If [l, r] is enveloped in [L, R], we can just use max([1, R], [L, n]) which is clearly a bigger interval than [l, r].

I love how you separated the editorial into key idea and solution so people have a second shot at upsolving without looking at the solution

Maybe we can write an NN to find out whether an image is from an anime or not and crawl through every user's page on codeforces and find out the percentage?

On brdy → Is codeforce Machiavelli, 6 years ago
+65

I love how this has bitset as one of the tags.

It's the famous increasing subarray problem which can be solved by dp. I tried to understand what you coded and I think there are fundamental mistakes with logic looking at it.

  • you directly compare last two elements without checking if len > 1

  • in the second if, you should return 1 because individual element is an increasing subarray

  • in the last return you are adding 1,assuming that the subarray takes last two elements into account, but that way you are getting pieces of increasing subarrays and not a single one.

Solution saving maximum value

Note : you can solve it using dp to maintain the length of the longest increasing subarray ending at index i or just maintain the maximum value saving memory. My submission uses the second approach.

Read the comments and the corresponding changes I made to your code. Do reply back if you don't understand the reasoning behind the changes, although I suggest you work them out yourself to understand where you went wrong :)

I submitted the commented code and it got accepted. Also, you can look at my code for reference.

Your code modified
My code

Can you update your discord link fam? I think the link expired and I couldn't join today :/

On vinu_93 → 1325C, 6 years ago
0

Solution

  • You have a tree with n vertices, so n-1 edges.
  • Mark all the edges with numbers from 0 to n-2.
  • Take any pair of vertices and write down the numbers on the edges in the shortest path between them.
  • Find the MEX of this array.

If you understand how MEX works, the problem is simple. The MEX of any path that doesn't contain the edge labeled 0 is 0. Find a vertex which has more than 3 edges, and mark them 0, 1, 2. Mark the remaining edges randomly. This ensures that the maximum MEX is 2.

On LIFE_GOES_ON → Help, 6 years ago
+11
Main code

Link to AC submission

Take a cube and label it as follows.
Up, down : 0, 1
Front, Back : 2, 3
Left, Right : 4, 5

Now, we have 3 different kinds of rotations, each rotation has one of the above pairs fixed and the remaining ones complete some circles.

Say we have {1, 2, 3, 4} as an array. Then one rotation gives {2, 3, 4, 1}. Similarly, the positions for rotation as per the above notation are {0, 2, 1, 3}, {0, 4, 1, 5}, {2, 4, 3, 5}.

It is obvious that only 3 rotations are possible and 4th rotation gives us the original array.

So here's what I did :

  • Sort the initial string

  • Go through its permutations

  • Rotate each of its permutations along each of the 3 axes up to a maximum of 3 times(64 in total)

  • See if at least one of them already exists in the set

  • If it doesn't then increase the answer by 1

  • Push all the rotations of the current permutation into a set

On imtinanjibon → getting runtime error., 6 years ago
0

First of all, the answer is not $$$^{N}C_M$$$ as you think. You might want to think again or check the editorial.
Secondly, factorial grows faster than exponential, so it won't fit the integer limit or even long long after a small n. The submission shows the exception and the line number so you can try replacing the int by long long but I'm pretty sure it still won't help.

On crazyilian → Legends in statements?, 6 years ago
0

I have to appreciate your effort man. You did a great job.

Some people are pissed because the Corona issue is something they take very seriously. Writing a statement about taking a vacation(problem D) is fine, according to me at least. But some may feel like you're encouraging or even subtly suggesting that it's okay to go outside, which we all know is not desirable during the current times because of this virus. Or let's take problem E, someone who's lost their job because of Corona may feel bad.

The point of a problem is to solve it, not to nitpick on the statement. If a problem statement says you're superman, it doesn't mean you jump from a building because the statement said you could fly. So I think people should just chill out and enjoy the problems and not make a big deal out of it.

The test case says access denied. So can you please post the test case instead of the link.
I solved it using upper bound vector. Just removing duplicates and finding the score's position in the array will suffice.

code

Here's an explanation for D, if you haven't understood it from the editorial.
Let the prefix sum array be $$$M = { M_1, M_2, .., M_n}$$$.
All the elements in this array are unique and $$$\leq$$$ S as we are adding a positive number at each step.

Let $$$M' = (M_1+k)\%S, (M_2+k)\%S, .., (M_n+k)\%S$$$.
Elements in this array are unique as well and all are < S due to modulo.

If k=0, it is trivial. Suppose k>0.
If $$$M'_i = M_j$$$, then either $$$M_j = M_i+k$$$ or $$$M_i+k = M_j+S$$$.
If $$$M_j = M_i+k$$$, then j > i, segment from i to j has a sum = k.
If $$$M_i+k = M_j+S$$$, then j < i, segment from j to i has sum = S-k.

So there is no solution if $$$S \lt 2n$$$ as there should $$$2n$$$ unique elements($$$\neq k$$$ and $$$\neq S-k$$$) $$$\leq$$$ S and this is not possible. We don't know whether $$$S \geq 2n$$$ has a solution or not, but the construction in the editorial shows that it is always possible.

Imagine the numbers 0 to n as the leaves of a binary tree of height ceil(log(n)). Suppose leaves are the level 0. After every match, a player moves up a level. So I think the level of LCA(as defined here) is the answer.

Could be wrong, I would solve it like this.

This is the one I can think of. Solve $$$i^{2}+i-n=0$$$
So just take floor(sol) from the previous equation.
The problem is that solution involves computing square root which is not O(1).

On Hamim99 → Need help, 6 years ago
+2

Segtree + lazy works

  • Run a sieve to find primes less than $$$10^{6}$$$
  • Read input and only store if each number is a prime or not
  • Build a sum segtree
  • For query of type 0 just do a lazy update on the segment x y ie. change all the values to isPrime(v)
  • For query of type 1, return the sum.

Sorry I don't know how to link SPOJ codes so I posted the code here

Code

Only the number which you put first is taken as it is and the others have ~ added to them. So maintain prefix and suffix of ~ &. Answer would be maximum of pref[i] & $$$a_i$$$ & suff[i] for all i. pref[i] is the value of all the elements from 1 to i-1(using the ~ & operation) and suff[i] is the value of all the elements from i+1 to n(using the same operation).

The editorial explains it better so you might wanna read that :)

Check this submission for reference.

+10

A | B has all the bits where A or B have 1, set to 1.

A | B — B has only those bits set to 1 where A has a 1 but B doesn't have a 1. If B had a 1, then A | B would have a 1 in that position and — B would set that bit to 0.

So it is equivalent to A & ~ B

The key idea is that if the adjacent characters are equal, then we can untangle them. So if a sequence of length n exists, we can change it to a sequence of length n-2 by deleting the adjacent ones.

For example, (-++-) -> (--) -> ()

I used a stack to do this.

  • If the stack is empty, push the current character onto the stack.

  • Else if the top symbol is same as the current one, pop it off the stack.

  • Else push it onto the stack.

  • Finally check if the stack is empty or not.

code

You're merging two sets even when they represent the same set. I added two lines to your code and it gets accepted.

Binary search succeeds for day = 10, so we search over the range 1 to 9 and finally arrive at 9 to be the minimum day I think.

I'm not here to say "It's all going to be alright" or "Take some time off and do something you like" or generic things like that because I know they don't do any good to you in your current state.

I've been diagnosed with clinical depression and was suicidal at some point. It took me 2 or 3 years to recover and even now I have occasional episodes of depression in my life. So when I say I understand how you feel, you know I do.

Hell, I even tried to die, twice, and failed at it. So in a way, I was a failure at living as well as dying. The state of not caring that you mentioned about, I've been there too. I've hit rock bottom multiple times and all I did was to cry while the anti depressants were numbing my pain.

Here's the thing. We are humans. Most of us care about too many things like our social status, grades, what people think about us,our ratings etc. And then there's people at the other end of the spectrum who realized that these are meaningless and don't matter. So they stop caring about these things. The important thing here is the "these things" mentioned in the previous line. We have to care about something. Without a something to care about, we get engulfed by emptiness.

Usually, depression has a social stigma attached to it. It might be hard to ask someone for help. There might be feelings of paranoia which prevent you from opening up. So be it. At least, go visit a therapist. That will help you.

Depression is a very common illness. It is termed as the common cold of mental illnesses. The only difference is that common cold doesn't kill while depression does. There is nothing to be ashamed of and every person experiences some form of depression, mild or extreme, at some point in their lives.

Don't give up hope. It may or may not get better. But I know for a fact that you will become stronger. Your problems may become even worse and make you feel like giving up. But you will look them in the eye, say not today and solve them. So, stay strong.

While I was struggling with depression, I read a lot of books to understand why I felt the way I felt and what to do to get out of it. And it was very very helpful. I'll dm the list to you if you want to give it a shot. And the books are not about rainbows and unicorns. They are about how sometimes life sucks and the healthiest thing we can do is to accept that sometimes life does suck and learning from our experiences.

So, stay strong!!

You have two types of diagonals, ones with i+j constant(top right to bottom left) and ones with i-j constant(top left to bottom right). When you take any square sub matrix and apply transpose, you find that elements on the 2nd type of diagonal change positions. But they remain on the same diagonal. In other words, [0][0] can't change it's place. [1][0] and [0][1] can swap places among themselves but not with any other element. We can also obtain any permutation of an (i+j) diagonal by starting with the top left corner and progressively fixing elements in the next diagonal by picking matrix of size 2x2 and swapping elements which aren't on the main diagonal of it. So we only need to check if the corresponding multisets of diagonal (i+j) are same.

On deepak_097 → Spoj PPATH problem., 8 years ago
+1

There are only 1061 4-digit primes. Generate a graph with each vertex representing one of these primes. Add edges between two vertices if we can move from one vertex(prime) to another with a cost of 1.

Notice that there can be atmost 39 edges per vertex so the number edges is O(n). Then, since the number of test cases are small and the cost for each edge is same, a simple bfs from the initial prime as the root should do the trick for each test case.

Why is a random_shuffle performed at the end of the code for problem E?

Thank you very much.

I will declare variables common to function calls globally from the next time onwards.

Once again, thank you for your time :)

On Mohammadnhw → cheating, 8 years ago
+1

His previous contests where he had a spike in ratings seem legit.

Atleast he didn't get the increase by hacks. But the question is if he made submissions from his alt to confirm whether his solutions were correct or not, and then submitted them

On Mohammadnhw → cheating, 8 years ago
0

It won't happen every time I guess....

Next time he does a contest, if he doesn't get one of his alts in the same room, he'll surely fall down.

On atlasworld → Problem 16E codeforces, 8 years ago
0

We iterate a variable called mask from 0 to (1 << n)-1 Suppose our current mask is i.

Suppose it has x bits set.This means that those fishes with the corresponding bit set are already dead. The number of fish left alive would be n-x. Now, we iterate through the bits in the mask and check which bits are not set(which fish are still alive). Now we simulate a fight between two fishes alive,that is,we select two fishes randomly with equal probability. That probability(p) would be 1/(# of ways to select two fishes from n-x fishes).

Let those selected fishes be x and y.

If x wins, we update dp[mask|(1 << y)]+=dp[mask]*p*a[x][y].

Similarly we update dp[mask|(1 << x)]+=dp[mask]*p*a[y][x].

The required answer would be dp[~(1 << i)] for the ith fish.

This solution takes O(n^2*2^n) time and I think it might pass under the given constraints.

On atlasworld → Problem 16E codeforces, 8 years ago
0

It can be solved with dp + bitmasks I think...

This might help you :)

Than you very much.

I was hoping for a response like this.

Your points were very valuable and I will try my best to learn a new topic everyday(or week at least) and solve problems based on it. Also, thank you for suggesting the book CP3 book and I will surely start reading it asap.

Good luck on your way to becoming an LGM :)

We are not supposed to round off the fraction mate :)

That looks good, thanks mate!

On straw__hat → Strange error!, 8 years ago
0

Can you post a link to your submissions please?

Also, which question are you solving?

The grid has n rows and m columns.

We first calculate what is the cost for each column to turn it into black and white as well. Suppose we store the number of white cells in columns in an array whiteCount[n].

consider a 2-d array dp[n][2];

dp[i][0]=minimum cost to convert the current column completely into black and have the barcode valid.

dp[i][1]=minimum cost to convert the current column completely into white and have the barcode valid.

Now,

Let's iterate through 1 to m columns.

At every column, we have two choices.We paint it completely black or completely white. But, we also have to make sure that we have atleast x-1 and atmost y-1 columns behind the current column which have the same color.

Suppose we are at column i. We now iterate j through columns i-y+1 to i-x+1 and find the cost to convert those columns to black.And the j-1 column should certainly have white.

So, the dp relation would be,

dp[i][0] would be minimum of (sum of whiteCounts from j to i + dp[j-1][1])

dp[i][1] would be minimum of (sum of blackCounts(can be obtained easily if you know whiteCounts) from j to i + dp[j-1][0])

Also, you can save time by checking out this blog entry and reading partial sum :)

On vats0726 → Run-Time Error, 8 years ago
0

Hi,

I think there is some problem with erasing the iterator while iterating through the map..

The output of this code shows that the value of m.end() is not updated in the first for loop.

for(int i=0;i<n;i++)
{   int s,b;
    cin>>s>>b;
    m[s]=b;

}

int cnt=0; 
map<int,int>::iterator it,it2;
for(it=m.begin();it!=m.end();it++)
{   
    cout<<"it 1 "<<it->first<<" "<<it->second<<endl;

    for(it2=m.begin();it2!=m.end();it2++)
    {
        cout<<"it 2 "<<it2->first<<" "<<it2->second<<endl;
        if(it2->first<s)
        {   cnt++;
            s+=it2->second;
            m.erase(it2);
            break;
        }
    }
}

Input : 999 2

1010 10

67 89

Output: it 1 67 89

it 2 67 89

it 1 1330860869 1162633042

it 2 1010 10

also, I think m[s]+=b was what you were looking for :)

That meme never gets old

This is exactly what I think when I solve a problem

Thank you.

I definitely think those points you made will help me :)

Thanks for your response mate.

I'll try to solve more and more questions to get more experience :)

Thank you very much for your response.

Also, do you solve questions from a2oj ladders or based on a particular topic(from a2oj)?

(Also thanks for not going into energy saving mode Houtarou senpai :P)

Thanks a lot!!

"Tourist is no genius but he is simply good at it"

I really love tourist lol

Can you attach a link to the interview? It is always not a bad idea to read an interview of any LGM :)

Isn't that a bit unfair?

I mean the execution time was almost doubled due to sharing and if this happens during contests isn't there a possibility that someone gets a TLE and someone else with a similar code gets AC?

What if a code doesn't even pass the pretests and some other similar code passes the pretests and gets accepted?

On sankalp_ → Help Needed, 9 years ago
+1

Thanks fam!

Lol I misunderstood the question and I thought consecutive dances should not have more than one dancer in common.

I should really stop thinking about sequences :)

-16

Well... What if there is not gonna be a contest because April fools :P

On sankalp_ → Help needed in Go, 9 years ago
0

Thank you very much fam :)

It doesn't take L to figure out that he is indeed retarded lol.

DEBUGGING:

I don't know where you are, but I will find you.. and I'll fix you

Your pun is soo POLARizing

It is borderline un BEARable :P

-23

Lol that's because you don't watch stranger things

-33

Fun Fact:

Mille Bobby Brown is dating the biggest douche on earth(Jacob Sartorius)...

If you don't know who that guy is, just don't watch a song called "sweatshirt" by him.

I REPEAT DO NOT LISTEN OR WATCH THE SONG SWEATSHIRT BY JACOB SARTORIUS.

+37

Come on guys, no need to be so harsh.

Everyone makes mistakes. Maybe they had a rough day and didn't properly check their solutions. Look at the number of up votes for this announcement before and after the UPD.

It is tougher to come up with a quality question than to solve it so we have to appreciate them.

Good job guys!! Make sure you double check your solutions next time ;)

On tourist → Hello 2018, 9 years ago
+141

And porn

On Lewin → Good Bye 2017, 9 years ago
0

Did anyone else get 0:00 on timer and contest not yet started message??

On Lewin → Good Bye 2017, 9 years ago
+3

But... it's not even december 31st..