题目链接:点击打开链接

题意:

给定n个数。k个感叹号,常数S

以下给出这n个数。

目标:

随意给当中一些数变成阶乘。至多变k个。

再随意取一些数,使得这些数和恰好为S

问有多少方法。

思路:

三进制状压。中途查找。

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <string.h>
#include <string>
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c<'0' || c>'9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if (x <0) {
putchar('-');
x = -x;
}
if (x>9) pt(x / 10);
putchar(x % 10 + '0');
}
using namespace std;
typedef long long ll;
const double pi = acos(-1.);
const double e = 2.718281828459;
const ll ma = 1e8;
const int N = 2005;
int n, k;
ll a[30], m;
ll jie[1000], hehe;
ll cal(ll x){
if (x >= hehe)return -1;
return jie[x];
}
ll re[30];
map<ll, int>mp[2][30];
ll b[30], d[30], top;
ll y[30], t;
ll san[30];
void work(int x){
for (int i = 0; i < san[top]; i++)
{
int cnt = 0;
ll sum = 0;
int tmp = i, id = 0;
while (tmp){
if ((tmp % 3) == 1){
cnt++; sum += d[id];
if (d[id] < 0){ sum = m + 1; break; }
}
else if ((tmp % 3) == 2){
sum += b[id];
}
if (cnt >k || sum > m)break;
tmp /= 3; id++;
}
if (cnt <= k && sum <= m)mp[x][cnt][sum]++;
}
}
int main(){
while (cin >> n){
rd(k); rd(m);
san[0] = 1; for (int i = 1; i < 30; i++)san[i] = san[i - 1] * 3;
jie[1] = 1;
for (int i = 2;; i++){
jie[i] = jie[i - 1] * i;
if (m / jie[i] <= i){
hehe = i + 1; break;
}
}
for (int i = 0; i < n; i++){
rd(a[i]);
re[i] = cal(a[i]);
}
for (int i = 0; i < n / 2; i++){ b[i] = a[i]; d[i] = re[i]; }
top = n / 2;
work(0); for (int i = n / 2; i < n; i++){ b[i - n / 2] = a[i]; d[i - n / 2] = re[i]; }
top = n - n / 2;
work(1);
ll ans = 0;
for (int i = 0; i <= k; i++)
for (auto it : mp[0][i])
for (int j = 0; j + i <= k; j++)
if (mp[1][j].count(m - it.first))
ans += (ll)it.second * mp[1][j][m - it.first];
pt(ans);
}
return 0;
}

Codeforces 525E Anya and Cubes 中途相遇法的更多相关文章

  1. Codeforces 525E Anya and Cubes

    http://codeforces.com/contest/525/problem/E 题意: 有n个方块,上面写着一些自然数,还有k个感叹号可用.k<=n 你可以选任意个方块,然后选一些贴上感 ...

  2. Codeforces 1105E 最大独立集 状态DP 中途相遇法

    题意:你有一个字符串, 有两种操作,一种是改变字符串,一种是某个用户询问这个字符串,如果一个用户每次查询字符串的时候都是他的用户名,他就会高兴.问最多有多少个用户会高兴? 题意:容易发现,在两个1操作 ...

  3. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  4. uva 6757 Cup of Cowards(中途相遇法,貌似)

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  5. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  6. HDU 5936 Difference 【中途相遇法】(2016年中国大学生程序设计竞赛(杭州))

    Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. 高效算法——J 中途相遇法,求和

    ---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

  8. 【UVALive】2965 Jurassic Remains(中途相遇法)

    题目 传送门:QWQ 分析 太喵了~~~~~ 还有中途相遇法这种东西的. 嗯 以后可以优化一些暴力 详情左转蓝书P58 (但可能我OI生涯中都遇不到正解是这个的题把...... 代码 #include ...

  9. uva1152 - 4 Values whose Sum is 0(枚举,中途相遇法)

    用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度. 这里用到一个很实用的技巧: 求长度为n的有序数组a中的数k的个数num? num=upper_bound(a,a+ ...

随机推荐

  1. 杂项-Java:JNI

    ylbtech-杂项-Java:JNI JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++).从Java1.1开始, ...

  2. 3个不常用的HTML标签

    html标签众多,在HTML手册里你可以都查到.但有的HTML标签你可能从未使用过.不是因为你欠缺学习精神,而是它们确实用处不大.如果你有探索精神,那就接着往下看吧. 第一个:<abbr> ...

  3. Python 40 数据库-外键约束 、多对一与多对多的处理

    mysql提供了 foreign key,专门用于为表和表之间 建立物理关联 思考 表里存储的是一条条的记录,两个表之间能产生的关系有哪些? 现有 A B两张表 1.多对一         2.一对一 ...

  4. 网上干货 ElasticSearch详解与优化设计

    https://blog.csdn.net/joez/article/details/52171199?locationNum=3&fps=1 分析得很好,需要仔细阅读 问题遗留点: Fiel ...

  5. 错误:android.view.InflateException: Binary XML file line #167: Binary XML file line #167: Error inflating class <unknown>

    1:错误日志 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.8.activity.RecordActiv ...

  6. Android 4.0 Launcher2源码分析——主布局文件(转)

    本文来自http://blog.csdn.net/chenshaoyang0011 Android系统的一大特色是它拥有的桌面通知系统,不同于IOS的桌面管理,Android有一个桌面系统用于管理和展 ...

  7. ★Java面向对象(一)——————————基本概念

    package boll; /* 用Java语言对现实生活中的事物进行描述. 通过类的形式来体现, 怎么描述呢? 对于事物的描述通常只有两个方面,一个是属性,一个是行为. 只要明确该事物的行为和属性并 ...

  8. 【Hexo】本地local4000打不开解决方法

    错误:Cannot GET /spadesq.github.io/ (注:spadesq.github.io是原来放hexo文件夹的名字) 由于我后来把hexo文件夹搬迁到别处,但我发现打开本地,地址 ...

  9. BZOJ 3012: [Usaco2012 Dec]First! 字典树 + tarjan

    Code: #include<bits/stdc++.h> #define maxn 1000003 using namespace std; char str[maxn],strtot[ ...

  10. eas启动服务器时非法组件

    EAS实例启动报系统中存在非法组件,实例启动失败:   组件检查机制,要求除了 $EAS_HOME eas\server\lib: $EAS_HOME \eas\server\deploy\files ...