結果

問題 No.1747 Many Formulae 2
ユーザー Gautam VermaGautam Verma
提出日時 2021-11-19 21:56:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,562 bytes
コンパイル時間 2,074 ms
コンパイル使用メモリ 200,252 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-30 08:20:40
合計ジャッジ時間 3,004 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
using namespace std;
#define all(x) x.begin(), x.end()
using u64 = uint64_t;
using u128 = __uint128_t;
u64 binpower(u64 base, u64 e, u64 mod) {
   u64 result = 1;
   base %= mod;
   while (e) {
      if (e & 1)
         result = (u128)result * base % mod;
      base = (u128)base * base % mod;
      e >>= 1;
   }
   return result;
}
bool check_composite(u64 n, u64 a, u64 d, int s) {
   u64 x = binpower(a, d, n);
   if (x == 1 || x == n - 1)
      return false;
   for (int r = 1; r < s; r++) {
      x = (u128)x * x % n;
      if (x == n - 1)
         return false;
   }
   return true;
};
bool MillerRabin(u64 n) {
   if (n < 2)
      return false;
   int r = 0;
   u64 d = n - 1;
   while ((d & 1) == 0) {
      d >>= 1;
      r++;
   }
   for (int a : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) {
      if (n == a)
         return true;
      if (check_composite(n, a, d, r))
         return false;
   }
   return true;
}
void solve() {
   string s;
   cin >> s;
   int n = s.length(), ans = 0;
   for (int i = 0, en = 1 << (n - 1); i < en; i++) {
      ll p = 0;
      string t = string(1, s[0]);
      for (int j = 0; j < n - 1; j++) {
         if ((i >> j) & 1) {
            t += s[j + 1];
         } else {
            p += stoll(t), t = s[j + 1];
         }
      }
      p += stoll(t);
      if (MillerRabin(p))
         ans++;
   }
   cout << ans << '\n';
}
int main() {
   ios::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);
   solve();
   return 0;
}
0