Submission #991236

#TimeUsernameProblemLanguageResultExecution timeMemory
991236VMaksimoski008Trains (BOI24_trains)C++17
100 / 100
340 ms166740 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;
using ll = long long;

const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;

ll dp[maxn+5], pref[325][maxn+5], rem[325][maxn+5];

int32_t main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0); cin.tie(0);

    int n, mx = 0, mx2=0;
    cin >> n;
    int B = sqrt(n);

    vector<int> d(n+1), x(n+1);
    for(int i=0; i<n; i++) cin >> d[i] >> x[i];
    for(int i=0; i<n; i++) mx = max(mx, d[i]);
    for(int i=0; i<n; i++) mx2 = max(mx2, x[i]);

    dp[0] = 1;

    for(int i=0; i<n; i++) {
        for(int j=1; j<=B; j++) {
            pref[j][i%j] = (pref[j][i%j] - rem[j][i] + mod) % mod;
        }
        for(int j=1; j<=B; j++) {
            dp[i] += pref[j][i%j];
            dp[i] %= mod;
        }

        if(d[i] == 0) continue;

        if(d[i] > B) {
            for(int j=i+d[i]; j<=min((ll)n, (ll)i+x[i]*d[i]); j+=d[i]) dp[j] = (dp[j] + dp[i]) % mod;
        } else {
            pref[d[i]][i%d[i]] += dp[i];
            pref[d[i]][i%d[i]] %= mod;
            if((ll)i + (ll)(x[i]+1)*d[i] <= n) {
                rem[d[i]][i+(x[i]+1)*d[i]] += dp[i];
                rem[d[i]][i+(x[i]+1)*d[i]] %= mod;
            }
        }
    }
    
    ll ans = 0;
    for(int i=0; i<n; i++) ans = (ans + dp[i]) % mod;
    cout << ans << '\n';
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...