import java.util.*; /* * Problem: Tarot Knight (NAIPC19) * Author: Alex Coleman * Complexity: O(n*k*lg) * where k = number of unique gcd's reachable from subsets of * n numbers up to a or b (10^9), k ~ 30,000 * lg = log(10^9), but is from gcd function so very low overhead * * We can always compress a single card (a,b) to (g,0) or (g,g) where g = gcd(a,b) * If a/g and b/g are = mod 2, then we can show it's equal to (g,0), otherwise it is (g,g) * Additionally, the reachability of (g,0) and (g,g) is easy to check (see card.canDo) * * Next, we can show that merging cards with value x and y similarly reduces to a single card * of type (g,0) or (g,g) where g=gcd(x,y) (see merge function for details) * * With this in mind, we can do a DP where state is the card we have, and transition is trying every card to merge with. * We can always assume we return to start after buying a card (since travel is not a cost). */ public class TarotKnight_arc { static final long oo = Long.MAX_VALUE/3; static HashMap memo; static int n,sx,sy; static Card[] cs; static int[] xs,ys; static long[] costs; public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); xs = new int[n]; ys = new int[n]; costs = new long[n]; cs = new Card[n]; for(int i=0;i(); for(int i=0;i