結果

問題 No.10 +か×か
ユーザー alpha_virginisalpha_virginis
提出日時 2016-11-09 20:36:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 873 bytes
コンパイル時間 1,481 ms
コンパイル使用メモリ 163,208 KB
実行使用メモリ 7,516 KB
最終ジャッジ日時 2023-08-16 16:25:11
合計ジャッジ時間 9,086 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int n;
int total;
int xs[64];
int lower[64];

char buf[64];

bool dfs(int i, int x) {
  // for(int j = 0; j < i; ++j) putchar(' ');
  // printf("(i, sum, x) = (%d, %d)\n", i, x);
  if( i == n ) return (x == total);
  do {
    if( x + lower[i+1] > total ) break;
    buf[i-1] = '+';
    if( dfs(i + 1, x + xs[i]) ) return true;
  } while(false);
  do {
    if( x * lower[i+1] > total ) break;
    buf[i-1] = '*';
    if( dfs(i + 1, x * xs[i]) ) return true;
  } while(false);
  return false;
}

int main() {
  scanf("%d", &n);
  scanf("%d", &total);
  for(int i = 0; i < n; ++i) {
    scanf("%d", &xs[i]);
  }

  // step 1
  for(int i = 0; i < n; ++i) {
    int x = 0;
    for(int j = i; j < n; ++j) {
      if( xs[j] != 1 ) x += xs[j];
    }
    lower[i] = x;
  }

  // step 2
  dfs(1, xs[0]);

  // step 3
  printf("%s\n", buf);
  
  return 0;
}
0