fork download
  1. #include <bits/stdc++.h>
  2. #define lb lower_bound
  3. #define pii pair<int,int>
  4. #define fi first
  5. #define int long long
  6. #define se second
  7. #define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  8. #define op freopen
  9. #define sz size
  10. #define TXT "test"
  11. #define freo if(fopen(TXT".inp","r")){op(TXT".inp","r",stdin);op(TXT".out","w",stdout);}
  12.  
  13. using namespace std;
  14. const int INF=1e18;
  15. int n,a[50],k,dp[1<<18][20];
  16. main()
  17. {
  18. ios;
  19. freo;
  20. cin>>n>>k;
  21. for(int i=1;i<=n;i++)
  22. {
  23. cin>>a[i];
  24. }
  25. for(int i=1;i<=n;i++)
  26. {
  27. dp[1<<(i-1)][i]=1;
  28. }
  29. for(int mask=1;mask<(1<<n);mask++)
  30. {
  31. for(int last=1;last<=n;last++)
  32. {
  33. if(!(mask&(1<<(last-1))))
  34. continue;
  35. for(int next=1;next<=n;next++)
  36. {
  37. if(mask&(1<<(next-1)))
  38. continue;
  39. if(abs(a[next]-a[last])<=k)
  40. continue;
  41. int nextper=mask|(1<<(next-1));
  42. dp[nextper][next]+=dp[mask][last];
  43. }
  44. }
  45. }
  46. int res=0;
  47. for(int i=1;i<=n;i++)
  48. {
  49. res+=dp[(1<<n)-1][i];
  50. }
  51. cout<<res;
  52. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty