結果
問題 | No.10 +か×か |
ユーザー |
![]() |
提出日時 | 2015-07-13 00:15:32 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 37 ms / 5,000 ms |
コード長 | 2,020 bytes |
コンパイル時間 | 1,478 ms |
コンパイル使用メモリ | 166,304 KB |
実行使用メモリ | 10,220 KB |
最終ジャッジ日時 | 2024-12-14 15:30:04 |
合計ジャッジ時間 | 2,212 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 |
ソースコード
#include <bits/stdc++.h> typedef long long ll; typedef unsigned long long ull; #define FOR(i,a,b) for(int (i)=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define RANGE(vec) (vec).begin(),(vec).end() using namespace std; class AddOrMul { public: void solve(void) { int N, Total; cin>>N>>Total; vector<int> A(N); REP(i, N) cin>>A[i]; // 逆方向からパスを作る(順方向でやってハマった) // dp[i][s] = i 番目まで見たときに s の値を作成できるか vector<vector<bool>> dp(Total+1, vector<bool>(N+1, false)); dp[Total][N] = true; // start mark // O(Total*N) <= 50*10^5 = 5*10^6 for (int i = N-1; i >= 0; --i) REP(s, Total+1) { if (!dp[s][i+1]) continue; if (s-A[i] >= 0) dp[s-A[i]][i] = true; if (s%A[i] == 0) dp[s/A[i]][i] = true; } // 順方向でパスを作成する。 // 作成できないパスは途中でとまっているはず // N // +++++++ <- stop // +++++**+++++ <- stop // +...+++++*++++**+++++ <- reach // +...+**+++*+++++*++++ <- reach // string ans; int s = A[0]; FOR(i, 1, N) { if (s+A[i] <= Total && dp[s+A[i]][i+1]) { ans += "+"; s += A[i]; } else { ans += "*"; s *= A[i]; } } cout<<ans<<endl; } }; #if 1 int main(int argc, char *argv[]) { ios::sync_with_stdio(false); auto obj = new AddOrMul(); obj->solve(); delete obj; return 0; } #endif