CodeForces 283C Coin Troubles 背包问题 分析问题

来源:互联网 发布:linux 查看目录被使用 编辑:程序博客网 时间:2024/05/16 08:23

汉化

题意

你有n种不同的硬币,每个硬币有一个价值ai(不同硬币价值可能相同)
我有这些硬币总价值为t,并且给出q对(b,c),告诉你第b种硬币的数量严格大于第c中硬币的数量。保证这q对(b,c)中所有b都不同,并且所有c都不同。
我问你我有多少种持有硬币的方案满足上面的约束?方案不同仅当存在至少一种硬币数量不同。答案可能很大,模10^9+7。

Input

第一行n,q,t
第二行n个数,分别是每一种硬币的价值ai(1ai105)
接下来q行,每行2个数为bici

Output

一个数为答案

Sample

Input1

4 2 17
3 1 2 5
4 2
3 4

Output1

3

Input2

3 2 6
3 1 1
1 2
2 3

Output2

0

Input3

3 2 10
1 2 3
1 2
2 1

Output3

0

Explanation1

可能的方案是(0,1,3,2) (0,0,6,1) (2,0,3,1)

Hint

40%:n20t200
100%:1<=n<=3000qn1t105

Solution

最认真写题解的一次吗?

由题意可知,硬币的拓扑序,只有链和环两种情况。
因为(b,c)中b和c各不相等,即一个点多条出边或多条入边是不存在的。
而且有环显然为0种方案。

而且拓扑图不一定联通。
只要有环就必输出0(骗分大法好,加上暴力或许有50分?)。
因此有方案数只有全部都是链(包括孤点)的情况。
链的情况就和普通状态没啥软区别,按拓扑序捣腾一下就好。

问题是求拓扑分支的方案数。
对于各拓扑分支,跑一次背包问题的方案数?

f[i][j][k]>f[i+1][p][k+a[i+1]p](j>p)

瞬间爆炸!

普通的背包问题没有j这维,考虑化去这维。
发现如果na>nb,那么我定了nb,a肯定至少取了nb
即变形一下,na+nb>nb,其中na+nb=na
na>0
发现na就和nb没有关系了!
即如果去掉b强制a选的数量,那么这道题的顺序要求就没有啦。

假设现在存在(b,c),那么价值应该是nbab+ncac,而将nb拆开来,
变成(nb+nc)ab+ncac=>nbab+nc(ab+ac)
而此时的nbnc显然没有题目里的关系了!但是bc的价值就需要重构!
ab=ab,ac=ab+ac
因此如果存在(b,c),那么就把b的价值扔给c,这道题就变成简单的背包问题求方案数啦。

但是普通的背包问题是允许物品不拿的,而本题某些硬币b至少拿1个。
就是说其所必占的价值已经知道了,我们可以作死地修改总价值t减去一个ab就可以了。
此时的t变成了剩余需要补充的价值,而问题也转化为了普通的背包问题求方案数。
所以喜闻乐见打dp啦。

代码很短 50行over。

#include <cstdio>#include <algorithm>#define FOR(i,j,k) for(i=j;i<=k;i++)using namespace std;int read() {    int s = 0, f = 1; char ch = getchar();    for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1;    for (; '0' <= ch && ch <= '9'; ch = getchar()) s = s * 10 + ch - '0';    return s * f;}const int mod = 1000000007;int f[100001], a[301], nxt[301], in[301];int n, q, t;int circle() {    while (q--) {        int b = 0, c, i;        FOR(i,1,n) if (nxt[i] && !in[i])        { b = i; break; }        if (!b) return 1;        c = nxt[b]; nxt[b] = 0; in[c]--;        a[c] += a[b]; t -= a[b];        if (t < 0) return 1;    }    return 0;}int main() {    int i, j, u, v;    n = read(), q = read(), t = read();    FOR(i,1,n) a[i] = read();    FOR(i,1,q) {        u = read(); v = read();        nxt[u] = v; ++in[v];    }    if (circle()) puts("0");    else {        f[0] = 1;        FOR(i,1,n) FOR(j,a[i],t) (f[j] += f[j - a[i]]) %= mod;        printf("%d", f[t]);    }    return 0;}

C. Coin Troubles

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
In the Isle of Guernsey there are n different types of coins. For each i (1 ≤ i ≤ n), coin of type i is worth ai cents. It is possible that ai = aj for some i and j (i ≠ j).

Bessie has some set of these coins totaling t cents. She tells Jessie q pairs of integers. For each i (1 ≤ i ≤ q), the pair bi, ci tells Jessie that Bessie has a strictly greater number of coins of type bi than coins of type ci. It is known that all bi are distinct and all ci are distinct.

Help Jessie find the number of possible combinations of coins Bessie could have. Two combinations are considered different if there is some i (1 ≤ i ≤ n), such that the number of coins Bessie has of type i is different in the two combinations. Since the answer can be very large, output it modulo 1000000007 (109 + 7).

If there are no possible combinations of coins totaling t cents that satisfy Bessie’s conditions, output 0.

Input

The first line contains three space-separated integers, n, q and t (1 ≤ n ≤ 300; 0 ≤ q ≤ n; 1 ≤ t ≤ 105). The second line contains n space separated integers, a1, a2, …, an (1 ≤ ai ≤ 105). The next q lines each contain two distinct space-separated integers, bi and ci (1 ≤ bi, ci ≤ n; bi ≠ ci).

It’s guaranteed that all bi are distinct and all ci are distinct.

Output

A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109 + 7).

Sample test(s)

input

4 2 17
3 1 2 5
4 2
3 4

output

3

input

3 2 6
3 1 1
1 2
2 3

output

0

input

3 2 10
1 2 3
1 2
2 1

output

0

Note

For the first sample, the following 3 combinations give a total of 17 cents and satisfy the given conditions: {0 of type 1, 1 of type 2, 3 of type 3, 2 of type 4}, {0, 0, 6, 1}, {2, 0, 3, 1}.

No other combinations exist. Note that even though 4 occurs in both bi and ci,  the problem conditions are still satisfied because all bi are distinct and all ci are distinct.

0 0
原创粉丝点击