http://codeforces.com/contest/525/problem/E

题意:

有n个方块,上面写着一些自然数,还有k个感叹号可用。k<=n 
你可以选任意个方块,然后选一些贴上感叹号使上面的数值变成阶乘,然后把方块上的值加起来会得到一个和。 
求和等于S的选择方法数。

思路:折半搜索,然后把所有状态按权值排序,然后统计

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<algorithm>
  4. #include<cstring>
  5. #include<iostream>
  6. #define ll long long
  7. struct node{
  8. ll v,st;
  9. int id,y;
  10. }b[],c[];
  11. int tot1,tot2;
  12. int n,K,flag,a[],d[];
  13. ll S,bin[],jc[],ans;
  14. bool cmp(node a,node b){
  15. return a.v<b.v;
  16. }
  17. ll read(){
  18. ll t=,f=;char ch=getchar();
  19. while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
  20. while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
  21. return t*f;
  22. }
  23. int lowbit(int x){
  24. return (x)&(-x);
  25. }
  26. bool pd(ll st1,ll st2){
  27. int cnt=;
  28. while (st1){
  29. ll t=st1%;
  30. if (t==) cnt++;
  31. st1/=;
  32. }
  33. while (st2){
  34. ll t=st2%;
  35. if (t==) cnt++;
  36. st2/=;
  37. }
  38. return cnt<=K;
  39. }
  40. void dfs(int k,ll st,ll res,int cnt,int lim){
  41. if (k>lim){
  42. if (flag==){
  43. tot1++;
  44. b[tot1].v=res;
  45. b[tot1].st=st;
  46. b[tot1].id=;
  47. b[tot1].y=cnt;
  48. }else{
  49. tot2++;
  50. c[tot2].v=res;
  51. c[tot2].st=st;
  52. c[tot2].id=;
  53. c[tot2].y=cnt;
  54. }
  55. return;
  56. }
  57. dfs(k+,st+bin[k-]*,res,cnt,lim);
  58. dfs(k+,st+bin[k-]*,res+a[k],cnt,lim);
  59. if (a[k]<=&&res+jc[a[k]]<=S&&cnt+<=K)
  60. dfs(k+,st+bin[k-]*,res+jc[a[k]],cnt+,lim);
  61. }
  62. int num(ll x){
  63. int cnt=;
  64. while (x){
  65. ll t=x%;
  66. if (t==) cnt++;
  67. x/=;
  68. }
  69. return cnt;
  70. }
  71. void up(int x,int v){
  72. for (int i=x;i<=K;i++)
  73. d[i]+=v;
  74. }
  75. int ask(int x){
  76. return d[x];
  77. }
  78. int main(){
  79. n=read();K=read();S=read();
  80. bin[]=;
  81. for (int i=;i<=;i++)
  82. bin[i]=bin[i-]*;
  83. jc[]=;
  84. for (int i=;i<=;i++)
  85. jc[i]=jc[i-]*i;
  86. for (int i=;i<=n;i++)
  87. a[i]=read();
  88. flag=;
  89. dfs(,,,,(n+)>>);
  90. flag=;
  91. dfs(((n+)>>)+,,,,n);
  92. std::sort(b+,b++tot1,cmp);
  93. std::sort(c+,c++tot2,cmp);
  94. int j=tot2;
  95. for (int i=;i<=tot1&&j;){
  96. for (;j&&b[i].v+c[j].v>S;j--);
  97. if (b[i].v+c[j].v==S){
  98. for (int y=;y<=K;y++) d[y]=;
  99. for (;i<=tot1&&b[i].v+c[j].v==S;i++) d[b[i].y]++;
  100. for (int y=;y<=K;y++) d[y]+=d[y-];
  101. for (;j&&b[i-].v+c[j].v==S;j--) ans+=d[K-c[j].y];
  102. }else i++;
  103. }
  104. printf("%I64d\n",ans);
  105. return ;
  106. }

Codeforces 525E Anya and Cubes的更多相关文章

  1. Codeforces 525E Anya and Cubes 中途相遇法

    题目链接:点击打开链接 题意: 给定n个数.k个感叹号,常数S 以下给出这n个数. 目标: 随意给当中一些数变成阶乘.至多变k个. 再随意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制 ...

  2. codeforces E - Anya and Cubes 分块处理 暴力搜索

    说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方 ...

  3. Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索

    Codeforces Round #297 (Div. 2)E. Anya and Cubes Time Limit: 2 Sec  Memory Limit: 512 MBSubmit: xxx  ...

  4. Anya and Cubes CodeForces - 525E (双端搜索)

    大意: 给定$n$元素序列$a$, 可以任选不超过$k$个$a_i$变换为$a_i!$, 求变换后任选若干元素和为S的方案数. 分成两块暴搜, 复杂度$O(3^{\frac{n}{2}})$ #inc ...

  5. [Codeforces Round #297 Div. 2] E. Anya and Cubes

    http://codeforces.com/contest/525/problem/E 学习了传说中的折半DFS/双向DFS 先搜前一半数,记录结果,然后再搜后一半数,匹配之前结果. #include ...

  6. Codeforces525E Anya and Cubes(双向搜索)

    题目 Source http://codeforces.com/contest/525/problem/E Description Anya loves to fold and stick. Toda ...

  7. CodeForces 508C Anya and Ghosts

     Anya and Ghosts Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  8. Anya and Cubes 搜索+map映射

    Anya loves to fold and stick. Today she decided to do just that. Anya has n cubes lying in a line an ...

  9. codeforces 518C. Anya and Smartphone

    C. Anya and Smartphone time limit per test 1 second memory limit per test 256 megabytes input standa ...

随机推荐

  1. cf435C Cardiogram

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. HDU-2059龟兔赛跑(基础方程DP-遍历之前的所有状态)

    Problem Description 据说在很久很久以前,可怜的兔子经历了人生中最大的打击——赛跑输给乌龟后,心中郁闷,发誓要报仇雪恨,于是躲进了杭州下沙某农业园卧薪尝胆潜心修炼,终于练成了绝技,能 ...

  3. Spring+Mybatis+mysql配置

    mybatis的映射文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper SY ...

  4. jquery $.each()用法

    今天看到一个新的each玩法即each作为jquery的函数(平时用的大概都是用的each方法)使用: $.each([ 52, 97 ], function( index, value ) { al ...

  5. android后台截屏实现(3)--编译screencap

    修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...

  6. POJ 3356 AGTC(最小编辑距离)

    POJ 3356 AGTC(最小编辑距离) http://poj.org/problem?id=3356 题意: 给出两个字符串x 与 y,当中x的长度为n,y的长度为m,而且m>=n.然后y能 ...

  7. Android 字体设置

    Android 对中文字体支持很不好~~ 需要加入相应的字体库 (1)创建布局Layout //创建线性布局 LinearLayout linearLayout=newLinearLayout(thi ...

  8. Oracle11g新特性之动态变量窥视

    1. 11g之前的绑定变量窥视     我们都知道,为了可以让SQL语句共享运行计划,oracle始终都是强调在进行应用系统的设计时,必须使用绑定变量,也就是用一个变量来取代原来出如今SQL语句里的字 ...

  9. dsadm-dsconf数据导入导出

    cd instance-path/ds6/bin   #注意黄色参数修改为跟实际环境一致 -c,--accept-cert Does not ask for confirmation before a ...

  10. C复习手记(Day1)

    auto存储类:所有局部变量默认的存储类  ex:{int mount;auto int month}  auto只用在函数内,只做局部变量 register 存储类:register 存储类用于定义 ...