Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. /**
  2.  * Author           : Dipu Kumar Mohanto (Dip)
  3.  *                    CSE, BRUR.
  4.  *                    Batch - 6
  5.  *
  6.  * Problem          : DCP-103: Make Sixovisible
  7.  * Algorithm        : Dynamic Programing
  8.  * Complexity       :
  9.  * Category         : Non Classical DP
  10.  *
  11.  * Difficulty       : Meddium
  12.  *
  13.  * Source           : Dev Skill Online Judge
  14.  *
  15.  * Verdict          : Accepted
  16.  *
  17.  * Date             : 26-07-2017
  18.  * E-mail           : dipukumarmohanto1@gmail.com
  19.  *
  20.  * Note             : Accepted holeo kichu question theke jay
  21. **/
  22.  
  23.  
  24. #include <bits/stdc++.h>
  25.  
  26. using namespace std;
  27.  
  28.  
  29. #define  memo(a, b)             memset(a, b, sizeof(a))
  30. #define newLine                 "\n"
  31.  
  32. #define ll                      long long
  33.  
  34.  
  35. ll dp[510][450+5][2][6];   // dp[ position ][ sum of digit ][ digit taken or not ][ mod value ]
  36.  
  37. // jodi number er sob digit(500) jog kora hoy, tahole mot jogfol 4500
  38. // tahole [ sum of digit ] ghore 4500+ howa uchit kina?
  39. // ota dile memory limit / time limit khay
  40. // r eta dilei accepted kemne?
  41.  
  42. int maxSum;
  43. int tDigit;
  44.  
  45. string number;
  46.  
  47. bool solve(int pos=0, int sum=0, bool digitTaken=0, int mod=0)//, ll num=0)
  48. {
  49.     if (digitTaken && mod==0)
  50.     {
  51.         maxSum = max(maxSum, sum);
  52.     }
  53.  
  54.     if (pos >= tDigit)
  55.         return 0;
  56.  
  57.     ll &ret = dp[pos][sum][digitTaken][mod];
  58.  
  59.     if (ret != -1)
  60.         return ret;
  61.  
  62.     ll ret1 = solve(pos+1, sum+(number[pos]-'0'), digitTaken|1, (mod*10+(number[pos]-'0'))%6);//, num*10+(number[pos]-'0'));
  63.     ll ret2 = solve(pos+1, sum, digitTaken|0, mod);//, num);
  64.  
  65.     return ret = ret1 | ret2;
  66. }
  67.  
  68. void FAST();
  69. void TIME(double cl);
  70.  
  71. int main()
  72. {
  73.     //freopen("in.txt", "r", stdin);
  74.  
  75.     FAST();
  76.     double cl = clock();
  77.  
  78.     int tc;
  79.     cin >> tc;
  80.  
  81.     for (int tcase=1; tcase<=tc; tcase++)
  82.     {
  83.         cin >> number;
  84.  
  85.         tDigit = number.length();
  86.  
  87.         maxSum =  -1;
  88.         memo(dp, -1);
  89.  
  90.         bool ans = solve();
  91.  
  92.         cout << "Case " << tcase << ": ";
  93.  
  94.         if (maxSum==-1)
  95.             cout << "Impossible" << newLine;
  96.  
  97.         else
  98.             cout << maxSum << newLine;
  99.     }
  100.     TIME(cl);
  101. }
  102.  
  103.  
  104.  
  105. void FAST()
  106. {
  107.     ios::sync_with_stdio(0);
  108.     cin.tie(0);
  109. }
  110.  
  111. void TIME(double cl)
  112. {
  113.     cl = clock() - cl;
  114.  
  115.     cerr << newLine << newLine << newLine
  116.          << "------------------------------------------" << newLine
  117.          << "Total Execution Time = " << fixed << setprecision(8)
  118.          << (cl / CLOCKS_PER_SEC) << newLine
  119.          << "------------------------------------------" << newLine;
  120. }
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. /*****************************
  135.          Input  
  136. 5
  137. 124
  138. 3552
  139. 99912137
  140. 82113
  141. 10
  142.  
  143.          Output
  144. Case 1: 6
  145. Case 2: 15
  146. Case 3: 30
  147. Case 4: Impossible
  148. Case 5: 0
  149.  
  150. *******************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement