|
|
back to boardMy Code 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 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; |
|
|