Anupznk

offline 10

Jul 6th, 2021
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include <stdlib.h>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. #define MOD 1000000007
  7. int faces[101];
  8. long long table[101][10001];
  9.  
  10. long long diceThrow(int n, int sum) {
  11. // cumulative sum approach
  12.  
  13. table[0][0] = 1;
  14. for (int i = 1; i<=n; i++) {
  15. for (int j = i; j<= sum; j++) {
  16.  
  17. table[i-1][j] = (table[i-1][j-1] + table[i - 1][j])%MOD;
  18. // calculating cumulative sum of the previous row
  19. if (j - (faces[i] + 1) >= 0){
  20. table[i][j] = table[i-1][j-1] - table[i-1][j - (faces[i] + 1)] + MOD;
  21. } else {
  22. table[i][j] = table[i-1][j-1];
  23. }
  24. table[i][j] %= MOD;
  25. }
  26. }
  27. return (table[n][sum])%MOD;
  28. }
  29.  
  30. int main() {
  31. int n, s;
  32. ifstream myStream ("test.txt", ios::in);
  33. if (!myStream.is_open()) {
  34. cout << "Error accessing the file" << endl;
  35. exit(1);
  36. }
  37. myStream >> n >> s;
  38.  
  39. for (int i = 1; i<=n; i++) {
  40. int face;
  41. myStream >> face;
  42. faces[i] = face;
  43. }
  44. myStream.close();
  45. cout << diceThrow(n, s);
  46. }
  47.  
RAW Paste Data