题目链接:点击打开链接

题意:

给定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. Codeforces--626B--Cards(模拟)

     Cards Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  2. [BZOJ 1718] Redundant Paths

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1718 [算法] 用Tarjan算法找出所有e-DCC(边-双联通分量),然后将这张图 ...

  3. bzoj 4481 [ Jsoi 2015 ] 非诚勿扰 —— 期望

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4481 太弱了这种题都要看半天TJ...:https://blog.csdn.net/chai ...

  4. 杂项-Java:JDBC

    ylbtech-杂项-Java:JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访 ...

  5. c++之——————各种变量

    对我们程序员来讲,“变量”和“对象”是可以相互互换使用的.-------------开篇之词. 变量:提供一个具有名字的可供程序操作的存储空间.由类型说明符和其后紧跟的数个列表组成,其中变量名之间使用 ...

  6. PCB WCF Web接口增减参数后,在客户端不更新的情况,是否影响客户端,评估测试

    1.目的:由于接口众多,服务端变更接口,会造成服务停用更新,造成客户端不能使用或报错, 在此评估[Web中心]此服务端,接口接口参数增加或减少,是否对客户端造成影响 2.评估内容:服务端增加单值参数, ...

  7. 【原创分析帖】据说Google内部有史以来最难的一道面试题

    逛技术平台的时候,刷到一道算法题,一眼看去,就被其开头吸引了: 摘自知乎某 Google 分布式大神的一道题,技术是Google内部出的有史以来最难的一道题 嗯,距离下班还有一段时间,就看看把. 题目 ...

  8. 猜拳游戏项目(涉及知识点Scanner、Random、For、数组、Break、Continue等)

    package day03.d3.xunhuankongzhi; import java.util.Scanner; public class CaiQuan { public static void ...

  9. 【JAVA练习】- 一个逻辑题

    打印 1 3    4 5   8    12 7   12   20   32 9    16  28    48   80  ..... 输入任意一个奇数,输出那一行的数据 第一种方法找到规律进行 ...

  10. vue.js $set的使用 数组

    [javascript] view plain copy <!DOCTYPE html> <html lang="en"> <head> < ...