aoj-1189.cpp GitHub #include "../include/math/eratosthenes.cpp" using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() int y[2048000], x[2048000]; int number[2048][2048]; bool flag[2048][2048]; pair<int, int> dp[2048][2048]; int main() { int dy[4] = { 0, -1, 0, 1 }; int dx[4] = { -1, 0, 1, 0 }; int num = 0, ys = 1024, xs = 1024; for (int i = 2; i < 2500; i++) { REP(j, i / 2) { num++; y[num] = ys; x[num] = xs; number[ys][xs] = num; ys += dy[i % 4]; xs += dx[i % 4]; } } vector<int> prime_factor = eratosthenes(1000002); int m, n; while (cin >> m >> n, m) { memset(flag, 0, sizeof(flag)); REP(i, 2048) REP(j, 2048) dp[i][j] = make_pair(0, 0); REP(i, m + 1) flag[y[i]][x[i]] = (prime_factor[i] == 0); for (int i = 1; i < 2000; i++) { for (int j = 1; j < 2000; j++) { dp[i][j] = max({ dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1] }); dp[i][j].first += flag[i][j]; if (dp[i][j].first == 1 && flag[i][j]) dp[i][j].second = number[i][j]; } } cout << dp[y[n]][x[n]].first << " " << dp[y[n]][x[n]].second << endl; } return 0; } Includes eratosthenes.cpp Back