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;
    }
  • 相关阅读:
    Section 3.1 Shaping Regions
    3D@OpenSource
    查找资料
    Section 3.1 Shaping Regions Again
    USACO Contact IOI’98 TLE
    事项ON丰宁坝上草原
    四叉树@POJ1610 Quad Trees
    在TabCtrl上放View@MFC
    CUGB的一场周赛
    贴图程序进展
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4371851.html
Copyright © 2011-2022 走看看