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:
x
with 0 < x < n
and n % x == 0
.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
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
temp.f95:46:12: 46 | contains | 1 Error: CONTAINS statement at (1) is already in a contained program unit temp.f95:49:25: 49 | implicit none | 1 Error: Duplicate IMPLICIT NONE statement at (1) temp.f95:50:39: 50 | integer, intent(inout) :: n | 1 Error: Symbol ānā at (1) already has basic type of INTEGER temp.f95:51:36: 51 | integer, intent(in) :: x | 1 Error: Unexpected data declaration statement at (1) temp.f95:54:11: 54 | end subroutine make_move | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:53:12: 53 | n = n - x | 1 Error: Dummy argument ānā with INTENT(IN) in variable definition context (assignment) at (1)
!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
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7d978fcf4960 in ??? #1 0x7d978fcf54d9 in ??? #2 0x7d978ff4917b in ??? #3 0x7d978ff42684 in ??? #4 0x7d978ff432aa in ??? #5 0x5a2b1fa04219 in MAIN__ #6 0x5a2b1fa0444a in main
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]
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.
#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];
}
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.