zoukankan      html  css  js  c++  java
  • hdu1023 Train Problem II

    结题思路很容易想到递归,用记忆化搜索方式寻找答案。

    由于ans可能非常大,用c++需要自己写加法器。

    acm.hdu.edu.cn/showproblem.php?pid=1023

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 typedef __int64 LL;
     7 const int maxn = 150 + 10;
     8 char buffer[maxn * maxn][maxn];
     9 LL f[maxn][maxn];
    10 int n, N;
    11 
    12 void add(int lhs, int rhs, int dest){
    13     int d = 0;
    14     for(int i = maxn - 2; i >= 0; i--){
    15         int j = buffer[lhs][i] + buffer[rhs][i] + d;
    16         buffer[dest][i] = j % 10;
    17         d = j / 10;
    18     }
    19 }
    20 
    21 int solve(int s, int q){
    22     if(f[s][q] != -1) return f[s][q];
    23     if(!q) return f[s][q] = 0;
    24     if(s){
    25         int l1 = solve(s + 1, q - 1), l2 = solve(s - 1, q);
    26         add(l1, l2, N);
    27         N++;
    28         return f[s][q] = N - 1;
    29     }
    30     return f[s][q] = solve(s + 1, q - 1);
    31 }
    32 
    33 void print(int i){
    34     int low = maxn - 1;
    35     for(int j = 0; j < maxn - 1; j++){
    36         if(buffer[i][j]){
    37             low = j;
    38             break;
    39         }
    40     }
    41     for(int j = low; j < maxn - 1; j++) putchar(buffer[i][j] + '0');
    42     putchar('
    ');
    43 }
    44 
    45 int main(){
    46     memset(f, -1, sizeof f);
    47     memset(buffer, 0, sizeof buffer);
    48     buffer[0][maxn - 2] = 1;
    49     N = 1;
    50     while(~scanf("%d", &n)){
    51         int ans = solve(0, n);
    52         print(ans);
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    Bzoj1027 [JSOI2007]合金
    Bzoj4318 OSU!
    Bzoj3931 [CQOI2015]网络吞吐量
    Bzoj3551 [ONTAK2010]Peaks加强版
    Bzoj3545 [ONTAK2010]Peaks
    Bzoj4031 [HEOI2015]小Z的房间
    Bzoj3613 [Heoi2014]南园满地堆轻絮
    Bzoj4516 [Sdoi2016]生成魔咒
    HDU1847 Good Luck in CET-4 Everybody!
    HDU1846 Brave Game
  • 原文地址:https://www.cnblogs.com/astoninfer/p/4747226.html
Copyright © 2011-2022 走看看