結果

問題 No.910 素数部分列
ユーザー fura
提出日時 2020-05-15 13:18:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 650 bytes
コンパイル時間 2,300 ms
コンパイル使用メモリ 192,268 KB
最終ジャッジ日時 2025-01-10 11:06:42
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

int main(){
	int n;
	string s; cin>>n>>s;

	// 3, 5, 7 はそれ自身で使うのが最適
	int ans=0;
	string t;
	rep(i,n){
		if(s[i]=='1' || s[i]=='9') t+=s[i];
		else ans++;
	}

	// 11, 19, 991 は素数
	// できるだけ多く 19 と 991 を作ったあと, 残りで 11 を作るのが最適
	int cnt1=0,cnt9=0;
	rep(i,t.length()){
		if(t[i]=='1'){
			cnt1++;
			if(cnt9>=2){
				cnt1--;
				cnt9-=2;
				ans++;
			}
		}
		else{
			cnt9++;
			if(cnt1>=1){
				cnt1--;
				cnt9--;
				ans++;
			}
		}
	}
	ans+=cnt1/2;

	printf("%d\n",ans);

	return 0;
}
0