test/math/aizu2870.test.cpp
Depends on
Code
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2870"
#include "math/linear_sieve.hpp"
#include <atcoder/modint>
#include <bits/stdc++.h>
using Z = atcoder::modint1000000007;
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N;
std::cin >> N;
std::vector<int> A(N);
for (auto &x : A) {
std::cin >> x;
}
linear_sieve sieve(std::ranges::max(A));
std::vector<Z> dp(N + 1);
dp[N] = 1;
for (auto i = N - 1; 0 <= i; --i) {
if (!sieve.is_prime(A[i])) {
continue;
}
for (auto j = 1; j <= 2; ++j) {
if (i + j <= N && (i + j == N || A[i] < A[i + j])) {
dp[i] += dp[i + j];
}
}
}
std::cout << dp[0].val() << "\n";
}
#line 1 "test/math/aizu2870.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/2870"
#line 1 "math/linear_sieve.hpp"
#include <cassert>
#include <map>
#include <vector>
struct linear_sieve {
explicit linear_sieve(int n) : lpf(n + 1) {
for (auto i = 2; i <= n; ++i) {
if (lpf[i] == 0) {
lpf[i] = i;
primes.push_back(i);
}
for (auto p : primes) {
if (lpf[i] < p || n < 1LL * i * p) {
break;
}
lpf[i * p] = p;
}
}
}
std::map<int, int> factorize(int x) const { // O(log x)
assert(1 <= x && x < int(lpf.size()));
std::map<int, int> f;
while (1 < x) {
++f[lpf[x]];
x /= lpf[x];
}
return f;
}
bool is_prime(int x) const { // O(1)
assert(1 <= x && x < int(lpf.size()));
return lpf[x] == x;
}
std::vector<int> lpf;
std::vector<int> primes;
};
#line 4 "test/math/aizu2870.test.cpp"
#include <atcoder/modint>
#include <bits/stdc++.h>
using Z = atcoder::modint1000000007;
int main() {
std::cin.tie(0)->sync_with_stdio(0);
int N;
std::cin >> N;
std::vector<int> A(N);
for (auto &x : A) {
std::cin >> x;
}
linear_sieve sieve(std::ranges::max(A));
std::vector<Z> dp(N + 1);
dp[N] = 1;
for (auto i = N - 1; 0 <= i; --i) {
if (!sieve.is_prime(A[i])) {
continue;
}
for (auto j = 1; j <= 2; ++j) {
if (i + j <= N && (i + j == N || A[i] < A[i + j])) {
dp[i] += dp[i + j];
}
}
}
std::cout << dp[0].val() << "\n";
}
Back to top page