SPOJ 12943. Counting, dp ,巧妙

Given integers N and M, output in how many ways you can take N distinct positive integers such that sum of those integers is <= M. Since result can be huge, output it modulo 1000000007
(10^9 + 7)

N <= 20

M <= 100000

Input

First line of input is number t, number of test cases. Each test case consists only of 2 numbers N and M, in that order. 

Output

Output number aksed in description.

题意:找n个不同的数且和不超过m, 求有多少种方案?

DP

n个数为ai,bi = an-i+1 - an-i, bn = a1.

sum(a[i]) = sum(b[i]*i)

即把问题

n个不同的数且和不超过m

转化为

b[i]>0 且 sum(b[i]*i)<=m

然后设dp[i][j]表示b数组前i项的和为j的方案数。

则:dp[i][j] = dp[i][j-i] + dp[i-1][j-i];

answer = sum(dp[n][j]);(1<=j<=m)

#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;
const int maxn = 20;
const int maxm = 1e5;
int dp[maxn+10][maxm+10];
int n, m;

int main() {
    int T;
    dp[0][0] = 1;
    for(int i=1; i<=maxn; ++i) for(int j=i; j<=maxm; ++j){
        dp[i][j] = dp[i][j-i] + dp[i-1][j-i];
        if(dp[i][j]>=mod) dp[i][j] -= mod;
    }
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        int ans = 0;
        for(int i=1; i<=m; ++i) {
            ans += dp[n][i];
            if(ans>=mod) ans -= mod;
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-04 10:38:23

SPOJ 12943. Counting, dp ,巧妙的相关文章

SPOJ ANARC05H 计数DP

给定一个数字串,问有多少种拆分方法,题目所谓的拆分,就是分成若干个子块,每个块的和 即为各个数字相加,当前块的和一定要小于等于后面的块的和 比如1117  就有这些[1-117], [1-1-17], [1-11-7], [1-1-1-7], [11-17],and [111-7] 肯定是计数DP,而且二维即可,不过第二维应该怎么设置是亮点,我也想了好多种方案,不过都被否定了,后来还是一种其实比较经典的方案进来了,就是代表当前最后一个块的和是多少,则当前dp[i][j] 由dp[i-1][k]转

SPOJ LUCIFER (数位dp)

LUCIFER - LUCIFER Number no tags Lucifer is the only human whi has defeated RA-ONE in a computer game .. RA-One is after lucifer for revenge and G-One is there to protect him ... All thi G-One and Ra-one Nonsense has disturbed lucifers life.. He want

SPOJ MIXTURES 区间dp

Harry Potter has n mixtures in front of him, arranged in a row. Each mixture has one of 100 different colors (colors have numbers from 0 to 99). He wants to mix all these mixtures together. At each step, he is going to take two mixtures that stand ne

Codeforces Beta Round #51---D. Beautiful numbers(数位dp, 巧妙)

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful num

SPOJ BALNUM (数位DP)

题意:求区间内出现过的奇数是偶数,出现过的偶数是奇数的个数. 析:这个题是要三进制进行操作的.dp[i][j] 表示前 i 位,状态是 j,可以用三进制来表示 0表示没有出现,1表示奇数,2表示偶数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include

SPOJ GSS3 (动态dp)

题意 题目链接 Sol 这题可以动态dp做. 设\(f[i]\)表示以\(i\)为结尾的最大子段和,\(g[i]\)表示\(1-i\)的最大子段和 那么 \(f[i] = max(f[i - 1] + a[i], a[i])\) \(g[i] = max(g[i - 1], f[i])\) 发现只跟前一项有关,而且\(g[i]从\)f[i]$转移过来的那一项可以直接拆开 那么构造矩阵 \[ \begin{bmatrix} a_{i} & -\infty & \dots a_{i} \\ a

poj 3046 Ant Counting DP

大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多少个. 那么,当前考虑第i种数字,我要组成长度为j的集合,只用在前i-1种数字所组成的集合中,只要添加0...cnt[i]个第i种数字之后长度能够达到j的那些集合数加起来 所以方程可以写成d[i][j] = ∑(cnt[i],0)  d[i-1][j-k]. 每种数字最多100个,数字最大为1000

SPOJ : DIVCNT2 - Counting Divisors (square)

设 \[f(n)=\sum_{d|n}\mu^2(d)\] 则 \[\begin{eqnarray*}\sigma_0(n^2)&=&\sum_{d|n}f(d)\\ans&=&\sum_{i=1}^n\sigma_0(i^2)\\&=&\sum_{i=1}^n\sum_{d|i}\sum_{k|d}\mu^2(k)\\&=&\sum_{k=1}^n\mu^2(k)G(\lfloor\frac{n}{k}\rfloor)\end{eqnarr

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734