选数字

题意

当给定一个序列 a[0],a[1],a[2],...,a[n-1]和一个整数 K 时,我们想找出,有多少子序列满足这么一个条件:把当前子序列里面的所有元素乘起来恰好等于K。


思路

比较裸的背包,将k质因数分解即可。

代码

#include <bits/stdc++.h>

using namespace std;

namespace StandardIO {

    template<typename T>inline void read (T &x) {
        x=0;T f=1;char c=getchar();
        for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
        for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
        x*=f;
    }

    template<typename T>inline void write (T x) {
        if (x<0) putchar('-'),x*=-1;
        if (x>=10) write(x/10);
        putchar(x%10+'0');
    }

}

using namespace StandardIO;

namespace Project {
    
    const int N=50005;
    
    int n,k;
    int cnt;
    int head[N];
    struct node {
        int to,next;
    } edge[N<<1];
    int tot;
    int dep[N],leaf[N],vis[N],f[N];
    
    inline bool cmp (int x,int y) {
        return (dep[x]==dep[y])?x<y:dep[x]>dep[y];
    }
    
    inline void add (int a,int b) {
        edge[++cnt].to=b,edge[cnt].next=head[a],head[a]=cnt;
    }
    void dfs (int now,int fa) {
        dep[now]=dep[fa]+1,f[now]=fa;
        int tot_son=0;
        for (register int i=head[now]; i; i=edge[i].next) {
            int to=edge[i].to;
            if (to==fa) continue;
            ++tot_son,dfs(to,now);
        } 
        if (!tot_son) leaf[++tot]=now;
    }

    inline void MAIN () {
        read(n),read(k);
        for (register int i=1,x; i<=n-1; ++i) {
            read(x);
            add(x,i),add(i,x);
        }
        dfs(k,k);
        sort(leaf+1,leaf+tot+1,cmp);
        for (register int i=1; i<=tot; ++i) {
            int now=leaf[i],ori=now;
            dep[now]=0;
            while (!vis[now]) {
                vis[now]=1,now=f[now],++dep[ori];
                if (now==k) break;
            }
        }
        sort(leaf+1,leaf+tot+1,cmp);
        write(k),putchar('\n');
        if (leaf[1]==0) return;
        for (register int i=1; i<=tot; ++i) {
            write(leaf[i]),putchar('\n');
        }
    }
    
}

int main () {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    Project::MAIN();
}

转载于:https://www.cnblogs.com/ilverene/p/11560363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值