title: Painting Fence Algorithm
signature: int paintingFenceAlgorithm(int n, int k) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(int n, int k, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(n, k), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver(6, 30, 725841870); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver(23, 87, 370622679); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver(89, 31, 7554776); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver(63, 36, 76740139); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver(23, 68, 647498520); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver(44, 66, 70208793); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver(81, 18, 227130467); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver(43, 73, 204618786); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver(9, 42, 797370673); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver(41, 98, 903959704); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
source_py: def painting_fence_algorithm(n, k):
      total = k
      mod = 1000000007
      (same, diff) = (0, k)
      for i in range(2, (n + 1)):
          same = diff
          diff = (total * (k - 1))
          diff = (diff % mod)
          total = ((same + diff) % mod)
      return total
source_cpp: long painting_fence_algorithm(int n, int k) {
    long total = k;
    int mod = 1000000007;
    int same = 0, diff = k;
    for ( int i = 2;
    i <= n;
    i ++ ) {
      same = diff;
      diff = total * ( k - 1 );
      diff = diff % mod;
      total = ( same + diff ) % mod;
    }
    return total;
  }