UVa 10616 Divisible Group Sums:DFS以及DP

10616 - Divisible Group Sums

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1557

思路:用DFS+记忆化搜索枚举组合,注意数字有<0的。

return dp[i][cnt][sum] = f(i + 1, cnt + 1, ((sum + arr[i]) % d + d) % d) + f(i + 1, cnt, sum);///i<n,cnt<m

完整代码:

01./*0.055s*/
02.
03.#include<cstdio>
04.#include<cstring>
05.typedef long long ll;
06.
07.ll dp[205][15][25];
08.int d, m, n, arr[205];
09.
10.ll f(int i, int cnt, ll sum)
11.{
12.    if (cnt == m) return dp[i][cnt][sum] = (sum % d ? 0 : 1);///选m个
13.    if (i == n) return dp[i][cnt][sum] = 0;///总共n个,如果i==n,说明cnt!=m,也就是还没有选完,此时dp=0
14.    if (dp[i][cnt][sum] >= 0) return dp[i][cnt][sum];
15.    return dp[i][cnt][sum] = f(i + 1, cnt + 1, ((sum + arr[i]) % d + d) % d) + f(i + 1, cnt, sum);
16.}
17.
18.int main()
19.{
20.    int cas = 0, q, i, ans;
21.    while (scanf("%d%d", &n, &q), n)
22.    {
23.        for (i = 0; i < n; ++i) scanf("%d", &arr[i]);
24.        printf("SET %d:\n", ++cas);
25.        for (i = 1; i <= q; ++i)
26.        {
27.            ans = 0;
28.            memset(dp, -1, sizeof(dp));
29.            scanf("%d%d", &d, &m);
30.            printf("QUERY %d: %d\n", i, f(0, 0, 0));
31.        }
32.    }
33.    return 0;
34.}

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索dfs
, 思路
, category
group
group by、ikongroup、恒地金融hengdigroup、group、蓝骄lanjiaogroup,以便于您获取更多的相关知识。

时间: 2024-10-01 10:29:34

UVa 10616 Divisible Group Sums:DFS以及DP的相关文章

UVA 10405 Longest Common Subsequence:简单DP

省赛还有不到50天了,自己DP这块实在是弱,准备就拿着些天狂刷DP了. http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346 大意: 求两个字符串的最长公共子序列. 思路:水题,不过需要注意的就是字符串里可能会出现空格,需要用gets,真是坑. 更多精彩内容:http://www.bianceng.cnhttp://www.biancen

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di

UVa 331 Mapping the Swaps (DFS)

331 - Mapping the Swaps Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=267 Sorting an array can be done by swapping certain pairs of adjacent entries in

UVa 437 The Tower of Babylon:DP&amp;amp;DAG

437 - The Tower of Babylon Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=378 思路: 对于一个(x,y,z)砖头,它可以有3中姿势放置: (前两个为地面,后一个为高) x, y, z x, z, y y, z, x 把每种姿势

UVa 10013 Super long sums:简单高精度

10013 - Super long sums Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=954 The Problem The creators of a new programming language D++ have found out that

UVa 348 Optimal Array Multiplication Sequence:区间DP&amp;amp;矩阵链乘,MCM

348 - Optimal Array Multiplication Sequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=284 记忆化搜索:dp[a][b] = max(dp[a][b], dp[a][i] + dp[i + 1][b] + x

UVa 11181 Probability|Given:DFS及贝叶斯公式

11181 - Probability|Given Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=2122 思路: 拿样例一的"三选二"为例: 分母怎么求?P=买*买*不买+买*不买*买+不买*买*买=0.092,这是三个人中恰有两个人

算法题:UVA 348 Optimal Array Multiplication Sequence(区间dp)

Optimal Array Multiplication Sequence Given two arrays A and B, we can determine the array C = A B using the standard definition of matrix multiplication: The number of columns in the A array must be the same as the number of rows in the B array. Not

算法题:UVA 10280 Old Wine Into New Bottles(dp完全背包)

Problem C: Old Wine Into New Bottles Wine bottles are never completely filled: a small amount of air must be left in the neck to allow for thermal expansion and contraction. If too little air is left in the bottle, the wine may expand and expel the c