The Priest Mathematician of ultraviolet A 10254

Source: Internet
Author: User

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]);
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.