Advertisement
K_Y_M_bl_C

Untitled

Feb 5th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. long long n, a[15];
  2. int mask[15];
  3. int m;
  4. int if1;
  5. int ans[15];
  6. int cnt;
  7.  
  8. void gen(int len)
  9. {
  10.     if (len == m)
  11.     {
  12.         long long sum = 0;
  13.         int ccnt = 0;
  14.         for (int i = 0; i < m; ++i)
  15.         {
  16.             sum += a[i] * mask[i];
  17.             ccnt += mask[i];
  18.         }
  19.         if (sum == n)
  20.         {
  21.             if (ccnt < cnt)
  22.             {
  23.                 cnt = ccnt;
  24.                 for (int i = 0; i < m; ++i)
  25.                     ans[i] = mask[i];
  26.             }
  27.         }
  28.         if (sum > n)
  29.             if1 = 1;
  30.     }
  31.     else
  32.     {
  33.         for (int i = 0; i <= 2; ++i)
  34.         {
  35.             mask[len] = i;
  36.             gen(len + 1);
  37.         }
  38.     }
  39. }
  40.  
  41. int main()
  42. {
  43.     cnt = 31;
  44.     cin >> n >> m;
  45.     for (int i = 0; i < m; ++i)
  46.         cin >> a[i];
  47.     gen(0);
  48.     if (cnt == 31)
  49.     {
  50.         if (if1)
  51.             cout << 0;
  52.         else
  53.             cout << 1;
  54.     }
  55.     else
  56.     {
  57.         cout << cnt << "\n";
  58.         for (int i = 0; i < m; ++i)
  59.         {
  60.             for (int j = 0; j < ans[i]; ++j)
  61.                 cout << a[i] << " ";
  62.         }
  63.     }
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement