codeforces 1194 D. 1-2-K Game

Solution one:

For each question it is intended to be seen from a position as long as the upper hand can come to a losing position, the upper hand will win; 0 is known as the upper hand losing position, traversing the 1-n, and then click OK; time complexity is o (TN) space complexity o (N) .MLE, TLE, so do not pay attention to the problem of time complexity, but also concerned about space complexity;

#include<bits/stdc++.h>
using namespace std;

void solve();

int main()
{
	int T;
	cin >> T;

	for (int t = 0; t < T; t++) solve();

	return 0;
}

void solve()
{
	int n, k;
	cin >> n >> k;
	vector<int> state(n + 1, 0);

	for (int i = 1; i <= n; i++)
	{
		if ((i - 1 >= 0 && state[i - 1] == 0) || (i - 2 >= 0 && state[i - 2] == 0) || (i - k >= 0 && state[i - k] == 0)) state[i] = 1;
	}

	if (state[n] == 1) cout << "Alice" << endl;
	else cout << "Bob" << endl;

	return;
}

  

Solution two :( ah, this is very difficult to think, how to find the law, when there is an uncertain time, you can see the relationship with the rest of the law to determine the condition of the find. Double game of chance most important thing is to find the state will lose or will win state)

When only 2 kinds of move time, (0, 3, 6, 9 ..... 3x) position is the position of losing the upper hand, the upper hand to win the rest position;

! When k% 3 = 0, or 1 or 2, the upper hand for the 3x losing bits, the remainder bits win upper hand;

When k% 3 == 0, when i <k, i% 3 == 0 is sente losing bits, i == k, the upper hand is a winning position. k + 1 bits of losing the upper hand, according to the n-block k + 1, n% = k + 1; if (n == 0) losing the upper hand, if n == k upper hand will win, if the remaining n% 3 == 0 losing the upper hand, the upper hand will win otherwise;

#include<bits/stdc++.h>
using namespace std;

void solve();

int main()
{
	int T; 
	cin >> T;

	for (int t = 0; t < T; t++) solve();

	return 0;
}

void solve()
{
	int n, k;
	cin >> n >> k;

	if (k % 3 != 0)
	{
		if (n % 3 == 0) cout << "Bob" << endl;
		else cout << "Alice" << endl;
	}
	else
	{
		n %= (k + 1);

		if (n == k || n % 3 != 0) cout << "Alice" << endl;
		else cout << "Bob" << endl;
	}
}

  

Guess you like

Origin www.cnblogs.com/mychen06/p/12604067.html