結果

問題 No.1747 Many Formulae 2
ユーザー yasuwoyasuwo
提出日時 2021-11-21 19:49:28
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,170 bytes
コンパイル時間 8,559 ms
コンパイル使用メモリ 306,280 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 05:35:42
合計ジャッジ時間 7,094 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "main.cpp"
#include <bits/stdc++.h>
#include <atcoder/all>
// #include "library/templates/template.cpp"
using namespace atcoder;
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

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

long long solve(string S)
{
    int N = S.length();
    int ans = 0;
    for (int bit = 0; bit < 1 << (N - 1); bit++)
    {
        int le = 0;
        ll sum = 0;
        for (int i = 0; i < (N - 1); i++)
        {
            if (bit >> i & 1)
            {
                sum += stoll(S.substr(le, i - le + 1));
                le = i + 1;
            }
        }
        sum += stoll(S.substr(le));
        if (sum > 1 && is_prime(sum))
        {
            ans++;
        }
    }
    return ans;
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    string S;
    std::cin >> S;
    auto ans = solve(S);
    std::cout << ans << '\n';
    return 0;
}
0