結果

問題 No.1747 Many Formulae 2
ユーザー yansi819yansi819
提出日時 2024-04-20 14:14:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,027 bytes
コンパイル時間 4,773 ms
コンパイル使用メモリ 255,248 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 14:14:33
合計ジャッジ時間 5,708 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using mint = modint998244353;

bool is_prime(ll x) {
  if (x == 1) return false;
  for (ll i = 2; i * i <= x; i++) {
    if (x % i == 0) return false;
  }
  return true;
}

ll calc(string t) {
  vector<ll> v;
  string tmp;
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == '+') {
      //cout << t << ' ' << tmp << endl;
      v.push_back(stoll(tmp));
      tmp = "";
    } else {
      tmp += t[i];
    }
  }
  //cout << t << ' ' << tmp << endl;
  v.push_back(stoll(tmp));
  ll res = 0;
  for (int i = 0; i < v.size(); i++) {
    res += v[i];
  }
  return res;
}

int main() {
  ll res = 0;
  string s; cin >> s;
  int n = s.size();
  for (ll b = 0; b < (1LL << n - 1); b++) {
    string t;
    for (int i = 0; i < n; i++) {
      t += s[i];
      if (i < n - 1 && b >> i & 1) {
        t += '+';
      }
    }
    res += is_prime(calc(t));
  }
  cout << res << endl;
  return 0;
}
0