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
第一行
第二行
接下来
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%:
100%:
Solution
最认真写题解的一次吗?
由题意可知,硬币的拓扑序,只有链和环两种情况。
因为(b,c)中b和c各不相等,即一个点多条出边或多条入边是不存在的。
而且有环显然为0种方案。
而且拓扑图不一定联通。
只要有环就必输出0(骗分大法好,加上暴力或许有50分?)。
因此有方案数只有全部都是链(包括孤点)的情况。
链的情况就和普通状态没啥软区别,按拓扑序捣腾一下就好。
问题是求拓扑分支的方案数。
对于各拓扑分支,跑一次背包问题的方案数?
瞬间爆炸!
普通的背包问题没有j这维,考虑化去这维。
发现如果
即变形一下,
有
发现
即如果去掉
假设现在存在
变成
而此时的
即
因此如果存在
但是普通的背包问题是允许物品不拿的,而本题某些硬币b至少拿1个。
就是说其所必占的价值已经知道了,我们可以作死地修改总价值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.
- CodeForces 283C Coin Troubles 背包问题 分析问题
- CodeForces 283C Coin Troubles 分析+背包思想
- 【Codeforces 283 C. Coin Troubles】+ 拓扑序 + 完全背包
- codeforces 283C Coin Troubles (神级思维+判环+背包)
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- leetcode 322. Coin Change 类似背包问题
- 问题 C: 背包问题
- 算法->完全背包问题 UVa 674 Coin Change
- UVA 674 Coin Change 钱币兑换问题 类似完全背包
- 背包问题的分析
- 完全背包问题分析
- 01背包问题分析
- 01背包问题分析
- 背包问题详细分析
- 3背包问题-C
- C++:背包问题
- C++: 背包问题
- C语言背包问题
- Android架构思考
- 最大连续子序列
- Markdown Learning
- iOS 面试++
- 添加header后的listview顶部多出divider的处理
- CodeForces 283C Coin Troubles 背包问题 分析问题
- 【实战Java高并发程序设计5】让普通变量也享受原子操作
- java_微信公众号开发笔记
- iOS--kxmovie之FFmpeg编译和使用
- android: qq 5.0 demo学习笔记(主 粒子爆炸效果+ViewDragHelper)
- Ionic项目中使用极光推送
- android悬浮窗口的实现
- iOS项目接入高德skd
- PeopleRank从社交网络中发现个体价值