HEOI2014 南园满地堆轻絮

题目链接

题解

容易看出$Ans$越大,数列调整的余地越大,数列就越有可能变成一个不下降序列。
所以考虑二分答案,然后从头到尾遍历,尽量保持数列的不下降性。无法保持则判断为当前答案过小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cctype>
#define INF 2000000000
using namespace std;
typedef long long ll;
int read(){
int f = 1, x = 0;
char c = getchar();
while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); }
while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
return f * x;
}
int n, s[5000005];
ll a, b, c, d, mod;
int f(ll x){
ll res = (b + a * x) % mod;
res = (res * x + c) % mod;
res = (res * x + d) % mod;
return (int)res;
}
bool C(int g){
int bef = max(1, s[1] - g);
for(int i = 2; i <= n; ++i){
if(s[i] - g >= bef)
bef = s[i] - g;
else if(s[i] + g < bef)
return false;
}
return true;
}
void init(){
n = read();
a = read(), b = read(), c = read(), d = read();
s[1] = read(), mod = read();
s[0] = 0;
for(int i = 2; i <= n; i++)
s[i] = (f(s[i - 2]) + f(s[i - 1])) % mod;
}
void solve(){
int l = 0, r = mod;
while(r - l){
int mid = (r + l) >> 1;
if(C((r + l) >> 1))
r = mid;
else
l = mid + 1;
}
printf("%d\n", r);
}
int main(){
init();
solve();
return 0;
}