QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#37482 | #1284. Partition Number | shyou | ML | 0ms | 3664kb | C++ | 1.3kb | 2022-07-01 18:08:58 | 2022-07-01 18:08:59 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int partition(int m, int k, int** dyn, vector<int> setA) {
while (!setA.empty() && k == setA.back()) {
k--;
setA.pop_back();
}
if (dyn[m][k] != -1) return dyn[m][k];
else if (m == 0) return 1;
else if (k == 0) return 0;
else {
int sum = 0;
int num_iter = m / k;
for (int j = 0; j <= num_iter; j++) {
int new_m = m - j * k;
int new_k = min(new_m, k - 1);
sum = sum % int(pow(10, 9) + 7) + partition(new_m, new_k, dyn, setA)%int(pow(10,9)+7);
sum = sum % int((pow(10, 9) + 7));
}
dyn[m][k] = sum;
return sum;
}
}
int main() {
int N,m,size_A ;
cin >> N; //get number of test cases
for (int i = 0; i < N; i++) {
cin >> size_A >> m; //get size of setA and m
vector<int> setA(size_A);
for (int j = 0; j < size_A; j++) {
cin >> setA[j];
}
sort(setA.begin(), setA.end()); //sort setA in ascending order
//2D array for dynamic programming
int** dyn = new int* [m + 1];
for (int j = 0; j < m + 1; j++) {
dyn[j] = new int[m + 1];
}
for (int j = 0; j < m + 1; j++) {
for (int k = 0; k < m + 1; k++) {
dyn[j][k] = -1;
}
}
cout << partition(m, m, dyn, setA) << "\n";
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3664kb
input:
5 1 4 1 1 4 2 1 4 3 3 4 1 2 3 4 4 1 2 3 4
output:
2 3 4 1 0
result:
ok 5 tokens
Test #2:
score: -100
Memory Limit Exceeded
input:
500 1 2921 832 1 1952 1842 1 1890 1711 1 2710 2136 1 1420 118 1 1427 921 1 1436 1346 1 1099 676 1 1146 75 1 1993 963 1 2819 34 1 1830 19 1 2900 1912 1 1070 993 1 2114 1434 1 2115 457 1 1407 888 1 1374 98 1 1450 555 1 2740 1469 1 2983 490 1 2209 418 1 2698 2671 1 2653 734 1 1707 1674 1 1247 527 1 147...
output:
656513071 370664764 307269426 290963116 49499707 470710 612150225 330845525 873165144 948675759 659641122 968862177 64639712 328389177 408917220 4498768