結果

問題 No.12 限定された素数
コンテスト
ユーザー 久我山菜々
提出日時 2018-11-19 21:52:25
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,515 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 592 ms
コンパイル使用メモリ 86,160 KB
最終ジャッジ日時 2026-06-07 04:07:32
合計ジャッジ時間 1,633 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function 'void stage(std::array<int, 10>&, int, int)':
main.cpp:27:9: error: no match for 'operator[]' (operand types are 'std::array<int, 10>' and 'int')
   27 |     digs[n % 10] += direction;
      |         ^
main.cpp: In function 'int main(int, char**)':
main.cpp:35:23: error: variable 'std::array<int, 10> as' has initializer but incomplete type
   35 |   std::array<int, 10> as{};
      |                       ^~
main.cpp:6:1: note: 'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'
    5 | #include<bitset>
  +++ |+#include <array>
    6 | 
main.cpp:45:23: error: variable 'std::array<int, 10> nums' has initializer but incomplete type
   45 |   std::array<int, 10> nums{};
      |                       ^~~~
main.cpp:45:23: note: 'std::array' is defined in header '<array>'; this is probably fixable by adding '#include <array>'

ソースコード

diff #
raw source code

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<bitset>

static int const MAX = 5000000 + 1;

std::vector<int> primes() {
  std::vector<int> rs;
  std::bitset<MAX> ps;
  ps.set();
  ps[0] = ps[1] = 0;
  for(int i{2}; i < MAX; ++i) {
    if(!ps[i]) continue;

    rs.push_back(i);
    for(int j{i * 2}; j < MAX; j += i) {
      ps[j] = 0;
    }
  }
  return rs;
}

void stage(std::array<int, 10>& digs, int n, int direction) {
  while(n > 0) {
    digs[n % 10] += direction;
    n /= 10;
  }
}

int main(int, char**) {
  int N;
  std::cin >> N;
  std::array<int, 10> as{};
  for(int i{}; i < N; ++i) {
    int t;
    std::cin >> t;
    as[t] = 1;
  }
  std::vector<int> ps = primes();

  int ans{-1};
  int last{};
  std::array<int, 10> nums{};
  for(int i{}; i < ps.size(); ++i) {
    stage(nums, ps[i], 1);
    for(int j{}; j < 10; ++j) {
      if(as[j]) continue;
      while(nums[j] > 0) {
        stage(nums, ps[last++], -1);
      }
    }
    // std::cout << "### i=" << i << ", ps[i]=" << ps[i] << ", last=" << last << std::endl;

    bool flg{true};
    for(int j{}; j < 10; ++j) {
      if(!as[j]) continue;
      if(nums[j] == 0) {
        flg = false;
      }
    }
    if(i >= last && flg) {
      // std::cout << "enter i=" << i << std::endl;
      int k, l;
      k = last == 0 ?
        1 :
        ps[last - 1] + 1;
      l = i == (ps.size() - 1) ?
        MAX - 1 :
        ps[i + 1] - 1;
      ans = std::max(ans, l - k);
    }
  }
  std::cout << ans << std::endl;
}
0