Surge226's blog

By Surge226, history, 2 months ago, In English

Hello everyone, I am trying to solve Required Substring problem from CSES.

I thought about a combinatorial solution for the problem. My idea was that for every $$$i < n$$$

  • if $$$i$$$ does not divide $$$n$$$, then there would be total $$$n$$$ arrangements which would be similar, so we would have to count $$$ways = \frac{\text{# of arrangements}}{n}$$$ in it, for ex, in sample test case, $$$n=4, m=3$$$, arrangement, {$$$1,1,1,2$$$}, {$$$1,1,2,1$$$}, {$$$1,2,1,1$$$}, {$$$2,1,1,1$$$} would be considered similar
  • if $$$i$$$ divides $$$n$$$, then there would be total $$$i$$$ arrangements which would be similar, so we would have to count $$$ways = \frac{\text{# of arrangements}}{i}$$$ in it, for ex, in sample test case, $$$n=4, m=3$$$, arrangement, {$$$1,2,1,2$$$}, {$$$2,1,2,1$$$} would be considered similar

So, I am iterating from $$$0$$$ to $$$n-1$$$, and if current period is prime and it divides total number of pearls $$$n$$$, then we can subtract the number of arrangements which it can form, from total number of arrangements and add the number of ways to the answer. We should also check whether, current period is prime because the period for $$$4$$$ would be same as $$$2$$$

My Code

Surprisingly, my code worked for smaller test cases, but it is giving wrong answer for large test cases. I checked the modular operations also and the factorial and prime conditions, but cannot figure out the issue. If anyone can help me out in this, I would highly appreciate it.

Thanks

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By Surge226, history, 3 months ago, In English

Hello everyone, I am trying to solve Required Substring problem from CSES.

First of all, I tried to solve it using combinatorics. My idea was that consider $$$n=6$$$ and string $$$s=$$$ABCDB with 5 characters, then the number of ways we can make $$$n$$$ character string with string $$$s$$$ in it, would be to fix string $$$s$$$ at first position in $$$n$$$ characters and fill rest positions with $$$26$$$ characters, giving $$$numberOfWays = 26$$$. Then, we can shift the substring for $$$(n - m)$$$, i.e, $$$6 - 5 = 1$$$ times to right, and we would get $$$numberOfWays = (n - m + 1) * 26^{(n - m)}$$$, which would, for sample test case, result in $$$numberOfWays = (6 - 5 + 1) * 26^{(6 - 5)} = 52$$$

But, it failed for this test case $$$n=6$$$ and $$$s=$$$ AA. And, I am not able to figure it out, why this is not working because of the large output.

Then, I peeked the solution here, and tried it to code myself, but in the end it is not working. My idea was same as the solution, that is, to build string character by character, and we track progress of building required string as our substring.

My Code

And, the Solution is following slightly different approach.

Solution

I think my code is faulty at many places but if someone can tell what mistakes my code is doing, or even the main problem of my code, then it would be really helpful for me.

Thanks

Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By Surge226, history, 4 months ago, In English

Hello everyone, I am trying to do Sliding Window Median problem from CSES. I have thought of multiset approach which is pretty close to one of its solutions and wrote this peice of code.

My Code

When I ran it on larger test case, it is taking this much time.

real    0m50.343s
user    0m50.003s
sys     0m0.195s

And, on the running the USACO solution, following same approach.

USACO solution

It is taking this much time.

real    0m1.396s
user    0m1.178s
sys     0m0.214s

Now, I am not able to understand that why the time taken is increasing so drastically, even though the complexities are same. I can share the complete code also if it would help. Thanks

Full text and comments »

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