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

题意:

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

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

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
struct node{
ll v,st;
int id,y;
}b[],c[];
int tot1,tot2;
int n,K,flag,a[],d[];
ll S,bin[],jc[],ans;
bool cmp(node a,node b){
return a.v<b.v;
}
ll read(){
ll t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
int lowbit(int x){
return (x)&(-x);
}
bool pd(ll st1,ll st2){
int cnt=;
while (st1){
ll t=st1%;
if (t==) cnt++;
st1/=;
}
while (st2){
ll t=st2%;
if (t==) cnt++;
st2/=;
}
return cnt<=K;
}
void dfs(int k,ll st,ll res,int cnt,int lim){
if (k>lim){
if (flag==){
tot1++;
b[tot1].v=res;
b[tot1].st=st;
b[tot1].id=;
b[tot1].y=cnt;
}else{
tot2++;
c[tot2].v=res;
c[tot2].st=st;
c[tot2].id=;
c[tot2].y=cnt;
}
return;
}
dfs(k+,st+bin[k-]*,res,cnt,lim);
dfs(k+,st+bin[k-]*,res+a[k],cnt,lim);
if (a[k]<=&&res+jc[a[k]]<=S&&cnt+<=K)
dfs(k+,st+bin[k-]*,res+jc[a[k]],cnt+,lim);
}
int num(ll x){
int cnt=;
while (x){
ll t=x%;
if (t==) cnt++;
x/=;
}
return cnt;
}
void up(int x,int v){
for (int i=x;i<=K;i++)
d[i]+=v;
}
int ask(int x){
return d[x];
}
int main(){
n=read();K=read();S=read();
bin[]=;
for (int i=;i<=;i++)
bin[i]=bin[i-]*;
jc[]=;
for (int i=;i<=;i++)
jc[i]=jc[i-]*i;
for (int i=;i<=n;i++)
a[i]=read();
flag=;
dfs(,,,,(n+)>>);
flag=;
dfs(((n+)>>)+,,,,n);
std::sort(b+,b++tot1,cmp);
std::sort(c+,c++tot2,cmp);
int j=tot2;
for (int i=;i<=tot1&&j;){
for (;j&&b[i].v+c[j].v>S;j--);
if (b[i].v+c[j].v==S){
for (int y=;y<=K;y++) d[y]=;
for (;i<=tot1&&b[i].v+c[j].v==S;i++) d[b[i].y]++;
for (int y=;y<=K;y++) d[y]+=d[y-];
for (;j&&b[i-].v+c[j].v==S;j--) ans+=d[K-c[j].y];
}else i++;
}
printf("%I64d\n",ans);
return ;
}

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. Delphi 调试Dll报错 通过GetLastError显示错误信息。

    LibHandle := LoadLibrary('c:\windows\system32\SpcClass.dll');  ShowMessage(SysErrorMessage(GetLastEr ...

  2. spring注解理解

    步骤一:编写web.xml文件,主要代码如下:<servlet> Java代码 <servlet-name>spmvc</servlet-name> <ser ...

  3. JAVA的四种引用,强弱软虚用到的场景

    1.强引用 最常用的引用类型,如Object object = new Object(),只要强引用存在,GC必定 不回收,即使当前内存空间不足,jAVA虚拟机宁愿抛出OutofMemoryError ...

  4. jQuery效果-滑动

    index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> &l ...

  5. Python偏函数实例

    目标: 1.编写一个gui,生成按钮 2.通过偏函数,生成按钮 3.通过装饰器,实现按钮输出信息功能 1.使用Tkinter,创建一个按钮 代码如下: handetiandeMacBook-Pro:~ ...

  6. Java 实现Md5算法

    package other; import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/* * ...

  7. (转)Linux bash shell脚本语法入门

    http://www.linuxsky.org/doc/newbie/201004/389.html 1.基础 #!/bin/bash //bash脚本第一句都是这个,他会让系统指定以bash来解释这 ...

  8. 【MySQL】关于MySQL错误日志信息的收集

    为方便维护MySQL,写了个脚本用以提供收集错误信息的接口.这些错误信息来自与MySQL错误日志,而 通过grep mysql可以获取error-log的路径. #!/usr/bin/env pyth ...

  9. MVC框架个人浅析

    1.概述: 相信不少前端从业者,都会遇到MVC编程模式,现今多数轻量级网站用php作为后台交互,MVC编程模式用于JAVAWEB开发,应用类型以系统占多数(包括并不限于系统,电商网站,平台交互当然都能 ...

  10. Vi操作技巧

    Vi操作技巧: :nu    显示当前所在行的行号 :set nu    显示全部行号 :set nonu        取消显示行号 /字符串    查询字符串,按n查询下一个,按N查询上一个 持续 ...