May 12th, 2009, 12:22 am
I think this strategy might work:First the prisoners number the colors 1,2,....,M.Then prisoner n (just anybody will do) looks at all the other hats and does the following thing:for every prisoner i (i=1,2,...,n-1) he looks at his hat, and the adds the number of the color of this hat to the grand total X.So he gets the number X as sum of n-1 numbers.He then finds Y = X mod M. He goes up to the kind and says the color corresponding to number Y.Doesn't matter if he gets killed or not, the important thing is everyone else can survive now.Prisoners 1,2,....,n-1 go in order of their indexes.Then for prisoner i (i=1,2,...,n-1) he knows the total Y_{i} of numbers of colors of his hat and all hats of remaining prisoners (i, i+1,...,n-1), because he can keep subtracting z_{i} from Y-{i-1} (in terms of mod M, where Y=Y_{1}) and z_{i} is number of color of hat of prisoner who just went before him.He also knows total T_{i} of numbers of hats of remaining prisoners (i+1,...,n-1), NOT including prisoner i, by just adding all numbers of colors of hats he sees.Then Y_{i}-T_{i} mod M is number of color of prisoner i. But this number Y_{i}-T_{i} allows prisoner i to find what color his hat is (from the assigning of "color numbers" in the beginning). So prisoner i gets the right color and survives.So all prisoners 1,2,...,n-1 survive. This is optimal because first prisoner to go up has no way of knowing his hat color so there is no guarantee all n prisoners will survive.