This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pli = pair<ll, int>;
const int mod = 1e9 + 7;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> d(n + 1), x(n + 1);
for (int i = 1; i <= n; i++){
cin>>d[i]>>x[i];
}
vector<ll> dp(n + 1); dp[1] = 1;
int sq = (int) sqrt(n);
vector<vector<ll>> sum(sq, vector<ll>(sq));
auto recalc = [&](int i){
for (int f = 1; f < sq; f++){
dp[i] = (dp[i] + sum[f][i % f]) % mod;
}
};
priority_queue<pli, vector<pli>, greater<pli>> pq;
auto add = [&](int i){
recalc(i);
if (!d[i]) return;
if (d[i] >= sq){
for (int j = 1; j <= x[i]; j++){
ll v = i + 1LL * d[i] * j;
if (v > n) break;
dp[v] = (dp[v] + dp[i]) % mod;
}
return;
}
pq.push({i + 1LL * d[i] * x[i], i});
sum[d[i]][i % d[i]] += dp[i];
sum[d[i]][i % d[i]] %= mod;
};
for (int i = 1; i <= n; i++){
add(i);
while (!pq.empty()){
auto [v, j] = pq.top();
if (v > i) break;
pq.pop();
sum[d[j]][j % d[j]] -= dp[j];
sum[d[j]][j % d[j]] %= mod;
}
}
ll out = 0;
for (int i = 1; i <= n; i++){
out = (out + dp[i]) % mod;
}
cout<<(out + mod) % mod<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |