結果

問題 No.12 限定された素数
ユーザー Gara GaraGara Gara
提出日時 2023-02-28 23:43:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,100 bytes
コンパイル時間 3,833 ms
コンパイル使用メモリ 203,180 KB
実行使用メモリ 4,540 KB
最終ジャッジ日時 2023-10-14 06:05:07
合計ジャッジ時間 3,694 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////////////
/// Variables ///
///////////////////////////////////////////////////
#define ll long long
#define REP(x,n) for(int x=0;x<n;x++)
#define REPll(x,n) for(ll int x=0;x<n;x++)
#define REP2(x,l,r) for(int x=l;x<r;x++)
#define REP2ll(x,l,r) for(ll int x=l;x<r;x++)
#define all(x) (x).begin(),(x).end() 
#define rall(x) (x).rbegin(),(x).rend() 
const ll int MOD=1000000007;
const ll int MOD2=998244353;
const int INF=1<<30;
const ll int INF2=(ll)1<<60;
const double PI = 3.1415926535897932;
///////////////////////////////////////////////////
/// Debug Function ///
///////////////////////////////////////////////////
/* debug fucのoverload
	https://o-treetree.hatenablog.com/entry/2020/06/11/220242
	debug(a,b,c): a,b,cを表示. 
	DEBUG,lDEBUG: 行数、変数名も表示
*/
namespace /* debug */{
	#define DEBUG(...) do{cout<<#__VA_ARGS__<<" = "; debug(__VA_ARGS__);}while(0) //変数
	#define ldebug(...) do{cout<<"["<<setw(3)<<__LINE__<<"] "; debug(__VA_ARGS__);}while(0) //行数
	#define lDEBUG(...) do{cout<<"["<<setw(3)<<__LINE__<<"] "<<#__VA_ARGS__<<" = "; debug(__VA_ARGS__);}while(0) //変数, 行数
	template <class T> void show(T &x){cout<<x<<" ";}
	template <class T> void showendl(T &x){cout<<x<<endl;}
	template <class P, class Q> void show(pair<P, Q> &x){cout<<"("<<x.first<<", "<<x.second<<") ";}
	template <class P, class Q> void showendl(pair<P, Q> &x){cout<<"("<<x.first<<", "<<x.second<<")"<<endl;}
	template <class H> void debug(H&& h){showendl(h);}
	template <class H, class... Ts> void debug(H&& h, Ts&&... ts){show(h);debug(forward<Ts>(ts)...);}
	template <class T> void debug(vector<T> &vt){int i=0;int size = vt.size();for(auto x: vt)++i!=size ? show(x) : showendl(x);}
	template <class T> void debug(vector<vector<T>> &vt){int i=0;int size = vt.size();for(int k=0; k<size; k++){int size_k = vt[k].size();for(auto x: vt[k])++i!=size_k ? show(x) : showendl(x);i=0;}}
	template <class T> void debug(deque<T> &vt){int i=0;int size = vt.size();for(auto x: vt)++i!=size ? show(x) : showendl(x);}
	template <class T> void debug(stack<T> s){int i=0;int size = s.size();while(!s.empty()){T w = s.top();++i!=size ? show(w) : showendl(w);s.pop();}}
	template <class T> void debug(queue<T> s){int i=0;int size = s.size();while(!s.empty()){T w = s.front();++i!=size ? show(w) : showendl(w);s.pop();}}
	template <class T> void debug(priority_queue<T> s){int i=0;int size = s.size();while(!s.empty()){T w = s.top();++i!=size ? show(w) : showendl(w);s.pop();}}
	template <class T> void debug(set<T> s){int i=0;int size = s.size();for(auto x: s)++i!=size ? show(x) : showendl(x);}
	template <class T> void debug(multiset<T> s){int i=0;int size = s.size();for(auto x: s)++i!=size ? show(x) : showendl(x);}
}
///////////////////////////////////////////////////
/// Function / Library ///
///////////////////////////////////////////////////
// a^b // O(logb) 
ll intpow(ll a,ll b){ ll ans = 1; while(b){if(b & 1) {ans *= a;} a *= a; b /= 2;}return ans;}
// a^b mod p // O(logb)
ll modpow(ll a, ll b, ll p){ll ans = 1; while(b){if(b & 1) {(ans *= a) %= p;} (a *= a) %= p; b /= 2;}return ans;}
// 負の数にも対応した % 演算
ll mod(ll val, ll m) {ll res = val % m; if (res < 0) res += m; return res;}
// mod. m での a の逆元 a^{-1} を計算する  逆元の存在条件:  gcd(a, m) = 1
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0; while (b) {ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v);} u %= m; if (u < 0) u += m; return u;}
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
// 整数を桁ごとにvectorへ // O(桁数)
template<class T> vector<ll int> num_digit (T a){string ss = to_string(a);int size = ss.size();vector<ll int> v(size);for(int i=0; i<size; i++){v[i] = ss[i] - '0';}return v;}
template<class T> ll int digit_length(T a){string ss = to_string(a);return (ll int)ss.size();}
// gcd(最大公約数) // O(log(min(a,b)))
ll int gcd(ll int a, ll int b){if (a%b == 0){return(b);}else{return(gcd(b, a%b));}}
// lcm(最小公倍数)// O(log(min(a,b)))
ll int lcm(ll int a, ll int b){return a / gcd(a, b) * b;}
/////////////////////////////////////////////////////////////

// 実行速度が気になる場合はtask.jsonのwall-optionを消して再buildする
int main(){
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
	
	int N; cin >> N;
	vector<int> A(N); REP(i,N) cin >> A[i];

	int M = 5000000; // bitsetのmax16000000?-1 
	bitset<5000001> is_prime(0); // 十分大きなサイズを確保します
    for (int i = 2; i <= M; ++i) is_prime.set(i); // とりあえず全部ふるいに入れます
    for (int i = 2; i <= M; ++i) {
        if (is_prime[i]) { // 素数 i を発見したら
            for (int j = i * 2; j <= M; j += i) {
                is_prime.reset(j); // i の倍数をふるい落とす
            }
        }
    }

	vector<int> ai(10, 0); REP(i,N) ai[A[i]] = 1;
	vector<int> cnt(10, 0);

	auto isok = [&]() -> int {
		int ok = 2;
		REP(i,10){
			if(!ai[i]){
				if(cnt[i]){
					ok = 0;
					break;
				}
			}
		}
		if(ok){
			REP(i,10){
				if(ai[i]){
					if(!cnt[i]){
						ok = 1;
						break;
					}
				}
			}
		}

		return ok;
	};

	auto add = [&](int p) -> void {
		int n = digit_length(p);
		REP(i,n){
			cnt[p % 10]++;
			p /= 10;
		}
	};

	auto dele = [&](int p) -> void {
		int n = digit_length(p);
		REP(i,n){
			cnt[p % 10]--;
			p /= 10;
		}
	};

	int ans = -1;
	// 尺取り [l, r)
	int l = 1;
	int r = 1;
	int f;
	while(l <= M){
		while(!is_prime[r]){
			if(r >= M){
				f=3;
				break;
			}
			r++;
		}
		if(f==3)break;

		while(r <= M){
			add(r);
			f = isok();
			if(!f){
				dele(r);
				break;
			}
			if(f==2) ans = max(ans, r - l);
			r++;
			while(!is_prime[r]){
				if(f==2) ans = max(ans, r - l);
				r++;
			}
		}

		if(is_prime[l]){
			dele(l);
		}
		l++;
		if(l >= r) {
			r = l+1;
			add(l);
		}
	}

	cout << ans;

	return 0;
}
0