11246 - K-Multiple Free set
Moderator: Board moderators
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
11246 - K-Multiple Free set
Hi
I got AC this question in the contest time but when this question added in online judge problemset I got TLE. What's Wrong?
My algorithm is:
I first select the numbers that is not the factor of k then consider the factors of k. suppose x is a factor of k (x = ak) if I selected a or a is belong to non-factors of k I don't select x otherwise select it.
for example consider n = 10 an k = 2. First I select 1, 3, 5, 7, 9. Now only the factors of 2 remains we cannot select 2 because we select 1 so we don't select 2 and this cause we select 4. We cannot select 6 because we select 3. We cannot select 8 because we select 4. We cannot select 10 because we select 5.
and I use an array named select which select = (i + 1) * k It means I only keep the factors of k and select occupy only one bit
Thanks
I got AC this question in the contest time but when this question added in online judge problemset I got TLE. What's Wrong?
My algorithm is:
I first select the numbers that is not the factor of k then consider the factors of k. suppose x is a factor of k (x = ak) if I selected a or a is belong to non-factors of k I don't select x otherwise select it.
for example consider n = 10 an k = 2. First I select 1, 3, 5, 7, 9. Now only the factors of 2 remains we cannot select 2 because we select 1 so we don't select 2 and this cause we select 4. We cannot select 6 because we select 3. We cannot select 8 because we select 4. We cannot select 10 because we select 5.
and I use an array named select which select = (i + 1) * k It means I only keep the factors of k and select occupy only one bit
Thanks
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
Thanks for your reply sclo. Can you give me some hint? I saw Double-Free Set recurrence that works for k = 2:
But how above recurrence work?
Code: Select all
f(n) = ceil(n / 2) + f(floor(n / 4))
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
Last edited by saman_saadi on Mon Jul 30, 2007 1:16 am, edited 1 time in total.
-
- A great helper
- Posts: 383
- Joined: Mon Oct 18, 2004 8:25 am
- Location: Bangladesh
- Contact:
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
You don't need BigInt. "int" is enough because n <= 1000000 < 2 ^ 31 - 1 You don't say anything about your algorithm but you can use emotional blind recurrence:
Think about the recurrence. why it's true and what it means?AFAIK
it should be like this
f(n,i) = n/(k^i) - f(n, i+1)
Result is: f(n,0)
SPOILER??
+/-wa
Hi saman_saadi,
thank you by the hint
here is my code. it's use emotional blind recurrence:
input:
output:
you can to see my error?
thanks.
thank you by the hint
![:D](./images/smilies/icon_biggrin.gif)
here is my code. it's use emotional blind recurrence:
Code: Select all
AC
Code: Select all
3
10 2
100 2
1000 2
Code: Select all
6
62
625
thanks.
Last edited by adelar on Mon Aug 06, 2007 11:23 pm, edited 1 time in total.
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
1) Don't use float, double, long double (same as double in some compilers) as sentinel. for example:
you declare t as long double. In my compiler your program didn't terminate so you must declare t as int.
2) I think you don't understand the recurrence correctly. Before use it try to prove it. emotional blind don't explain the recurrence too much because he wants we think about it.
3) In my computer your code only output 0
Code: Select all
double i;
for (i = 0; i < 3; i++) //dangerous code
{
}
2) I think you don't understand the recurrence correctly. Before use it try to prove it. emotional blind don't explain the recurrence too much because he wants we think about it.
3) In my computer your code only output 0
I thought the IEEE standard for double precision floating point numbers requires them to represent 52bit fractional digits. So it follows that they can represent every 32bit integers exactly. However, some decimal such as 0.1 don't have a finite base 2 representation, so they can't be represented exactly. As a rule of thumb, float's range and precision is so limited that I can't find any reason to use floats, unless you're really out of space.saman_saadi wrote:1) Don't use float, double, long double (same as double in some compilers) as sentinel. for example:
The error above lies in that we requires only integer floor, not floating point division.
-
- New poster
- Posts: 31
- Joined: Sun Feb 25, 2007 6:33 pm
- Location: Tehran
Yes you right But I think for more clarity when we need an integer value we use int. see below code:I thought the IEEE standard for double precision floating point numbers requires them to represent 52bit fractional digits. So it follows that they can represent every 32bit integers exactly. However, some decimal such as 0.1 don't have a finite base 2 representation, so they can't be represented exactly. As a rule of thumb, float's range and precision is so limited that I can't find any reason to use floats, unless you're really out of space.
The error above lies in that we requires only integer floor, not floating point division.
Code: Select all
#include <stdlib.h>
#include <stdio.h>
int main()
{
long double a;
scanf("%lf", &a);
while (a--);
return 0;
}