Codeforces 525E Anya and Cubes 中途相遇法

题目链接:点击打开链接

题意:

给定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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-03 19:40:29

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

codeforces 525 E Anya and Cubes 中途相遇法

codeforces 525 E Anya and Cubes 中途相遇法 题意: 给出n个数a1,a2,...,an,要求从中选出一些数,可以把其中最多k个变成它自己的阶乘,然后选出的数求和,问最后和等于s的选法有多少种. 限制: 1 <= n <= 25; 0 <= k <= n; 1<= s <= 1e16; 1 <= ai <= 1e9 思路: 一般数据量20~30都会考虑中途相遇法,就是折半暴力. ps:用三进制暴力会比直接深搜多一个常数10,因为

Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)

题目地址:Anya and Cubes 比赛的时候居然没想起中途相遇法...这题也是属于想起来就很简单系列. 中途相遇法也叫折半搜索.就是处理前一半,把结果储存起来,再处理后一半,然后匹配前一半存储的结果. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib

Codeforces 525E Anya and Cubes

题意:给你n(25)个数,任选几个数 ,你最多可以对任选的几个数中的 K个数进行操作,操作是将 这个数变为它的阶乘,你选出来的数经过操作以后 等于 s的种类数有多少. 解题思路:看到这题的时候没什么思路,想想水一发dp  ,dp[i][j][k]<map>,就想到了这个四维的DP,但是显然这是会挂掉的.因为情况太多了,最多有 3^n种. 那么需要是什么方法呢.那就是折半了,因为我们只需要找到一个值, 而全部DP的话会产生太多的值了,而这大部分值都不是我们需要的.所以就把n个数分成两堆去处理,然

Codeforces 31E TV Game 中途相遇法 状压dp

题目链接:点击打开链接 题意: 给定2*n长的数字. 把这个数字拆成2个长度为n的数字,且相对位置不变.使得拆后得到的2个数字的和最大. 输出一个方案. 显然是中途相遇法,先计算左半段,再计算右半段 分别状压左半段和右半段,注意左半段状压后要在末尾补上0. 代码估计哪里有小越界==,数组开大了一点才过..具体就不查了. #include<iostream> #include<stdio.h> #include<string.h> #include<string&g

【中途相遇法】【STL】BAPC2014 K Key to Knowledge

题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11674&courseid=0 题目大意: N个学生M道题(1<=N<=12,1<=M<=30),每道题只有正误两种选项(0 1),每个学生的答题情况和正确题数已知,求标准答案可能有多少种. 如果标准答案只有一种则输出标准答案,否则输出解的个数. 题目思路: [

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

uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 di?erent characters (Mage, Tank, Fighter,Assassin and Marksman). A team consists of 5 players (one from each kind) and the goal is to kill amonster with L life points. The

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

用中途相遇法的思想来解题.分别枚举两边,和直接暴力枚举四个数组比可以降低时间复杂度.可是我不会写...看了紫书作者刘汝佳老师的代码,真是太美了!简单明了,就像看吕钦下的棋一样.我就模仿的写了一下: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<map> #include<set>

POJ 1840 Eqs Hash + 中途相遇法

把等式分成两拨算用中途相遇法就好了. 不过要注意的是这里不能用map,会超时,要自己手写hash,我重载了[]操作符之后用起来和map差不多,很随意 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <qu

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

Difference Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 62    Accepted Submission(s): 19 Problem Description Little Ruins is playing a number game, first he chooses two positive integers y an