HDU1848-Fibonacci again and again

Source: Internet
Author: User

Question Link

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1848

The code for this question is not very complex, but the equivalence class (SG function) is not fully understood,

Difficult questions, but the code is not very complex. I found some explanations on the Internet,

Game questions are hard to understand at first. I used that theorem.

 

For a nim game location (x1, x2, X3 ), the current location is at the point of failure

 

At that time, I was looking for a tutorial on the Internet and introduced a lot of things. It took me a long time to understand them. I will give a link to the big bull's problem-solving report later. Here I will talk about my understanding.

 

This question uses the theorem above. There are three piles of stones that can only be removed from each pile at a time. Since each Heap has different methods, the numbers that may have the same situation are classified as one type. It is called an equivalence class. Each class corresponds to a number called the number of equivalence classes.

 

Number of equivalence classes algorithms (0 is actually a point of defeat, for a bunch)


E [0] = 0;


Number of equivalence classes E [I] algorithm:

 

Remove fib [1], FIB [2] From I..., FIB [J] <= I and I-fib [1], I-fib [2],..., i-fib [J]

The minimum number that does not appear in their equivalence classes is the number of I equivalence classes.

 

For example, I = 1,

Take fib [1] = 1 I-fib [1] = the number of equivalence classes is 0, and the minimum number that does not appear is 1

E [1] = 1;

 

For example, I = 2,

Remove fib [1] = 1 I-fib [1] = 1, remove fib [2] = 2 I-fib [1] = 0,
The number of equivalence classes between 1 and 0 is 1 and 0, and the minimum number that is not displayed is 2.

E [2] = 2;

 

For example, I = 3

Remove fib [1] = 1 I-fib [1] = 2, remove fib [2] = 2 I-fib [1] = 1, remove fib [3] = 3 I-fib [1] = 0,

The number of equivalence classes of 2, 1 and 0 is 2, 1, 0, and the minimum number that does not appear is 3.

E [3] = 3;

 

For example, I = 4

Take fib [1, 2] = 1, 2, 3 left 3, 2, 1, the minimum number that does not appear is 0

E [4] = 0; 4 is a point of defeat

 

For example, I = 5

Take fib [,] =, 5 remaining, the number of equivalence classes is, and the minimum number of 2, 0 that does not appear is 1

E [5] = 1;

 

In this way, an array of the number of equivalence classes is obtained. Then we will use that theorem. If a certain point of failure appears at the beginning, that is(E[N] ^E[M] ^E[P]) =0. Therefore, we must follow the optimal rule. In other cases, we will win.

 

For more information, see.

 

Http://hi.baidu.com/king___haha/blog/item/9addc65ae96948272934f029.html

 

Code)

