Codeforces 39e what has Dirichlet got to do with that? Game + Memory search

Source: Internet
Author: User

Question link: Click the open link

Question:

Given a box, B ball constant N (the ball and the box are different and there will be no identical items)

Set Way = put B balls in a box. If way> = N, the game ends.

Two people play games.

If the current turn is X

1. Select "X" to add a box or a ball.

2. If the number of methods> = N after adding the number, X fails.

If the first hand wins, the output is Masha. If the first hand wins, the output is Stas. If the first hand wins, the output is missing.

Ideas:

Memory-based search

If a ++ or B ++ is given at present, the current situation will fail if it is greater than or equal to n.

Transfer from the situation where no> = N is involved.

Note that if there is only one box and another box is added, it will be greater than or equal to N, so it is inevitable that only B will be added, so there is no solution.


If there is only one ball and another ball is no solution, then only a can be added, then the parity of N-A can be achieved to the current situation.

# Include <cstdio> # include <iostream> # include <string. h ># include <map> using namespace STD; # define ll long longll n, a, B; bool win (LL X, ll y) {// y balls in X boxes. The point is the Fail state ll TMP = 1; for (ll I = 1; I <= y; I ++) {TMP * = x; If (TMP> = N) return true;} return false;} Map <pair <LL, ll>, ll> MP; ll DFS (ll x, ll y) {// The status of this vertex if (MP. find (pair <LL, ll> (x, y ))! = MP. end () return MP [pair <LL, ll> (x, y)]; If (x = 1 & Win (2, y )) return MP [pair <LL, ll> (x, y)] =-1; if (y = 1 & Win (x, 2 )) {If (n-x) & 1) return MP [pair <LL, ll> (x, y)] = 0; return MP [pair <LL, ll> (x, y)] = 1;} If (WIN (x, y) return MP [pair <LL, ll> (x, y)] = 1; ll u = Win (x + 1, Y), V = Win (X, Y + 1); If (u = 1 & V = 1) return MP [pair <LL, ll> (x, y)] = 0; If (u = 0) u = DFS (x + 1, y ); if (V = 0) V = DFS (X, Y + 1); If (u = 0 | V = 0) Return MP [pair <LL, ll> (x, y)] = 1; if (u =-1 | V =-1) return MP [pair <LL, ll> (x, y)] =-1; return MP [pair <LL, ll> (x, y)] = 0;} int main () {ll, b; while (CIN> A> B> N) {MP. clear (); If (WIN (a + 1, B) & Win (A, B + 1) {puts ("Stas"); continue ;} if (B = 1 & Win (A, 2) {If (! (N-a) & 1) puts ("Masha"); else puts ("Stas"); continue;} ll TMP = DFS (A, B ); if (TMP <0) puts ("missing"); else TMP? Puts ("Masha"): puts ("Stas");} return 0 ;}


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.