Submission #1033681

#TimeUsernameProblemLanguageResultExecution timeMemory
1033681kustizusTrains (BOI24_trains)C++17
100 / 100
122 ms19060 KiB
// #pragma GCC optimize("O3,unroll-loops") // #pragma GCC target("avx2,fma,bmi2,popcnt,lzcnt") #include <bits/stdc++.h> #define int long long using namespace std; mt19937_64 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N=1e5,MOD=1e9+7; int n,x[N+5],d[N+5],m[320][320],dp[N+5]; map <pair<int,int>,int> mp[N+5]; vector <pair<int,int>> v[N+5]; void Solve(){ cin>>n; int block=sqrt(n); for (int i=1;i<=n;++i){ cin>>d[i]>>x[i]; int o=i+d[i]*x[i]; if (o+d[i]<=n) v[o].push_back({d[i],x[i]}); } for (int i=n;i>=1;--i){ dp[i]=1; if (!d[i] || !x[i]) dp[i]=1; else if (d[i]>block){ int j=i+d[i]; while (j<=n){ dp[i]+=dp[j]; j+=d[i]; } dp[i]%=MOD; } else dp[i]=m[d[i]][i%d[i]]+1; for (pair <int,int> p : v[i]){ int d=p.first,x=p.second; if (!d || !x) mp[i][p]=0; else if (d>block){ int j=i+d,val=0; while (j<=n){ val+=dp[j]; j+=d; } val%=MOD; mp[i][p]=val; } else mp[i][p]=m[d][i%d]; } if (d[i] && x[i]){ int o=i+d[i]*x[i]; if (o+d[i]<=n) dp[i]-=mp[o][{d[i],x[i]}]; dp[i]=(dp[i]%MOD+MOD)%MOD; } for (int j=1;j<=block;++j) m[j][i%j]=(m[j][i%j]+dp[i])%MOD; } cout<<dp[1]; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); // freopen ("FILE.INP","r",stdin); // freopen ("FILE.OUT","w",stdout); int t=1; while (t--) Solve(); cerr<<"\nTIME: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n"; }
#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...