fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static BigInteger fooBI2(int n) {
  12. BigInteger[] vars = new BigInteger[3];
  13. vars[0] = BigInteger.valueOf(1L);
  14. vars[1] = BigInteger.valueOf(2L);
  15. vars[2] = BigInteger.valueOf(4L);
  16. for (int i = 3; i < n; i++) {
  17. vars[i%3] = vars[0].add(vars[1]).add(vars[2]);
  18. }
  19. return vars[(n-1)%3].mod(BigInteger.valueOf((long) (Math.pow(10, 9) + 7)));
  20. }
  21.  
  22. public static int fooBI3(int n) {
  23. int p = 1000000007;
  24. int[] vars = new int[3];
  25. vars[0] = 1;
  26. vars[1] = 2;
  27. vars[2] = 4;
  28. for (int i = 3; i < n; i++) {
  29. vars[i%3] = ((vars[0] + vars[1])%p +vars[2])%p;
  30. }
  31. return vars[(n-1)%3];
  32. }
  33.  
  34. public static void main (String[] args) throws java.lang.Exception
  35. {
  36. System.out.println("15: " + fooBI2(15) + " " + fooBI3(15));
  37. System.out.println("20000: " + fooBI2(20000) + " " + fooBI3(20000));
  38. }
  39. }
Success #stdin #stdout 0.2s 98640KB
stdin
Standard input is empty
stdout
15: 5768  5768
20000: 886919695  886919695