40分求助dalao,玄关

P1464 Function

miffy_123 @ 2023-09-29 15:52:51

//2023/9/29 15:37
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 2147483646
int ans[21][21][21];
int w(int a,int b,int c){
    if(a<=0||b<=0||c<=0){
        return 1;
    }
    if(ans[a][b][c]!=0){
        return ans[a][b][c];
    }
    if(a<b&&b<c){
        int k=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
        ans[a][b][c]=k;
        return k;
    }
    int k=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
    ans[a][b][c]=k;
    return k;
}
int main(){
    for(int i=0;i<=20;i++){
        for(int j=0;j<=20;j++){
            for(int k=0;k<=20;k++){
                ans[i][j][k]=0;
            }
        }
    }
    ll a,b,c;
    scanf("%lld %lld %lld",&a,&b,&c);
    while(a>-1||b>-1||c>-1){
        cout<<"w("<<a<<", "<<b<<", "<<c<<")";
        if(a>20){
            a=20;
            b=20;
            c=20;
        }
        else if(b>20){
            a=20;
            b=20;
            c=20;
        }
        else if(c>20){
            a=20;
            b=20;
            c=20;
        }
        cout<<" = "<<w(a,b,c)<<endl;
        cin>>a>>b>>c;
    }
    return 0;
}

by changzang @ 2023-09-30 11:26:22

#include<bits/stdc++.h>
using namespace std;
long long s[30][30][30];
long long x,y,z,t=0;
long long f(long long a,long long b,long long c){
    if(a<=0||b<=0||c<=0){
        return 1;
    }else if(a>20||b>20||c>20){
        if(t==0){
            s[20][20][20]=f(20,20,20);
            t=1;
        }
        return s[20][20][20];
    }
    if(s[a][b][c]>0){
        return s[a][b][c];
    }
    if(a<b&&b<c){
        s[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);
        return s[a][b][c];
    }else{
        s[a][b][c]=f(a-1,b,c)+f(a-1,b-1,c)+f(a-1,b,c-1)-f(a-1,b-1,c-1);
        return s[a][b][c];
    }
}
int main(){
    int s=0;
    int jjj=1;
    while(jjj=1){
        cin>>x>>y>>z;
        if(x==-1&&y==-1&&z==-1) return 0;
        s=f(x,y,z);
        cout<<"w("<<x<<", "<<y<<", "<<z<<") = "<<s<<endl;
    }
    return 0;
} 

by Sqj147 @ 2023-10-06 17:28:23

// 2023/9/29 15:37
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 2147483646
int ans[21][21][21];
int w(int a, int b, int c) {
  if (a <= 0 || b <= 0 || c <= 0) {
    return 1;
  }
  if (ans[a][b][c] != 0) {
    return ans[a][b][c];
  }
  if (a < b && b < c) {
    int k = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
    ans[a][b][c] = k;
    return k;
  }
  int k = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) -
          w(a - 1, b - 1, c - 1);
  ans[a][b][c] = k;
  return k;
}
int main() {
  // for (int i = 0; i <= 20; i++) {
  //   for (int j = 0; j <= 20; j++) {
  //     for (int k = 0; k <= 20; k++) {
  //       ans[i][j][k] = 0;
  //     }
  //   }
  // } 1. 全局变量默认初始化为 0
  ll a, b, c;
  scanf("%lld %lld %lld", &a, &b, &c);
  // while (a > -1 || b > -1 || c > -1) {
  // 2. 保证输入的数在 [−9223372036854775808,9223372036854775807] 之间,并且是整数。
  // 你这个就不循环了
  while (1) {
    if (a <= 0 || b <= 0 || c <= 0) {
      printf("w(%lld, %lld, %lld) = 1\n", a, b, c);
    } else {
      cout << "w(" << a << ", " << b << ", " << c << ")";
      if (a > 20) {// 3. 注意:例如 w(30,-1,0) 又满足条件 1 又满足条件 2,请按照最上面的条件来算,答案为 1。
                   // 如果有一个 > 20,修改其他的 -1,你的解法就相当于先满足条件 2 再满足条件 1 了
        a = 20;
        b = 20;
        c = 20;
      } else if (b > 20) {
        a = 20;
        b = 20;
        c = 20;
      } else if (c > 20) {
        a = 20;
        b = 20;
        c = 20;
      }
      cout << " = " << w(a, b, c) << endl;// 4. 如果不需要清空缓存区,且输出次数较多,如
      // 保证不包括 $-1, -1, -1$ 的输入行数 $T$ 满足 $1 \leq T \leq 10 ^ 5$。
      // 建议使用 "\n",如 cout << " = " << w(a, b, c) << "\n";

    }
    cin >> a >> b >> c;
    if (a == -1 && b == -1 && c == -1) break;
  }
  system("pause");
  return 0;
}
/*----------------------------------------------------*/
/*测试
输入   
-1 21 21
-1 -2 -1234567
2147483648 1 1
-1 -1 -1
输出 
w(-1, 21, 21) = 1
w(-1, -2, -1234567) = 1
w(2147483648, 1, 1) = 1048576
*/
/*----------------------------------------------------*/
#include <bits/stdc++.h> // 示例代码
using namespace std;     
typedef long long ll;   
typedef pair<int, int> PII;
const int maxn = 25;

ll p[maxn][maxn][maxn];

ll w(ll a, ll b, ll c) {
  if (a <= 0 || b <= 0 || c <= 0) return 1;
  if (p[a][b][c]) return p[a][b][c];// 放在递归 w 之前
  // if (a > 20 || b > 20 || c > 20) return p[a][b][c] = w(20, 20, 20);
  if (a < b && b < c) return p[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1)
  - w(a, b - 1, c);
  return p[a][b][c] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c -
  1)
  - w(a - 1, b - 1, c - 1);
}

int main() {
  ll a, b, c, d, e, f;
  while (scanf("%lld %lld %lld", &a, &b, &c) == 3) {
    if (a == -1 && b == -1 && c == -1) break;
    d = a; e = b; f = c;
    if (a <= 0 || b <= 0 || c <= 0) {
      printf("w(%lld, %lld, %lld) = 1\n", a, b, c);
    } else {
      if (a > 20 || b > 20 || c > 20) d = e = f = 20;
      printf("w(%lld, %lld, %lld) = %lld\n", a, b, c, w(d, e, f));
    }
  }
  system("pause");
  return 0;
}

|