HDU 1848 Fibonacci again and again "SG function"

Source: Internet
Author: User

For the Nim game, any singular situation (a,b,c) has a^b^c=0.

Extension: Any singular situation (A1, A2,... an) satisfies a1^a2^...^an=0

The MEX (minimal excludant) operation is defined first, which is an operation applied to a set that represents the smallest non-negative integer that does not belong to this set.

such as Mex{0,1,2,4}=3, Mex{2,3,5}=0, mex{}=0.

For a given, forward-free graph, define the Sprague-garundy function g for each vertex of the graph as follows:

g (x) =mex{g (y) | Y is the successor of X.

SG Function Properties:

1, all endpoints SG value is 0 (because its successor is an empty set)

2,SG is a vertex of 0, and all subsequent y of it satisfies SG not to be 0

3, for a SG not 0 vertex, there must be a successor to meet the SG is 0

4, meet the combined game nature: All SG is 0 fixed point corresponding P point, SG greater than 0 vertices corresponding to n points

sg theorem (Sprague-grundy theorem):

G (g) =g (G1) ^g (G2) ^...^g (Gn). The game's and SG function values are the XOR of all its sub-games ' SG function values.

#include <stdio.h>#include<string.h>Const intn=1111;intp[n],sg[n],f[ +];voidinit () {inti,j; f[1]=1, f[2]=2;  for(i=3;; i++) {F[i]=f[i-2]+f[i-1]; if(f[i]>n) Break; }     for(i=1; i<= +; i++){         for(j=1; f[j]<=i;j++) {P[sg[i-f[j]]]=i; }         for(j=0; j<=i;j++){            if(p[j]!=i) {Sg[i]=K;  Break; }        }    }}intMain () {init (); intn,m,p;  while(~SCANF ("%d%d%d",&n,&m,&p)) {        if(!n&&!m&&!p) Break; if(Sg[n]^sg[m]^sg[p]) puts ("Fibo"); ElsePuts"Nacci"); }    return 0;}

HDU 1848 Fibonacci again and again "SG function"

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.