比赛 NOIP水题争霸赛 评测结果 RRRRRRRRRR
题目名称 博士的密码 最终得分 0
用户昵称 Matirx 运行时间 0.001 s
代码语言 C++ 内存使用 0.19 MiB
提交时间 2018-02-11 20:49:05
显示代码纯文本
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #define ll long long
  6. using namespace std;
  7. int n,cnt;
  8. ll key,sum;
  9. ll a[45];
  10. void dfs(ll,int);
  11. int main()
  12. {
  13. //freopen("password1.in","r",stdin);
  14. //freopen("password1.out","w",stdout);
  15. scanf("%d%lld",&n,&key);
  16. for(int i = 1; i <= n; i++){
  17. scanf("%lld",&a[i]);
  18. }
  19. dfs(1,1);
  20. dfs(1,0);
  21. printf("%d",cnt);
  22. return 0;
  23. }
  24.  
  25. void dfs(ll x,int y)
  26. {
  27. ll tmp = a[x] * y;
  28. ll m = sum + tmp;
  29. if(x > n) return;
  30. if(sum + tmp > key) return;
  31. for(int i = x; i <= n; i++) m += a[i];
  32. if(m < key) return;
  33. if(sum + tmp < key) {
  34. sum += tmp;
  35. dfs(x + 1,0);
  36. dfs(x + 1,1);
  37. sum -= tmp;
  38. }
  39. if(sum + tmp == key) {cnt++;return;}
  40. return;
  41. }