Advertisement
rjlth

Untitled

Oct 5th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. //Zhandos Kapezov
  2. #include <iostream>
  3. #include <math.h>
  4. #include <cmath>
  5. #include <vector>
  6. #include <utility>
  7. #include <algorithm>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <fstream>
  11. #include <string>
  12. #include <string.h>
  13. #include <sstream>
  14. #include <map>
  15. #include <set>
  16. #include <stack>
  17. #include <queue>
  18. #include <deque>
  19. #include <limits>
  20. #include <list>
  21. #include <functional>
  22. #include <bitset>
  23. #include <numeric>
  24. #include <iomanip>
  25. #include <ctime>
  26. #include <ctype.h>
  27.  
  28. using namespace std;
  29. typedef long long ll;
  30.  
  31. #define F first
  32. #define S second
  33. #define pb push_back
  34. #define mp make_pair
  35. #define sz size()
  36. #define sqr(x) ((x)*(x))
  37. #define INF numeric_limits<int>::max()
  38.  
  39. int bit(int x, int i) {
  40.     return (x>>i)&1;
  41. }
  42.  
  43. int main()
  44. {
  45.     #ifndef ONLINE_JUDGE
  46.         freopen("input.txt","rt",stdin);
  47.         freopen("output.txt","wt",stdout);
  48.     #endif
  49.     cin>>x>>n; res=-1; pp=1;
  50.     for (int i=0; i<n; i++) pp*=x;
  51.     for (int i=0; i<n; i++) cin>>a[i];
  52.  
  53.     for (int p=0; p<pp; p++) {
  54.         cur=p; now=0; j=0; nn=0;
  55.         while (cur) {
  56.             now+=a[j]*(cur%3);
  57.             nn+=cur%3;
  58.             cur/=3; j++;
  59.         }
  60.         if (now>x) res=0;
  61.         if (now==x) {
  62.             if (!res) res=nn; else
  63.             res=min(res, nn);
  64.         }
  65.     }
  66.     cout<<res;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement