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
    */
  • 相关阅读:
    框架_爬虫如何应用(爬虫架构设计与实现)
    架构师_设计模式_结构型_代理模式
    7语法基础_CLR核心解析
    架构师_设计模式_结构型_享元模式
    架构师_设计模式_结构型_组合模式
    同源策略、跨域、Same-origin、Cross-origin
    ES(elasticsearch) query DSL 查询语法
    日志框架
    powerdesigner 名称、注释互转
    maven 可运行 jar 包的打包,包含依赖 jar 包
  • 原文地址:https://www.cnblogs.com/zxhl/p/7106132.html
Copyright © 2011-2022 走看看