Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author : Dipu Kumar Mohanto (Dip)
- * CSE, BRUR.
- * Batch - 6
- *
- * Problem : DCP-103: Make Sixovisible
- * Algorithm : Dynamic Programing
- * Complexity :
- * Category : Non Classical DP
- *
- * Difficulty : Meddium
- *
- * Source : Dev Skill Online Judge
- *
- * Verdict : Accepted
- *
- * Date : 26-07-2017
- * E-mail : dipukumarmohanto1@gmail.com
- *
- * Note : Accepted holeo kichu question theke jay
- **/
- #include <bits/stdc++.h>
- using namespace std;
- #define memo(a, b) memset(a, b, sizeof(a))
- #define newLine "\n"
- #define ll long long
- ll dp[510][450+5][2][6]; // dp[ position ][ sum of digit ][ digit taken or not ][ mod value ]
- // jodi number er sob digit(500) jog kora hoy, tahole mot jogfol 4500
- // tahole [ sum of digit ] ghore 4500+ howa uchit kina?
- // ota dile memory limit / time limit khay
- // r eta dilei accepted kemne?
- int maxSum;
- int tDigit;
- string number;
- bool solve(int pos=0, int sum=0, bool digitTaken=0, int mod=0)//, ll num=0)
- {
- if (digitTaken && mod==0)
- {
- maxSum = max(maxSum, sum);
- }
- if (pos >= tDigit)
- return 0;
- ll &ret = dp[pos][sum][digitTaken][mod];
- if (ret != -1)
- return ret;
- ll ret1 = solve(pos+1, sum+(number[pos]-'0'), digitTaken|1, (mod*10+(number[pos]-'0'))%6);//, num*10+(number[pos]-'0'));
- ll ret2 = solve(pos+1, sum, digitTaken|0, mod);//, num);
- return ret = ret1 | ret2;
- }
- void FAST();
- void TIME(double cl);
- int main()
- {
- //freopen("in.txt", "r", stdin);
- FAST();
- double cl = clock();
- int tc;
- cin >> tc;
- for (int tcase=1; tcase<=tc; tcase++)
- {
- cin >> number;
- tDigit = number.length();
- maxSum = -1;
- memo(dp, -1);
- bool ans = solve();
- cout << "Case " << tcase << ": ";
- if (maxSum==-1)
- cout << "Impossible" << newLine;
- else
- cout << maxSum << newLine;
- }
- TIME(cl);
- }
- void FAST()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- }
- void TIME(double cl)
- {
- cl = clock() - cl;
- cerr << newLine << newLine << newLine
- << "------------------------------------------" << newLine
- << "Total Execution Time = " << fixed << setprecision(8)
- << (cl / CLOCKS_PER_SEC) << newLine
- << "------------------------------------------" << newLine;
- }
- /*****************************
- Input
- 5
- 124
- 3552
- 99912137
- 82113
- 10
- Output
- Case 1: 6
- Case 2: 15
- Case 3: 30
- Case 4: Impossible
- Case 5: 0
- *******************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement