Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #define MAX 50001
- #define NAX 101
- int v[NAX];
- int mat[MAX][NAX] = {0};
- void zerar(int m, int n);
- int maior(int a, int b, int c);
- int moedas(int m, int n);
- int main()
- {
- int m, n, resul;
- while( 1 )
- {
- scanf("%d", &m);
- if(m == 0)
- {
- break;
- }
- scanf("%d", &n);
- for(int i = 0; i < n; i++)
- {
- scanf("%d", &v[i]);
- }
- resul = (-1) * moedas(m, n - 1);
- if(resul > m)
- {
- printf("Impossivel\n");
- }
- else
- {
- printf("%d\n", resul);
- }
- zerar(m, n);
- }
- }
- int maior(int a, int b, int c)
- {
- int maior;
- if(a > b)
- {
- maior = a;
- }
- else
- {
- maior = b;
- }
- if(c > maior)
- {
- maior = c;
- }
- return(maior);
- }
- int moedas(int m, int n)
- {
- if(n < 0)
- {
- return((-1) * MAX);
- }
- if(m == 0)
- {
- return(0);
- }
- mat[m][n-1] = moedas(m, n-1);
- if(v[n] <= m)
- {
- if(mat[m-v[n]][n] == 0)
- {
- mat[m-v[n]][n] = moedas(m - v[n], n);
- }
- if(mat[m - v[n]][n-1] == 0)
- {
- mat[m - v[n]][n-1] = moedas(m - v[n], n - 1);
- }
- if(mat[m][n] == 0)
- {
- mat[m][n] = maior(-1 + mat[m-v[n]][n] , -1 + mat[m - v[n]][n-1] , mat[m][n-1] );
- }
- return(mat[m][n]);
- }
- else
- {
- return(mat[m][n-1]);
- }
- }
- void zerar(int m, int n)
- {
- for(int i = 0; i <= m; i++)
- {
- for(int j = 0; j < n; j++)
- {
- mat[i][j] = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement