fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. char vt[100][10];
  4. vector<char>res;
  5. char str[]={'+','-','*'};
  6. int c=0;
  7. bool flag[5];
  8. bool big=false;
  9. void all(int pos)
  10. {
  11. if(res.size()==4)
  12. {
  13. for (int i=0;i<4 ;i++ )
  14. {
  15. vt[c][i]=res[i];
  16. // printf("%c",res[i]);
  17. }
  18. c++;
  19. // printf("\n");
  20. return;
  21. }
  22. for (int i=0;i<3 ;i++ )
  23. {
  24. res.push_back(str[i]);
  25. all(i);
  26. res.pop_back();
  27. }
  28. }
  29. int ara[54];
  30. vector<int>ans;
  31. int hisab(int a,char ch,int b)
  32. {
  33. if(ch=='+')
  34. return a+b;
  35. else if(ch=='-')
  36. return a-b;
  37. return a*b;
  38. }
  39. bool check(vector<int>temp)
  40. {
  41. int a=temp[0],b=temp[1],c=temp[2],d=temp[3],e=temp[4];
  42. for (int i=0;i<81 ;i++ )
  43. {
  44. if(hisab(hisab(hisab(hisab(a,vt[i][0],b),vt[i][1],c),vt[i][2],d),vt[i][3],e)==23)
  45. return true;
  46. }
  47. return false;
  48. }
  49. void per()
  50. {
  51. if(ans.size()==5)
  52. {
  53. // for (int i=0;i<5 ;i++ )
  54. // {
  55. // cout << ans[i] << ' ';
  56. // }
  57. // cout << '\n';
  58. if(check(ans))
  59. big=true;
  60. return;
  61. }
  62. for (int i=0;i<5 ;i++ )
  63. {
  64. if(!flag[i])
  65. {
  66. flag[i]=true;
  67. ans.push_back(ara[i]);
  68. if(big)return;
  69. per();
  70. ans.pop_back();
  71. flag[i]=false;
  72. }
  73. }
  74. }
  75. int main()
  76. {
  77. all(0);
  78. while(1)
  79. {
  80. res.clear();
  81. ans.clear();
  82. big=false;
  83. for (int i=0;i<5 ;i++ )
  84. {
  85. scanf("%d",&ara[i]);
  86. flag[i]=0;
  87. }
  88. if(ara[0]==0 and ara[1]==0 and ara[2]==0 and ara[3]==0 and ara[4]==0)
  89. return 0;
  90. big=false;
  91. per();
  92. if(!big)
  93. printf("Impossible\n");
  94. else printf("Possible\n");
  95. }
  96. return 0;
  97. }
  98.  
Success #stdin #stdout 0s 3476KB
stdin
42 8 2 32 37
10 43 21 46 5
15 42 12 4 19
44 2 27 30 29
10 20 20 2 36
28 3 34 42 2
22 6 6 5 37
34 3 31 18 12
25 29 9 13 15
25 46 28 13 2
12 4 19 2 50
1 12 2 1 49
48 48 42 2 11
42 5 3 4 43
1 2 43 26 33
0 0 0 0 0
stdout
Possible
Possible
Possible
Impossible
Impossible
Impossible
Impossible
Impossible
Possible
Impossible
Impossible
Impossible
Impossible
Possible
Impossible