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.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
const int MAXN=200000+10;
map<LL,int>mp[30];
int a[30], m, k, n;
LL fac[30], ans, s;
void init()
{
        int i;
        fac[1]=1;
        for(i=2; i<=18; i++) {
                fac[i]=fac[i-1]*i;
        }
}
void dfs(int cur, LL tmp, int step)
{
        mp[step][tmp]++;
        for(int i=cur+1; i<m; i++) {
                dfs(i,tmp+a[i],step);
                if(a[i]<=18&&step+1<=k)
                        dfs(i,tmp+fac[a[i]],step+1);
        }
}
void dfs1(int cur, LL tmp, int step)
{
        for(int i=0;i<=k-step;i++){
                if(mp[i].find(s-tmp)!=mp[i].end())
                        ans+=mp[i][s-tmp];
        }
        for(int i=cur+1; i<n; i++) {
                dfs1(i,tmp+a[i],step);
                if(a[i]<=18&&step+1<=k)
                        dfs1(i,tmp+fac[a[i]],step+1);
        }
}
int main()
{
        int i;
        init();
        while(scanf("%d%d%I64d",&n,&k,&s)!=EOF) {
                ans=0;
                m=n/2;
                for(i=0; i<n; i++) {
                        scanf("%d",&a[i]);
                }
                for(i=0;i<=k;i++) mp[i].clear();
                mp[0][0]=1;
                for(i=0; i<m; i++) {
                        dfs(i,a[i],0);
                        if(a[i]<=18)
                                dfs(i,fac[a[i]],1);
                }
                for(i=0; i<=k; i++) {
                        if(mp[i].find(s)!=mp[i].end())
                                ans+=mp[i][s];
                }
                for(i=m; i<n; i++) {
                        dfs1(i,a[i],0);
                        if(a[i]<=18)
                                dfs1(i,fac[a[i]],1);
                }
                printf("%I64d\n",ans);
        }
        return 0;
}
时间: 2024-08-03 11:14:25

Codeforces Round #297 (Div. 2) E题. 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) D题. Arthur and Walls(BFS)

题目地址:Arthur and Walls 这题有一个脑洞,对于当前的点(i,j)并且此点为"*"来说,若存在包含它的2*2正方形中除了它自己外,另外三个点都是".",那么这个点就必须要变成".".由于去掉这个点之后会对周围的8个点造成影响,所以可以用BFS去搜.WA第12组的应该是只考虑了会影响到周围的4个点了. 代码如下: #include <iostream> #include <string.h> #include

Codeforces 525E Anya and Cubes 中途相遇法

题目链接:点击打开链接 题意: 给定n个数,k个感叹号,常数S 下面给出这n个数. 目标: 任意给其中一些数变成阶乘,至多变k个. 再任意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制状压,中途查找. #include <stdio.h> #include <vector> #include <algorithm> #include <iostream> #include <cmath> #include <map>

模拟 Codeforces Round #297 (Div. 2) A. Vitaliy and Pie

题目传送门 1 /* 2 模拟:这就是一道模拟水题,看到标签是贪心,还以为错了呢 3 题目倒是很长:) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 using namespace std; 11 12 const int MAXN = 2e5 + 10; 13

Codeforces Round #257 (Div. 2) E题:Jzzhu and Apples 模拟

E. Jzzhu and Apples time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Jzzhu has picked n apples from his big apple tree. All the apples are numbered from 1 to n. Now he wants to sell them to

BFS Codeforces Round #297 (Div. 2) D. Arthur and Walls

题目传送门 1 /* 2 题意:问最少替换'*'为'.',使得'.'连通的都是矩形 3 BFS:搜索想法很奇妙,先把'.'的入队,然后对于每个'.'八个方向寻找 4 在2*2的方格里,若只有一个是'*',那么它一定要被替换掉 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <queue> 1

贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

题目传送门 1 /* 2 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 3 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 typedef long long ll; 12 13 co

Codeforces Round #243 (Div. 1) A题

http://codeforces.com/contest/425/problem/A 题目链接: 然后拿出这道题目是很多人不会分析题目,被题目吓坏了,其中包括我自己,想出复杂度,一下就出了啊!真是弱! 直接暴力求出矩阵数值,然后枚举每一个[I,J];再O[N]判断,分配好在[I,J]区间的数和之内的数,再排序下SOLO了 CODE:#include <cstdio> #include <cstring>#include <queue>#include <vect

Codeforces Round #634 (Div. 3) 补题

A. Candies and Two Sisters 签到题,直接输出即可 代码 #include<bits/stdc++.h> #define INF 0x3f3f3f3f typedef long long ll; using namespace std; inline void read(int &p) { p=0;int flag=1;char c=getchar(); while(!isdigit(c)) {if(c=='-') flag=-1;c=getchar();} w