Advertisement
Guest User

revdiv7

a guest
Aug 29th, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Arrays;
  3.  
  4. public class div7 {
  5.   // main idea taken from here: https://www.math.hmc.edu/funfacts/ffiles/10005.5.shtml
  6.   public static int MAXD = 10000;
  7.   public static int[] res = {1,3,2,6,4,5}; // 10^i modulo 7, repeating
  8.  
  9.   public static void main (String[] args) {
  10.     long start = System.currentTimeMillis();
  11.     BigInteger sum = count(MAXD);
  12.     int x = 0;
  13.     for (char c : sum.toString().toCharArray()) x += c-'0';
  14.     System.out.println("Sum digits: " + x);
  15.     if (sum.toString().length() < 1000) System.out.println("Sum: " + sum);
  16.     System.out.println("Time: " + (System.currentTimeMillis()-start) + "ms");
  17.   }
  18.  
  19.   public static BigInteger count(int lim) {
  20.     int bf = 0, ff = (lim - 1) % res.length;
  21.  
  22.     BigInteger[][] dp = new BigInteger[2][49];
  23.     BigInteger[][] count = new BigInteger[2][49];
  24.     for (int i = 0; i < 2; i++) {
  25.       Arrays.fill(dp[i], BigInteger.ZERO);
  26.       Arrays.fill(count[i], BigInteger.ZERO);
  27.     }
  28.    
  29.     int cur = 0, next = 1;
  30.     count[cur][0] = BigInteger.ONE;
  31.    
  32.     BigInteger ans = BigInteger.ZERO;
  33.     for (int i = 0; i < lim; i++) {
  34.       if (i % 1000 == 0) System.out.println(i+" "+ans.toString().length());
  35.      
  36.       Arrays.fill(dp[next], BigInteger.ZERO);
  37.       Arrays.fill(count[next], BigInteger.ZERO);
  38.      
  39.       for (int cc = 0; cc < 49; cc++) {
  40.         int k = cc/7, m = cc%7;
  41.         BigInteger x = dp[cur][cc].multiply(BigInteger.TEN);
  42.         for (int j = 0; j <= 9; j++) {
  43.           int nk = (k+res[bf]*j)%7, nm = (m+res[ff]*j)%7, nc = nk*7+nm;
  44.           dp[next][nc] = dp[next][nc].add(x);
  45.           count[next][nc] = count[next][nc].add(count[cur][cc]);
  46.           x = x.add(count[cur][cc]);
  47.         }
  48.       }
  49.       cur ^= 1; next ^= 1;
  50.       bf++; if (bf >= res.length) bf -= res.length;
  51.       ff--; if (ff < 0) ff += res.length;
  52.     }
  53.     return dp[cur][0];
  54.   }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement