Advertisement
Guest User

PE546

a guest
Feb 7th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. typedef long long big;
  5. #include "10.txt"
  6.  
  7. #define M 49
  8. #define N 50
  9. #define K 10
  10. #define INPUT 100000000000000LL
  11. #define PRE 1000000
  12. #define MOD 1000000007
  13.  
  14. big (*precomputed)[N];
  15.  
  16. int main(int argc, char **argv) {
  17.  
  18.     precomputed = (big (*)[N])malloc(sizeof(big) * PRE * N);
  19.  
  20.     /* LOOP TO PRECOMPUTE VALUES */
  21.     //-- i: value of n that we will be computing
  22.     //-- j: index of function that we will be computing
  23.     // precompute values for all functions up until PRE
  24.     for (int i=0;i<PRE;i++) {
  25.         for (int j=0;j<N;j++) {
  26.             // initialize all funcs f(0) = 1
  27.             if (!i) {
  28.                 precomputed[i][j] = 1;
  29.             } else {
  30.                 // if this is the actual f(n)
  31.                 if (!j) {
  32.                     // the formula from the problem
  33.                     precomputed[i][j] = (precomputed[i-1][j] + precomputed[i/K][j]) % MOD;
  34.                 } else {
  35.                     precomputed[i][j] = (precomputed[i-1][j] + precomputed[i][j-1]) % MOD;
  36.                 }
  37.             }
  38.         }
  39.     }
  40.  
  41.  
  42.     /* ACTUALLY CALCULATE F(INPUT) */
  43.  
  44.     //-- this is the value that we are currently at. The expression is in terms of functions of this value
  45.     //-- once it goes down below PRE, we can evaluate
  46.     big current = INPUT;
  47.  
  48.     //-- this is the vector of coefficients of each function for the current value. So, at all times,
  49.     //-- f(INPUT) = multiply[0]*f(current) + multiply[1]*g(current), + ....
  50.     big multiply[N];
  51.  
  52.     multiply[0] = 1;
  53.     for (int i=1;i<N;i++) multiply[i] = 0;
  54.  
  55.     while (current >= PRE) {
  56.         // find the modulus
  57.         int mod = current % K;
  58.  
  59.         //-- this is the array of values in the form temp[M][j] * M(K*k+j). we are done when all entries are zero
  60.         big temp[M][K];
  61.         memset(temp,0,sizeof(temp));
  62.  
  63.         // move all values of "multiply" into this array as appropriate
  64.         for (int i=0;i<M;i++) {
  65.             temp[i][mod] = multiply[i];
  66.         }
  67.  
  68.         // clear out the "multiply" array, it will be repopulated
  69.         memset(multiply,0,sizeof(multiply));
  70.  
  71.         bool morechange = true;
  72.         //-- we will bail out as soon as the "temp" array is all 0s
  73.         while (morechange) {
  74.  
  75.             morechange = false;
  76.  
  77.             // iterate thorugh all functions
  78.             for (int i=0;i<M;i++) {
  79.  
  80.                 // iterate through all remainders
  81.                 for (int j=0;j<K;j++) {
  82.  
  83.                     if (!temp[i][j]) continue; // there is nothing to do here
  84.                     morechange = true;
  85.  
  86.                     // if we are in function 0, we simply add the value to the output j times
  87.  
  88.                     if (!i) {
  89.                         multiply[0] = (multiply[0] + ((j * temp[i][j]) % MOD)) % MOD;
  90.  
  91.                     } else { // otherwise, we need to increment the value for the previous function
  92.                         for (int rem=1;rem<=j;rem++) {
  93.                             temp[i-1][rem] = (temp[i-1][rem] + temp[i][j]) % MOD;
  94.                         }
  95.                     }
  96.  
  97.  
  98.                     // and of course, use the coefficients table to map out lower-order coefficients
  99.                     for (int func=0;func<N;func++) multiply[func] = (multiply[func] + (coefficient[i][func] * temp[i][j] % MOD)) % MOD;
  100.  
  101.                     // this entry was used up fully
  102.                     temp[i][j] = 0;
  103.    
  104.                 }
  105.             }
  106.  
  107.         }
  108.  
  109.         current /= K;
  110.     }
  111.    
  112.     //-- now obtain the final sum using the lookup table
  113.     big answer = 0;
  114.     for (int i=0;i<N;i++) {
  115.         answer = (answer + (multiply[i] * precomputed[current][i] % MOD)) % MOD;
  116.     }
  117.  
  118.     std::cout << answer;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement