zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 9 E. Thief in a Shop NTT

    E. Thief in a Shop
     

    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
     
     
    题意:
      给你n个数,你可以选数选k次,每次可以选相同的数,问你有多少种和
    题解:
      将k分解
      进行log(k)次FFT
      FFT精度可能不够,bool好像能过
      NTT,换一个合适 的 费马素数和原格就能过
       复杂度:O(n*log(n)*log(k))
    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18+1LL;
    const double pi = acos(-1.0);
    const int N = 5e5+10, M = 1e3+20,inf = 2e9,mod = 1e9+7;
    const long long P=23068673LL;
    const int G=3;
    
    LL mul(LL x,LL y){
        return (x*y-(LL)(x/(long double)P*y+1e-3)*P+P)%P;
    }
    LL qpow(LL x,LL k,LL p){
        LL ret=1;
        while(k){
            if(k&1) ret=mul(ret,x);
            k>>=1;
            x=mul(x,x);
        }
        return ret;
    }
    
    LL wn[50];
    void getwn(){
        for(int i=1; i<=22; ++i){
            int t=1<<i;
            wn[i]=qpow(G,(P-1)/t,P);
        }
    }
    
    int len;
    void NTT(LL y[],int op){
        for(int i=1,j=len>>1,k; i<len-1; ++i){
            if(i<j) swap(y[i],y[j]);
            k=len>>1;
            while(j>=k){
                j-=k;
                k>>=1;
            }
            if(j<k) j+=k;
        }
        int id=0;
        for(int h=2; h<=len; h<<=1) {
            ++id;
            for(int i=0; i<len; i+=h){
                LL w=1;
                for(int j=i; j<i+(h>>1); ++j){
                    LL u=y[j],t=mul(y[j+h/2],w);
                    y[j]=u+t;
                    if(y[j]>=P) y[j]-=P;
                    y[j+h/2]=u-t+P;
                    if(y[j+h/2]>=P) y[j+h/2]-=P;
                    w=mul(w,wn[id]);
                }
            }
        }
        if(op==-1){
            for(int i=1; i<len/2; ++i) swap(y[i],y[len-i]);
            LL inv=qpow(len,P-2,P);
            for(int i=0; i<len; ++i) y[i]=mul(y[i],inv);
        }
    }
    void Convolution(LL A[],LL B[]){
        for (int i=0;i<len;i++) A[i]=mul(A[i],B[i]);
    }
    int x,n,m;
    LL num[1<<22],now[1<<22];
    int main() {
        scanf("%d%d",&n,&m);
        int mx = 0;
        for(int i = 1; i <= n; ++i) scanf("%d",&x),num[x]=1,mx = max(x,mx);
        mx*=m;
        for(len=1; len<=mx; len<<=1);
        now[0] = 1;getwn();
        NTT(now,1); NTT(num,1);
        while(m > 0) {
            if(m%2) {
                Convolution(now,num);
            }
            Convolution(num,num);
            m/=2;
        }
        NTT(now,-1);
        for(int i = 1; i <= mx; ++i) {
            if(now[i]) printf("%d ",i);
        }
        return 0;
    }
    
    /*
    3 3
    3 5 11
    */
  • 相关阅读:
    【树状数组·进阶篇】树状数组实现平衡树(树状数组上二分)
    【$Polya$定理·应用篇】$Polya$定理的几种模型简介
    【博弈论·入门篇】$SG$函数基础入门
    关于win10安装CPC专利软件以及win10安装office2003
    部署react项目到服务器,配置nginx的伪静态
    中等 39. 组合总和 (递归,去除数组中重复的元素)
    jsonp策略,cors响应头,实现跨域
    文档-关于这次参加比赛写文档的总结
    前端-关于这次暑假的和大家一起做项目的总结
    servlet获取前台数据或者返回前台数据乱码的问题
  • 原文地址:https://www.cnblogs.com/zxhl/p/7106132.html
Copyright © 2011-2022 走看看