SingKwenCat @ 2023-04-28 20:01:20
记搜
#include<bits/stdc++.h>
#define N 21
using namespace std;
typedef long long ll;
ll mem[N][N][N];
ll dfs(ll a, ll b, ll c){
if (a <= 0 || b <= 0 || c <= 0) return 1;
if (a > 20 || b > 20 || c > 20) return dfs(20,20,20);
if (mem[a][b][c]) return mem[a][b][c];
ll res = 0;
if (a == 0 || b == 0 || c == 0) res = 1;
//else if (a > 20 || b > 20 || c > 20) res = dfs(20,20,20);
else if (a < b && b < c) res = dfs(a,b,c-1) + dfs(a,b-1,c-1) - \
dfs(a,b-1,c);
else res = dfs(a-1,b,c) + dfs(a-1,b-1,c) + dfs(a-1,b,c-1) - \
dfs(a-1,b-1,c-1);
if (a <= 20 && b <= 20 && c <= 20 && a > 0 && b > 0 && c > 0) \
mem[a][b][c] = res;
return res;
}
int main(){
ll a,b,c;
scanf("%lld%lld%lld", &a, &b, &c);
memset(mem, 0, sizeof mem);
while(a != -1 || b != -1 || c != -1){
ll ans = dfs(a,b,c);
printf("w(%lld, %lld, %lld) = %lld\n", a,b,c, ans);
scanf("%lld%lld%lld", &a, &b, &c);
memset(mem, 0, sizeof mem);
}
return 0;
}
dp
#include <bits/stdc++.h>
#define N 21
using namespace std;
typedef long long ll;
ll f[N][N][N];
int main(){
ll res;
ll a,b,c;
scanf("%lld%lld%lld", &a, &b, &c);
memset(f, 0, sizeof f);
while(a != -1 || b != -1 || c != -1){
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++){
if (i == 0 || j == 0 || k == 0) f[i][j][k] = 1;
else if (i<j && j<k) f[i][j][k] = f[i][j][k-1] + f[i][j-1][k-1] - f[i][j-1][k];
else f[i][j][k] = f[i-1][j][k] + f[i-1][j-1][k] + f[i-1][j][k-1] - f[i-1][j-1][k-1];
}
if (a <= 0 || b <= 0 || c <= 0) res = 1;
else if (a > 20 || b > 20 || c > 20) res = f[20][20][20];
else res = f[a][b][c];
printf("w(%lld, %lld, %lld) = %lld\n", a,b,c, res);
scanf("%lld%lld%lld", &a, &b, &c);
memset(f, 0, sizeof f);
}
return 0;
}
都是subtask#1 的#2点TLE了,不知道为什么,有什么能优化的吗?谢谢指点
by SingKwenCat @ 2023-04-29 09:53:58
或者给个数据也行 谢谢大家了
by SingKwenCat @ 2023-05-01 13:37:25
此贴终结
家人们我抽风了
把递推放while里面了 导致每次输入都会重新算一次
把for从while里面拉出来再把while里的memset注释掉就能过了