UVA_10254
If we set f [I] to four columns, the minimum steps for moving I from one column to another are as follows, if g [I] is set to three columns, we can obtain f [n] = min {2 * f [k] + g [n-k]}. where g [I] is known as a 2 ^ I-1.
Then I couldn't proceed. I read other people's reports and said that I was looking for a rule, o (∩ □∩) o. With the above formula, we print the first 60 solutions is still very good print, and f [I]-f [I-1] also printed out, at this time, we will find that f [I]-f [I-1] is a power of 2, and the k power of 2 appears k + 1 consecutive times, so we can pre-process all the solutions based on this feature.
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
BigInteger[] f = new BigInteger[10010];
f[0] = new BigInteger("0");
int k = 1, cnt = 0;
BigInteger d = new BigInteger("1");
for(int i = 1; i <= 10000; i ++)
{
f[i] = f[i - 1].add(d);
cnt ++;
if(cnt == k)
{
cnt = 0;
k ++;
d = d.multiply(BigInteger.valueOf(2));
}
}
while(cin.hasNext())
{
int n = cin.nextInt();
System.out.println(f[n]);
}
}
}