zoukankan      html  css  js  c++  java
  • 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 MB
    Submit: xxx  Solved: 2xx

    题目连接

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

    Description

    Anya loves to fold and stick. Today she decided to do just that.

    Anya has n cubes lying in a line and numbered from 1 to n from left to right, with natural numbers written on them. She also has k stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes.

    Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.

    You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most k exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to S. Anya can stick at most one exclamation mark on each cube. Can you do it?

    Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.

    Input

    The first line of the input contains three space-separated integers n, k and S (1 ≤ n ≤ 25, 0 ≤ k ≤ n, 1 ≤ S ≤ 1016) — the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.

    The second line contains n positive integers ai (1 ≤ ai ≤ 109) — the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.

    Multiple cubes can contain the same numbers.

    Output

    Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number S.

    Sample Input

    Input
    2 2 30
    4 3
     
    Input
    2 2 7
    4 3
     
    Input
    3 1 1
    1 1 1

    Sample Output

    Output
    1
    Output
    1
    Output
    6

    HINT

    题意:

    给你n个数,k个魔法棒,s为所求的数,然后让你找有多少种方法,能够使的这n个数之和为s,其中一个魔法棒可以使的一个数变成他的阶乘。

    题解:

    折半搜索
    对于每一个数,都有三种策略,拿,不拿,使用魔法棒
    那么我们就直接折半搜索然后扔进一个map里面,然后就查询就好啦~
    13^3*12^3=3 796 416;
    复杂度算的刚刚好呀= =

    ~(≧▽≦)/~啦啦啦,讲完啦~

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    //const int inf=0x7fffffff;   //无限大
    const int inf=0x3f3f3f3f;
    /*
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    */
    //**************************************************************************************
    ll n,s,k;
    /*
    inline ll read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    */
    struct node
    {
        ll x;
        ll y;
    }dota[1111111];
    ll pl[40];
    ll kiss[110],mid,ans;
    ll cnt=0;
    map<ll,ll> mp[30];
    void dfs(ll a,ll b,ll c)
    {
        //cout<<a<<" "<<b<<" "<<c<<endl;
        if(b>k||c>s)
            return;
        if(a==mid+1)
        {
            //cout<<a<<" "<<b<<" "<<c<<endl;
            dota[++cnt].x=b,dota[cnt].y=c;
            return;
        }
        dfs(a+1,b,c);
        dfs(a+1,b,c+kiss[a]);
        if(kiss[a]<=19)
            dfs(a+1,b+1,c+pl[kiss[a]]);
    }
    void DFS(ll a,ll b,ll c)
    {
    
        if(b>k||c>s)
            return;
        if(a==n+1)
        {
            //cout<<a<<" "<<b<<" "<<c<<endl;
            mp[b][c]++;
            return;
        }
        DFS(a+1,b,c);
        DFS(a+1,b,c+kiss[a]);
        if(kiss[a]<=19)
            DFS(a+1,b+1,c+pl[kiss[a]]);
    }
    int main()
    {
        cin>>n>>k>>s;
        pl[0]=1;
        for(int i=1;i<19;i++)
            pl[i]=i*pl[i-1];
        for(int i=1;i<=n;i++)
            cin>>kiss[i];
        mid=n/2;
        dfs(1,0,0);
        DFS(mid+1,0,0);
        for(int i=1;i<=cnt;i++)
        {
            for(int j=0;j<=k-dota[i].x;j++)
            {
                ans+=mp[j][s-dota[i].y];
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    剑指offer:2.二维数组的查找(Java版)
    剑指offer:1.找出数组中重复的数(java版)
    Java自动内存管理机制学习(二):垃圾回收器与内存分配策略
    Java自动内存管理机制学习(一):Java内存区域与内存溢出异常
    Java并发编程学习:线程安全与锁优化
    Java并发编程学习:volatile关键字解析
    Java 8之重新认识HashMap
    【转】java内部类的作用分析
    不能进入String.class调试
    SCJP考试题310-025(第二套<4>)92-147/147
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4371851.html
Copyright © 2011-2022 走看看