你有n种不同的硬币,每个硬币有一个价值ai(不同硬币价值可能相同)
我有这些硬币总价值为t,并且给出q对(b,c),告诉你第b种硬币的数量严格大于第c中硬币的数量。保证这q对(b,c)中所有b都不同,并且所有c都不同。
我问你我有多少种持有硬币的方案满足上面的约束?方案不同仅当存在至少一种硬币数量不同。答案可能很大,模10^9+7。
第一行 n,q,t
第二行 n 个数,分别是每一种硬币的价值 ai(1≤ai≤105)
接下来 q 行,每行2个数为 bi,ci
一个数为答案
4 2 17
3 1 2 5
4 2
3 4
3
3 2 6
3 1 1
1 2
2 3
0
3 2 10
1 2 3
1 2
2 1
0
可能的方案是(0,1,3,2) (0,0,6,1) (2,0,3,1)
40%: n≤20t≤200
100%: 1<=n<=3000≤q≤n1≤t≤105
最认真写题解的一次吗?
由题意可知,硬币的拓扑序,只有链和环两种情况。
因为(b,c)中b和c各不相等,即一个点多条出边或多条入边是不存在的。
而且有环显然为0种方案。
而且拓扑图不一定联通。
只要有环就必输出0(骗分大法好,加上暴力或许有50分?)。
因此有方案数只有全部都是链(包括孤点)的情况。
链的情况就和普通状态没啥软区别,按拓扑序捣腾一下就好。
问题是求拓扑分支的方案数。
对于各拓扑分支,跑一次背包问题的方案数?
普通的背包问题没有j这维,考虑化去这维。
发现如果 na>nb ,那么我定了 nb ,a肯定至少取了 nb 。
即变形一下, n′a+nb>nb ,其中 n′a+nb=na
有 n′a>0 。
发现 n′a 就和 nb 没有关系了!
即如果去掉 b 强制 a 选的数量,那么这道题的顺序要求就没有啦。
假设现在存在 (b,c) ,那么价值应该是 nb∗ab+nc∗ac ,而将 nb 拆开来,
变成 (n′b+nc)∗ab+nc∗ac=>n′b∗ab+nc∗(ab+ac)
而此时的 n′b 和 nc 显然没有题目里的关系了!但是 b 和 c 的价值就需要重构!
即 a′b=ab,a′c=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;
}
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.
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.
A single integer, the number of valid coin combinations that Bessie could have, modulo 1000000007 (109 + 7).
4 2 17
3 1 2 5
4 2
3 4
3
3 2 6
3 1 1
1 2
2 3
0
3 2 10
1 2 3
1 2
2 1
0
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.