fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. long long C(int n, int k) {
  7. if (k > n / 2) k = n - k;
  8. long long res = 1;
  9. for (int i = 1; i <= k; ++i) res = res * (n - i + 1) / i;
  10. return res;
  11. }
  12.  
  13. int main() {
  14. ios_base::sync_with_stdio(0);
  15. cin.tie(0);
  16.  
  17. string line;
  18. while (getline(cin, line)) {
  19. string s;
  20. for (char c : line) {
  21. if (c != ' ' && c != '\t' && c != '\r') s += c;
  22. }
  23. if (s.empty()) continue;
  24.  
  25. int p = s.find('+'), r = s.find(')'), c = s.find('^');
  26. if (p == -1 || r == -1 || c == -1) continue;
  27.  
  28. string v1 = s.substr(1, p - 1);
  29. string v2 = s.substr(p + 1, r - p - 1);
  30. int n = stoi(s.substr(c + 1));
  31.  
  32. if (!n) {
  33. cout << "1\n";
  34. continue;
  35. }
  36.  
  37. for (int k = 0; k <= n; ++k) {
  38. long long v = C(n, k);
  39. if (v > 1) cout << v;
  40.  
  41. if (n - k) {
  42. cout << v1;
  43. if (n - k > 1) cout << "^" << n - k;
  44. }
  45. if (k) {
  46. cout << v2;
  47. if (k > 1) cout << "^" << k;
  48. }
  49.  
  50. if (k < n) cout << "+";
  51. }
  52. cout << "\n";
  53. }
  54. return 0;
  55. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty