题目链接:http://codeforces.com/problemset/problem/478/D
题意:给出红色方块r个,绿色方块g个,问最高能叠几层等腰三角形,而且每一层的颜色必须相同。
题解:一道简单的背包问题,设dp[i]表示放了i个红方块一共有几种方案。
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#define mod 1000000007
using namespace std;
const int M = 2e5 + 10;
int dp[M];
int main() {
int r , g;
scanf("%d%d" , &r , &g);
int sum = r + g;
int h = (sqrt(1 + 8 * sum) - 1) / 2;
//cout << h << endl;
memset(dp , 0 , sizeof(dp));
for(int i = 0 ; i <= h ; i++) {
dp[0] = 1;
}
int ans = 0;
dp[0] = 1;
for(int i = 1 ; i <= h ; i++) {
for(int j = r ; j >= i ; j--) {
dp[j] += dp[j - i];
dp[j] %= mod;
}
}
int num = h * (h + 1) / 2;
for(int i = 0 ; i <= r ; i++) {
if(num - i <= g) {
ans += dp[i];
ans %= mod;
}
}
printf("%d\n" , ans);
return 0;
}