| Krofna (269) | ||||||||
This problem was on yesterdays Croatian Open Competition in Informatics:
My solution:
Sample I/O:
It turned out to either wrong on some input or slow(1 sec time limit on a crap machine). What did I do wrong here? | ||||||||
|
Last edited on
|
||||||||
| helios (10126) | |||
I can't find anything wrong with your code, besides the possibility of a stack overflow. I would have written it like this:
| |||
|
Last edited on
|
|||
| ne555 (4041) | |||
|
>slow(1 sec time limit on a crap machine) Your algorithm is O(n), ¿how big is B-A? You need to make it O(1) Supposing N<2^32, the maximum `smallest positive integer which doesn't divide N' is less than 30 (¿how did I obtain that?) Instead of a linear walk, work with the extremes. Given a number N, ¿how many numbers n<=N are not divisible by 2? Their strength will be 2 Now consider the numbers that are divisible by 2, ¿how many are not by 3? That's easy, two thirds. ... The numbers that are divisible by 2, 3, 4, 5, 6 but not by 7: six sevenths (be careful with modulus) (I'll let you figure out the composite numbers) Compute the strength of the numbers from 2 to 30. Store in an array.
how_many counts n<=N | mod(n,b)== 0, b<K and mod(n,K)!=0 Be careful to not recount the numbers from 2 to 30 | |||
|
|
|||
| helios (10126) | |
|
It's not as simple as that, ne555. According to you, how many integers between 1 and 100 are divisible by 2 and not 3? And by 2 and 3 but not 4? | |
|
|
|
| ne555 (4041) | |
|
Because 3 is prime, for the number to be a multiple of 3 it must be of the way n=3k We have numbers of the form k=2j so that means n=3*2*j -> j = floor( n/(3*2) ) Those are the divisible by 2 that are also divisible by 3. But you want n/2 - j In your example 50-16 = 34 In the case of a composite, you've got 4 that's 2*2 and we got numbers that are n=2*3*k, so we need that k=2j -> j=n/(2*2*3) In the example 16 - 8 = 8, namely: 6, 18, 30, 42, 54, 66, 78, 90. So as general rule how_many(N,K) = N/mcm(1..K-1) - N/mcm(1..K) Edit: Sorry, I think you know the mcm as LCM (least common multiple) > Be careful to not recount the numbers from 2 to 30 Well, that can't happen. The number can't be in its strength set. | |
|
Last edited on
|
|
| tntxtnt (61) | ||||
|
my solution for 3 <= A < B < 10^17 sumStrength_f (a, b) sumStrength (a, b) is for checking sumStrength_f()
just some scratch paper work and you'll realize a pattern here for the strength of numbers... it only returns 2 (odd) , 3 (even, not special) or 4 (even + special) | ||||
|
Last edited on
|
||||
| helios (10126) | |
| Yes, that's correct. It wasn't at all clear that that's what you meant, in your previous post. | |
|
|
|