Submission #129058

# Submission time Handle Problem Language Result Execution time Memory
129058 2019-07-11T13:56:11 Z gs14004 Fibonacci representations (CEOI18_fib) C++17
50 / 100
4000 ms 4272 KB
#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, pwr[MAXN];

mtrx num(int n){
	mtrx m;
	m.a[1][0] = m.a[1][1] = (n % 2 == 0);
	m.a[0][0] = (n + 1) / 2;
	m.a[0][1] = (n - 1) / 2;
	return m;
}

set<pi> s;

int query(){
	int prv = 0;
	mtrx m = E;
	for(auto &i : s){
		m = m * num(i.first - prv);
		m = m * pwr[(i.second - i.first) / 2];
		prv = i.second;
	}
	return m.a[0][0];
}

void add(int x){ 
	if(x == 0) x = 1;
	if(x == -1) return;
	auto ins = [](pi x){
		s.insert(x);
	};
	auto rem = [](pi x){
		s.erase(x);
	};
	auto it = s.upper_bound(pi(x + 1, -1));
	if(it != s.begin() && prev(it)->second >= x){
		auto intv = *--it;
		rem(intv);
		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;
					rem(*l);
				}
				ins(nxt);
			}
			add(intv.first - 2);
		}
		else{
			ins(pi(intv.first, x - 1));
			add(intv.second + 1);
		}
	}
	else{
		auto nxt = it;
		if(nxt != s.begin()){
			nxt = prev(nxt);
			if(nxt->second == x - 1){
				auto prv = *nxt;
				rem(prv);
				if(prv.first != prv.second){
					prv.second -= 2;
					ins(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;
					rem(*l);
					add(pos);
				}
				else if(l != s.end() && l->first == x + 2){
					auto it = *nxt;
					it.second = l->second;
					rem(*l);
					rem(*nxt);
					ins(it);
				}
				else{
					auto it = *nxt;
					it.second += 2;
					rem(*nxt);
					ins(it);
				}
				return;
			}
		}
		auto l = s.lower_bound(pi(x + 1, -1));
		if(l->first == x + 1){
			int pos = l->second + 1;
			rem(*l);
			add(pos);
		}
		else if(l->first == x + 2){
			auto nxt = *l; nxt.first -= 2;
			rem(*l);
			ins(nxt);
		}
		else{
			ins(pi(x, x));
		}
	}
}

int main(){
	E.a[0][0] = E.a[1][1] = 1;
	pwr[0] = E;
	pwr[1] = num(2);
	for(int i=2; i<MAXN; i++) pwr[i] = pwr[i-1] * pwr[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

fib.cpp: In function 'int main()':
fib.cpp:138:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&n);
  ~~~~~^~~~~~~~~
fib.cpp:140:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d",&x);
   ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 10 ms 3448 KB Output is correct
3 Correct 10 ms 3448 KB Output is correct
4 Correct 10 ms 3448 KB Output is correct
5 Correct 10 ms 3448 KB Output is correct
6 Correct 10 ms 3576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 10 ms 3448 KB Output is correct
3 Correct 10 ms 3448 KB Output is correct
4 Correct 10 ms 3448 KB Output is correct
5 Correct 10 ms 3448 KB Output is correct
6 Correct 10 ms 3576 KB Output is correct
7 Correct 10 ms 3452 KB Output is correct
8 Correct 10 ms 3448 KB Output is correct
9 Correct 10 ms 3448 KB Output is correct
10 Correct 10 ms 3448 KB Output is correct
11 Correct 10 ms 3440 KB Output is correct
12 Correct 10 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 10 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 10 ms 3448 KB Output is correct
3 Correct 10 ms 3448 KB Output is correct
4 Correct 10 ms 3448 KB Output is correct
5 Correct 10 ms 3448 KB Output is correct
6 Correct 10 ms 3576 KB Output is correct
7 Correct 10 ms 3452 KB Output is correct
8 Correct 10 ms 3448 KB Output is correct
9 Correct 10 ms 3448 KB Output is correct
10 Correct 10 ms 3448 KB Output is correct
11 Correct 10 ms 3440 KB Output is correct
12 Correct 10 ms 3448 KB Output is correct
13 Correct 10 ms 3448 KB Output is correct
14 Correct 10 ms 3448 KB Output is correct
15 Correct 10 ms 3448 KB Output is correct
16 Correct 10 ms 3420 KB Output is correct
17 Correct 10 ms 3448 KB Output is correct
18 Correct 10 ms 3448 KB Output is correct
19 Correct 9 ms 3448 KB Output is correct
20 Correct 10 ms 3448 KB Output is correct
21 Correct 10 ms 3448 KB Output is correct
22 Correct 10 ms 3448 KB Output is correct
23 Correct 10 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Execution timed out 4022 ms 4272 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 3448 KB Output is correct
2 Correct 10 ms 3448 KB Output is correct
3 Correct 10 ms 3448 KB Output is correct
4 Correct 10 ms 3448 KB Output is correct
5 Correct 10 ms 3448 KB Output is correct
6 Correct 10 ms 3576 KB Output is correct
7 Correct 10 ms 3452 KB Output is correct
8 Correct 10 ms 3448 KB Output is correct
9 Correct 10 ms 3448 KB Output is correct
10 Correct 10 ms 3448 KB Output is correct
11 Correct 10 ms 3440 KB Output is correct
12 Correct 10 ms 3448 KB Output is correct
13 Correct 10 ms 3448 KB Output is correct
14 Correct 10 ms 3448 KB Output is correct
15 Correct 10 ms 3448 KB Output is correct
16 Correct 10 ms 3420 KB Output is correct
17 Correct 10 ms 3448 KB Output is correct
18 Correct 10 ms 3448 KB Output is correct
19 Correct 9 ms 3448 KB Output is correct
20 Correct 10 ms 3448 KB Output is correct
21 Correct 10 ms 3448 KB Output is correct
22 Correct 10 ms 3448 KB Output is correct
23 Correct 10 ms 3448 KB Output is correct
24 Correct 10 ms 3448 KB Output is correct
25 Execution timed out 4022 ms 4272 KB Time limit exceeded
26 Halted 0 ms 0 KB -