Timus 1180. Stone Game Problem Solving report

Source: Internet
Author: User

1. Title:

1180 Stone GameTime limit:1.0 Second
Memory limit:64 mbtwo nikifors play a funny game. There is a heap of NStones in front of them. Both nikifors in turns take some stones from the heap. One may take any number of stones with the only condition that this number is a nonnegative integer power of 2 (e.g. 1, 2, 4, 8 etc.). Nikifor who takes, the last stone wins. You is to write a program, that determines winner assuming Nikifor does it best. Inputan input contains the only positive integer number N(condition N≤10^250 holds). Outputthe first line should contain 1 in the case the first Nikifor wins and 2 in case the second one does. If the first Nikifor wins the second line should contain the minimal number of stones he should take at the first move in Order to guarantee his victory. Sample
input Output
8
12

2. Solving Ideas

This kind of game problem, all from the simplest case consideration, recursion to the complex situation. But there are some interesting tricks to this problem ~

Basic recursion:

N win?

1 y

2 y

3 N

Well. Analysis here, the idea is probably this:

BOOL Suc[maxn+1];

SUC[1] = suc[2] = true;

for (i = 3; I <= n; i++) {

for (b = 1; b < n; b *= 2) {

if (suc[i-b] = = False) {

Suc[i] = true;

Break

}

}

}

However, looking back at the size of the topic, found that the value of n range is n <= 10^250. This is a high-precision rhythm.

No hurry, go back and see if there is any optimization method. By finding patterns,

Found: n = 3*n, suc[n] = n

N = 3*n + 1 | | N = 3*n + 2, suc[n] = y

This problem is converted to n%3.

and a decimal number n = SUM (ai*10^i) = SUM (AI) + sum (ai* (10^i-1)) (i>=0)

and 10^i-1 = 0 (mod3)

So n = SUM (AI) (mod3)

3. Code:

#include <iostream>using namespacestd;intMain () {ints =0; Chartmp;  while(CIN >> tmp) s + = tmp-'0'; if(s%3==0) cout <<2; Elsecout <<1<<'\ n'<< S%3; //return 0;}

Timus 1180. Stone Game Problem Solving report

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.