来自:https://blog.csdn.net/nobleman__/article/details/80118296
题目:杭电fruit
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
#define mem(a) memset(a, 0, sizeof a)
int a[maxn],b[maxn];
int s1[maxn],s2[maxn];
void init() {
mem(a);
mem(b);
mem(s1);
mem(s2);
}
int main() {
int n,m;
while (~scanf("%d%d", &n, &m)) {
init();
for (int i = 0; i < n; i++) {
scanf("%d%d", &s1[i], &s2[i]);
}
for (int i = s1[0]; i <= s2[0]; i++) a[i] = 1;
for (int i = 1; i < n; i++) {
mem(b);
for (int j = 0; j <= m; j++) {
for (int k = s1[i]; k <= s2[i]; k++) {
b[j + k] += a[j];
}
}
memcpy(a, b, sizeof b);
}
printf("%d\n", a[m]);
}
return 0;
}
1
题目:杭电
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
#define mem(a) memset(a, 0, sizeof a)
double a[maxn],b[maxn]; // 注意为浮点型
int s1[maxn];
double f[11];
void init() {
mem(a);
mem(b);
mem(s1);
f[0] = 1;
for (int i = 1; i <= 10; i++) {
f[i] = f[i - 1] * i;
}
}
int main() {
int n,m;
while (~scanf("%d%d", &n, &m)) {
init();
for (int i = 0; i < n; i++) {
scanf("%d", &s1[i]);
}
for (int i = 0; i <= s1[0]; i++) a[i] = 1.0 / f[i];
for (int i = 1; i < n; i++) {
mem(b);
for (int j = 0; j <= m; j++) {
for (int k = 0; k <= s1[i] && k + j <= m; k++) {
b[j + k] += a[j] * 1.0 / f[k]; //注意这里
}
}
memcpy(a, b, sizeof b);
}
printf("%.0f\n", a[m] * f[m]);
}
return 0;
}