Advertisement
KShah

Untitled

Nov 22nd, 2021
1,158
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <climits>
  3. #include <cassert>
  4.  
  5. void f(int** arr, int arg, long long& answer, int index, int* size_arr, int* ind) {
  6.     if (index == arg) {
  7.         for (int i = 0; i < arg; ++i) {
  8.             for (int j = i + 1; j < arg; ++j) {
  9.                 if (ind[i] == ind[j]) return;
  10.             }
  11.         }
  12.         long long res = 1;
  13.         for (int i = 0; i < arg; ++i) {
  14.             res *= arr[i][ind[i]];
  15.         }
  16.         answer += res;
  17.         return;
  18.     }
  19.  
  20.     for (int i = 0; i < size_arr[index]; ++i) {
  21.         ind[index] = i;
  22.         f(arr, arg, answer, index + 1, size_arr, ind);
  23.     }
  24. }
  25.  
  26. int main(int arg, char** argv) {
  27.     --arg;
  28.     int* size_arr = new int[arg];
  29.     for (int i = 1; i <= arg; i++) {
  30.         int temp_size = 0;
  31.         int j = 0;
  32.         while (argv[i][j] != '\0') {
  33.             temp_size *= 10;
  34.             temp_size += argv[i][j] - '0';
  35.             ++j;
  36.         }
  37.         size_arr[i - 1] = temp_size;
  38.     }
  39.     int** arr = new int*[arg];
  40.    
  41.     for (int i = 0; i < arg; i++) {
  42.         arr[i] = new int[size_arr[i]];
  43.         for (int j = 0; j < size_arr[i]; j++) {
  44.           std::cin >> arr[i][j];
  45.         }
  46.     }
  47.    
  48.     long long answer = 0;
  49.     int* ind = new int[arg];
  50.     f(arr, arg, answer, 0, size_arr, ind);
  51.    
  52.     std::cout << answer;
  53.  
  54.     delete[] size_arr;
  55.     for (int i = 0; i < arg; ++i) {
  56.         delete[] arr[i];
  57.     }
  58.     delete[] arr;
  59.     delete[] ind;
  60.     return 0;
  61. }
  62.  
  63.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement