ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1012. K-based Numbers. Version 2

My Code
Posted by Imran Yusubov 13 Apr 2009 10:45
import java.math.BigInteger;
import java.util.Scanner;

public class bigKnum {

    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        int k=in.nextInt();

        BigInteger f1,f2,f3;
        f1=BigInteger.valueOf(k-1);
        f2=f1.multiply(BigInteger.valueOf(k));

        for(int i=2;i<n;i++)
        {
            f3=(f1.add(f2)).multiply(BigInteger.valueOf(k-1));
            f1=f2;
            f2=f3;

        }
        System.out.print(f2);

    }

}
Re: My Code
Posted by Filip Franik 14 Aug 2017 17:40
Instead of using three temporary variables f1, f2, f3 make one array of three BigInteger elements f and just index it using modulo like so f[i%3], f[(i-1)%3], f[(i-2)%3]. This works exactly the same, and you don't have to make this awful and confusing data move of f1=f2; f2=f3;