Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number n on the chalkboard. On each player's turn, that player makes a move consisting of:

  • Choosing any x with 0 < x < n and n % x == 0.
  • Replacing the number n on the chalkboard with n - x.

Also, if a player cannot make a move, they lose the game.

Return true if and only if Alice wins the game, assuming both players play optimally.

Example 1:

Input: n = 2 Output: true Explanation: Alice chooses 1, and Bob has no more moves.

Example 2:

Input: n = 3 Output: false Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.

Constraints:

  • 1 <= n <= 1000

Note: This problem is from LeetCode.
āŒ Compiled
āŒ Executed
āŒ Correct
program main
    implicit none

    integer :: n, x
    logical :: alice_wins

    ! Example 1
    n = 2
    alice_wins = solve(n)
    print '(L1)', alice_wins

    ! Example 2
    n = 3
    alice_wins = solve(n)
    print '(L1)', alice_wins

contains

    function solve(n) result(alice_wins)
        implicit none
        integer, intent(in) :: n
        logical :: alice_wins

        integer :: i
        logical :: can_make_move

        do while (n > 0)
            can_make_move = .false.
            do i = 2, n
                if (mod(n,i) == 0) then
                    can_make_move = .true.
                    exit
                end if
            end do

            if (.not. can_make_move) then
                alice_wins = .false.
                return
            end if

            call make_move(n, i)
        end do

        alice_wins = .true.

    contains

        subroutine make_move(n, x)
            implicit none
            integer, intent(inout) :: n
            integer, intent(in) :: x

            n = n - x
        end subroutine make_move

    end function solve

end program main
āœ… Compiled
āŒ Executed
āŒ Correct
!include "stdio.h"

integer :: n, x, y

! Read the input
read(*,*) n

! Initialize the chalkboard
x = 1
y = n

! Play the game
do while (x <= y)
    ! Check if x is a factor of n
    if (mod(n, x) == 0) then
        ! Replace the number on the chalkboard with n - x
        n = n - x
        ! Print the current state of the chalkboard
        write(*,*) "Alice: ", x, ", Bob: ", y
    end if
    ! Update x and y
    x = x + 1
    y = y - 1
end do

! Check if Alice wins
if (n == 0) then
    write(*,*) "Alice wins!"
else
    write(*,*) "Bob wins!"
end if

end
šŸŒ Data from online sources
def mincostTickets(days, costs):
  travel_days = set(days)
  dp = [0] * 366

  for i in range(1, 366):
    if i not in travel_days:
      dp[i] = dp[i - 1]
    else:
      dp[i] = min(dp[i - 1] + costs[0],
                  dp[max(0, i - 7)] + costs[1],
                  dp[max(0, i - 30)] + costs[2])

  return dp[365]
  1. Create a set of travel days.
  2. Initialize a dynamic programming (dp) array of length 366, where dp[i] will represent the minimum cost to travel on the given days up to the i-th day of the year.
  3. Iterate through the days of the year (1 to 365). a. If the current day is not a travel day, then there's no need to buy a pass for this day, therefore the minimum cost is the same as the previous day. b. If the current day is a travel day, calculate the minimum cost to travel by considering the three options: buying a 1-day pass, buying a 7-day pass, or buying a 30-day pass.
  4. Return the minimum cost to travel on all given days (dp[365]).

This algorithm leverages dynamic programming to calculate the minimum cost of traveling on the given days. By iterating through each day and updating the dp array based on previous results, the final minimum cost is obtained efficiently.

šŸŒ Data from online sources
#include <vector>
#include <unordered_set>
#include <algorithm>
using namespace std;

int mincostTickets(vector<int>& days, vector<int>& costs) {
  unordered_set<int> travel_days(days.begin(), days.end());
  vector<int> dp(366, 0);

  for (int i = 1; i <= 365; ++i) {
    if (travel_days.count(i) == 0) {
      dp[i] = dp[i - 1];
    } else {
      dp[i] = min({dp[i - 1] + costs[0],
                   dp[max(0, i - 7)] + costs[1],
                   dp[max(0, i - 30)] + costs[2]});
    }
  }

  return dp[365];
}
  1. Create a set of travel days.
  2. Initialize a dynamic programming (dp) array of length 366, where dp[i] will represent the minimum cost to travel on the given days up to the i-th day of the year.
  3. Iterate through the days of the year (1 to 365). a. If the current day is not a travel day, then there's no need to buy a pass for this day, therefore the minimum cost is the same as the previous day. b. If the current day is a travel day, calculate the minimum cost to travel by considering the three options: buying a 1-day pass, buying a 7-day pass, or buying a 30-day pass.
  4. Return the minimum cost to travel on all given days (dp[365]).

This algorithm leverages dynamic programming to calculate the minimum cost of traveling on the given days. By iterating through each day and updating the dp array based on previous results, the final minimum cost is obtained efficiently.