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


  • 相关阅读:
    《.NET 5.0 背锅案》第5集-案情大转弯:都是我们的错,让 .NET 5.0 背锅团队
    [转]广东2021年初级会计考试报名:2020年12月14日起
    [转]楷书书写教程
    漫漫岁月唯一身心
    POS及POSP平台管理系统
    微服务场景下的自动化测试
    Hazelcast集群服务——Hazelcast介绍
    缓存模式(Cache Aside、Read Through、Write Through、Write Behind)
    springboot~uaa~scope对实体的字段添加限制
    java~通过ClassLoader动态加载~tomcat模型
  • 原文地址:https://www.cnblogs.com/yohaha/p/5234737.html
Copyright © 2011-2022 走看看