# Include <stdio. h>
# Include <string. h>
Int main (void)
{
Int I, j, k, n, m, p;
Int A [1005], B [20], C [16]; // Why do the arrays only need to be opened to 16?
B [1] = 1; B [2] = 2;
For (I = 3; I <= 16; I ++)
B [I] = B [I-1] + B [I-2];
A [0] = 0; A [1] = 1;
For (I = 2; I <= 1000; I ++)
{
A [I] = I;
Memset (C, 0, sizeof (c ));
For (j = 1; B [J] <= I; j ++)
{
C [A [I-B [J] = 1;
}
For (j = 0; j <= 15; J ++) // Why do I have to wait for 15 minutes?
{
If (C [J] = 0)
{
A [I] = J;
Break;
}
}
}
While (scanf ("% d", & N, & M, & P) = 3 & (n + M + p ))
{
If (A [n] ^ A [m] ^ A [p])
Printf ("fibo \ n ");
Else
Printf ("nacci \ n ");
}
Return 0;
}

 

 

Hdu1848 Fibonacci again and again 3 heap

Solution report
FIB [1 ..] = {1, 2, 3, 5, 8, 13, 21,...}; is the Fibonacci sequence, FIB [] <= 1000
Only fib [I] can be obtained at a time.

1. If there are only 1 heap of M, M is a fib [I], M is a winning point, M =, 21,... is a winning point.
It is easy to know that 0, 4 is a point of failure. If K (k = fib [I]) is removed from M, M-K is a point of failure.
Then M is a winning point.

6-2 = 7-3 = 9-5 = 4 is the point of defeat, 6, 7, 9 is the point of victory

M = 10 M-1 = 9, m-2 = 8, M-3 = 7, M-5 = 5, m-8 = 2

From M = 10, all methods are winning points => M = 10.


In this way, when there is only one heap, We can find all the vertices to be defeated. W [1001] indicates that W [I] = 0 indicates that I is the vertices to be defeated.
W [I] = 1 indicates that I is a winning point,
W [0] = 0; W [1] = W [2] = W [3] = 1;
For (I = 4; I <= 1000; I ++)
{
For (j = 1; FIB [J] <= I; j ++) if (W [I-fib [J] = 0) {W [I] = 1; break ;}

// If the number of all Fibonacci instances wins, this point will be defeated.

If (FIB [J]> I) W [I] = 0;
}

 

 

When there is only one heap, all points will fail:
0 4 10 14 20 24 30 36 40 46 50 56 60 66 72 76 82 86 92 96 102 108 112 118 122
132 138 150 160 169 176 186 192 196 202 206 212 218 222 228 232 238 242 248 254
260 264 270 274 280 284 290 296 300 306 310 316 322 326 332 338 342 348 352 358
364 368 374 378 384 388 394 400 406 410 416 420 426 430 436 442 446 452 456 462
468 472 478 484 488 494 498 504 510 514 520 524 530 534 540 552 556 562 566 572
576 582 588 592 598 602 608 618 635 644 662 672 688 694 698 704 708 714 723 730
734 740 750 754 766 772 776 782 791 798 808 814 818 824 830 834 840 844 850 854
860 866 872 876 882 886 892 896 902 908 912 918 922 928 934 938 944 950 954 960
964 970 976 980 986 990 996

 


2. There are 2 heaps of M and N

If both m and n are (1 heap) points, 2 heap (m, n) points are also points of failure.
Generally, a team is defeated + A team is defeated = A team is defeated. Here + is not to combine two teams into one heap.
Defeat + victory = victory

Win + win =? Not necessarily

For example, 2 heap () is mandatory

(4, 5) Yes

Yi Zhi (M, m) is defeated


Definition: If (m, n) fails, m and n are equivalent.
Equal promotion: M = n, m and n are equal.

When there is only one heap, all the failed points are equivalent to 0. We say that when there is only one heap, all the failed points are 0th class or their number of equivalence classes is 0;

2 heap () is defeated. 5 and 1 are equivalent. m, which is equivalent to 1, is called 1.

2. W2 [M1] [m2] indicates the heap. W2 [M1] [m2] = 0 indicates that (M1, M2) will fail,
W2 [M1] [m2] = 1 indicates that (M1, M2) will win

The preceding method can be used to calculate that the number of equivalence classes is 1. For example, the number of equivalence classes for 1, 5, and 11 is 1.

Equivalent replacement can be performed, for example, 3 heap, 1, 5, and 7 can be replaced by 1, 1, and 7, which is a win.


3. Conclusion (no proof) if the number of equivalence classes M, N, and P is calculated, E [m], E [N], E [p]

Make S = E [m] ^ e [N] ^ e [p]


S = 0 is the point of defeat.


Number of equivalence classes
E [0] = 0;


Algorithm of the number of equivalence classes E [I]: Remove fib [1], FIB [2],..., FIB [J] After I <= I

I-fib [1], I-fib [2],..., I-fib [J]

The minimum number that does not appear in their equivalence classes is the number of I equivalence classes.

For example, if I = 1, the number of classes with fib [1] = 1 I-fib [1] = is 0, and the minimum number that is not displayed is 1.

E [1] = 1;

For example, I = 2, FIB [1] = 1 I-fib [1] = 1, FIB [2] = 2 I-fib [1] = 0,
The number of equivalence classes between 1 and 0 is 1 and 0, and the minimum number that is not displayed is 2.

E [2] = 2;

For example, I = 3, FIB [1] = 1 I-fib [1] = 2, FIB [2] = 2 I-fib [1] = 1, remove fib [3] = 3 I-fib [1] = 0,
The number of equivalence classes of 2, 1 and 0 is 2, 1, 0, and the minimum number that does not appear is 3.

E [3] = 3;

For example, I = 4, take fib [, 3] =, 3 remaining 3, 2, 1, the minimum number that does not appear is 0

E [4] = 0; 4 is a point of defeat

For example, I = 5, take fib [1, 3, 4] = 1, 2, 3, 5 left, 4, 3, 2, 0, the number of equivalence classes is 0, 3, 2, 0 does not appear, the minimum number is 1

E [5] = 1;

 


// Calculate the number of equivalence classes
E [0] = 0; E [1] = 1;
For (I = 2; I <= 1000; I ++)
{// Assume that I <= 1000 is the number of equivalence classes <= 15

For (j = 0; j <= 15; J ++) H [J] = 0; // H [J] = 0, J does not appear, H [J] = 1, J appears
For (j = 1; FIB [J] <= I; j ++)
{
H [E [I-fib [J] = 1;

}
For (j = 0; j <= 15; J ++) if (H [J] = 0) {e [I] = J; break ;} // H [J] = 0, J does not appear
}

 

Therefore, the calculation steps are as follows:
1. Calculate the Fibonacci sequence fib [I] and calculate it until fib [I]> 1000.
2. Calculate the number of equivalence classes E [I]

3. above 3

 

HDU1848-Fibonacci again and again

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.