zoukankan      html  css  js  c++  java
  • codeforces 632E. Thief in a Shop fft

    题目链接

    E. Thief in a Shop
    time limit per test
    5 seconds
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    A thief made his way to a shop.

    As usual he has his lucky knapsack with him. The knapsack can contain k objects. There are n kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind i is ai.

    The thief is greedy, so he will take exactly k products (it's possible for some kinds to take several products of that kind).

    Find all the possible total costs of products the thief can nick into his knapsack.

    Input

    The first line contains two integers n and k (1 ≤ n, k ≤ 1000) — the number of kinds of products and the number of products the thief will take.

    The second line contains n integers ai (1 ≤ ai ≤ 1000) — the costs of products for kinds from 1 to n.

    Output

    Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order.

    Examples
    input
    3 2
    1 2 3
    output
    2 3 4 5 6
    input
    5 5
    1 1 1 1 1
    output
    5
    input
    3 3
    3 5 11
    output
    9 11 13 15 17 19 21 25 27 33


    如果给出n个数, 每个数为xi, 那么a[xi]++, 然后对a做k次fft就可以了。
    #include <iostream>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <set>
    #include <string>
    #include <queue>
    #include <stack>
    #include <bitset>
    using namespace std;
    #define pb(x) push_back(x)
    #define ll long long
    #define mk(x, y) make_pair(x, y)
    #define lson l, m, rt<<1
    #define mem(a) memset(a, 0, sizeof(a))
    #define rson m+1, r, rt<<1|1
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define rep(i, n, a) for(int i = a; i<n; i++)
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int mod = 1e9+7;
    const int inf = 1061109567;
    const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
    struct complex
    {
        double r,i;
        complex(double _r = 0.0,double _i = 0.0)
        {
            r = _r; i = _i;
        }
        complex operator +(const complex &b)
        {
            return complex(r+b.r,i+b.i);
        }
        complex operator -(const complex &b)
        {
            return complex(r-b.r,i-b.i);
        }
        complex operator *(const complex &b)
        {
            return complex(r*b.r-i*b.i,r*b.i+i*b.r);
        }
    };
    void change(complex y[],int len)
    {
        int i,j,k;
        for(i = 1, j = len/2;i < len-1; i++)
        {
            if(i < j)swap(y[i],y[j]);
            k = len/2;
            while( j >= k)
            {
                j -= k;
                k /= 2;
            }
            if(j < k) j += k;
        }
    }
    void fft(complex y[],int len,int on)
    {
        change(y,len);
        for(int h = 2; h <= len; h <<= 1)
        {
            complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
            for(int j = 0;j < len;j+=h)
            {
                complex w(1,0);
                for(int k = j;k < j+h/2;k++)
                {
                    complex u = y[k];
                    complex t = w*y[k+h/2];
                    y[k] = u+t;
                    y[k+h/2] = u-t;
                    w = w*wn;
                }
            }
        }
        if(on == -1)
            for(int i = 0;i < len;i++)
                y[i].r /= len;
    }
    const int maxn = 2e6+5;
    complex x1[maxn], x2[maxn];
    int a[maxn], b[maxn];
    void cal(int *a, int *b, int &lena, int &lenb) {
        int len = 1;
        while(len<lena+lenb)
            len<<=1;
        for(int i = 0; i<=lenb; i++) {
            x1[i] = complex(b[i], 0);
        }
        for(int i = lenb+1; i<len; i++)
            x1[i] = complex(0, 0);
        for(int i = 0; i<=lena; i++) {
            x2[i] = complex(a[i], 0);
        }
        for(int i = lena+1; i<len; i++)
            x2[i] = complex(0, 0);
        fft(x1, len, 1);
        fft(x2, len, 1);
        for(int i = 0; i<len; i++)
            x1[i] = x1[i]*x2[i];
        fft(x1, len, -1);
        for(int i = 0; i<=lena+lenb; i++)
            b[i] = (int)(x1[i].r+0.5);
        for(int i = 0; i<=lena+lenb; i++)
            if(b[i]>0)
                b[i] = 1;
        lenb += lena;
    }
    int main()
    {
        int n, k, x;
        cin>>n>>k;
        for(int i = 0; i<n; i++) {
            scanf("%d", &x);
            a[x]++;
        }
        b[0] = 1;
        int lena = 1000, lenb = 0;
        while(k) {
            if(k&1) {
                cal(a, b, lena, lenb);
            }
            if(k>1) {
                cal(a, a, lena, lena);
            }
            k>>=1;
        }
        for(int i = 0; i<=lena+lenb; i++) {
            if(b[i]) {
                printf("%d ", i);
            }
        }
        cout<<endl;
        return 0;
    }


  • 相关阅读:
    postgresql大批量数据导入方法
    Odoo 的库存管理与OpenERP之前的版本有了很大的不同,解读Odoo新的WMS模块中的新特性
    一招解决OpenERP8.0安装旧版模块报错
    window下python 扩展库安装 使用第三方镜像源
    OpenERP 安装在Windows server上时间显示不对的解决办法
    深入理解OpenERP的工作流(Workflow)
    Need to add a caption to a jpg, python can't find the image
    OE中admin的内置帐号
    [Linux] Git: 基本使用
    ubuntu下实现openerp 7使用nginx反正代理及绑定域名
  • 原文地址:https://www.cnblogs.com/yohaha/p/5234737.html
Copyright © 2011-2022 走看看