# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
129050 | gs14004 | Fibonacci representations (CEOI18_fib) | C++17 | 2 ms | 376 KiB |
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 lint = long long;
using pi = pair<int, int>;
const int mod = 1e9 + 7;
const int MAXN = 200005;
struct mtrx{
int a[2][2];
mtrx operator*(const mtrx &m)const{
mtrx ret;
memset(ret.a, 0, sizeof(ret.a));
for(int i=0; i<2; i++){
for(int j=0; j<2; j++){
for(int k=0; k<2; k++){
ret.a[j][k] += 1ll * a[j][i] * m.a[i][k] % mod;
if(ret.a[j][k] >= mod) ret.a[j][k] -= mod;
}
}
}
return ret;
}
}E;
mtrx num(int n){
mtrx m;
m.a[0][1] = m.a[1][1] = (n % 2 == 0);
m.a[0][0] = (n + 1) / 2;
m.a[1][0] = (n - 1) / 2;
return m;
}
set<pi> s;
int n, a[MAXN];
lint dp[MAXN][2];
int query(){
n = 0;
int prv = 0;
mtrx m = E;
for(auto &i : s){
m = m * num(i.first - prv);
for(int j=0; j<(i.second - i.first)/2; j++){
m = m * num(2);
}
prv = i.second;
}
return m.a[0][0];
}
int cnt;
void add(int x){
if(x == 0) x = 1;
if(x == -1) return;
auto it = s.upper_bound(pi(x + 1, -1));
if(it != s.begin() && prev(it)->second >= x){
auto intv = *--it;
s.erase(it);
if(intv.first % 2 == x % 2){
add(intv.second + 1);
intv.second = x - 2;
if(intv.first <= intv.second){
auto l = s.lower_bound(pi(intv.second + 3, -1));
auto nxt = pi(intv.first + 1, intv.second + 1);
if(l != s.end() && l->first == intv.second + 3){
nxt.second = l->second;
s.erase(l);
}
s.insert(nxt);
}
add(intv.first - 2);
}
else{
s.emplace(intv.first, x - 1);
add(intv.second + 1);
}
}
else{
auto nxt = it;
pi ins(x, x);
if(nxt != s.begin()){
nxt = prev(nxt);
if(nxt->second == x - 1){
auto prv = *nxt;
s.erase(nxt);
if(prv.first != prv.second){
prv.second -= 2;
s.insert(prv);
}
add(x + 1);
return;
}
if(nxt->second == x - 2){
auto l = s.lower_bound(pi(x + 1, -1));
if(l != s.end() && l->first == x + 1){
int pos = l->second + 1;
s.erase(l);
add(pos);
}
else if(l != s.end() && l->first == x + 2){
auto it = *nxt;
it.second = l->second;
s.erase(l);
s.erase(nxt);
s.insert(it);
}
else{
auto it = *nxt;
it.second += 2;
s.erase(nxt);
s.insert(it);
}
return;
}
}
auto l = s.lower_bound(pi(x + 1, -1));
if(l->first == x + 1){
int pos = l->second + 1;
s.erase(l);
add(pos);
}
else if(l->first == x + 2){
auto nxt = *l; nxt.first -= 2;
s.erase(l); s.insert(nxt);
}
else{
s.emplace(x, x);
}
}
}
int main(){
E.a[0][0] = E.a[1][1] = 1;
int n, x;
scanf("%d",&n);
for(int i=0; i<n; i++){
scanf("%d",&x);
add(x);
printf("%d\n", query());
}
}
Compilation message (stderr)
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |