結果

問題 No.371 ぼく悪いプライムじゃないよ
ユーザー はまやんはまやん
提出日時 2016-05-18 14:30:57
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 160 ms / 1,000 ms
コード長 763 bytes
コンパイル時間 2,735 ms
コンパイル使用メモリ 172,548 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-30 18:19:32
合計ジャッジ時間 3,923 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 42
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'll solve()':
main.cpp:41:1: warning: control reaches end of non-void function [-Wreturn-type]
   41 | }
      | ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
typedef long long ll;

ll L, H;
vector<bool> primes;
void make_primes(int n)
{
	primes.resize(n + 1, true);
	primes[0] = primes[1] = false;
	rep(i, 2, sqrt(n))
	{
		if (primes[i])
		{
			for (int j = 0; i * (j + 2) < n; j++)
				primes[i * (j + 2)] = false;
		}
	}
}

ll solve()
{
	rrep(i, sqrt(H), 1) if (primes[i])
	{
		ll p = H / (ll)i * (ll)i;

		while (L <= p)
		{
			bool ok = true;
			rep(j, 2, i) if (p % (ll)j == 0) ok = false;
			if (ok)
			{
				return p;
			}

			p -= (ll)i;
		}
	}
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	make_primes(101010);

	while (cin >> L >> H)
	{
		cout << solve() << endl;
	}
}
0