Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include<math.h>
  3. #include <algorithm>
  4. #include<climits>
  5. using namespace std;
  6.  
  7. long long e = 1000000007;
  8.  
  9. int monet(int a[], int n, int s)
  10. {
  11.     int table[1000][100000], i, j, x, y;
  12.     for (i = 0; i<n; i++)
  13.         table[0][i] = 1;
  14.     for (int i = 1; i < s+1; i++)
  15.     {
  16.         for (int j = 0; j < n; j++)
  17.         {
  18.             if (i - a[j] >= 0)
  19.                 x = (table[i - a[j]][j]);
  20.             else
  21.                 x=0;
  22.             if (j >= 1)
  23.                 y = table[i][j - 1];
  24.             else
  25.                 y = 0;
  26.             table[i][j] = (x + y)%e;
  27.         }
  28.     }
  29.     return table[s][n - 1];
  30. }
  31.  
  32. int main()
  33. {
  34.     freopen("input.txt", "r", stdin);
  35.     freopen("output.txt", "w", stdout);
  36.  
  37.     int n, s, a[100000];
  38.     cin >> n >> s;
  39.     for (int i = 0; i < n; i++)
  40.         cin >> a[i];
  41.     cout << monet(a, n, s);
  42.     // идею решения подсмотрел на сайте
  43.     //http://www.geeksforgeeks.org/dynamic-programming-set-7-coin-change/
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement