ARC122C Calculator

Description

https://www.luogu.com.cn/problem/AT_arc122_c

Solution

如果从 $(1,0)$ 开始,因为 $N$ 很大,考虑增长率可能是比较快的一种方式:$y\gets x+y,x\gets x+y $ 这样不断进行下去,容易发现是斐波那契数列的形式。

也就是说,可以通过若干次 $(0,1)$ 或者 $(1,0)$ 开始轮流执行 $3,4$ 操作变成 $x$ 为斐波那契数列中的数。具体的,如果 $fib_1=fib_2=1$,奇数项在左边,偶数项在右边。

显然任何一个数都可以被分为斐波那契数列中的若干个数的和,我们贪心使拆分这个数,根据斐波那契数列的性质,容易发现每次下降的至少一半。所以拆分成的数是 $O(log)$ 级别的。

观察到每次执行 $3,4$ 操作对每个加入的 $1,2$ 操作带来的贡献是独立的。所以可以放在一起做!这样操作次数大概就是 $\log$ 级别的。

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
58
59
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define maxn 305
#define put() putchar('\n')
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
using namespace std;
Tp void read(T &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
#define fi first
#define se second
#define mk make_pair
const int mod=1e9+7;
int power(int x,int y=mod-2) {
int sum=1;
while (y) {
if (y&1) sum=sum*x%mod;
x=x*x%mod;y>>=1;
}
return sum;
}
const int inf=1e18;
int n,m;
int f[maxn],g[maxn],tot,ans[maxn],cnt;
signed main(void){
// freopen("1.in","r",stdin);
int i,j;
read(n);
f[1]=1,f[2]=1;m=2;
for (i=3;f[i-1]<=n;i++) f[i]=f[i-1]+f[i-2],m=i;
int now=n;
while (now) {
int it=lower_bound(f+1,f+1+m,now+1)-f-1;
now-=f[it];
g[++tot]=it;
}//贪心拆分
for (i=1;i<=tot;i++) {
if (i>1) {
for (j=g[i-1]-1;j>=g[i];j--) ans[++cnt]=(j%2?3:4);
}
for (j=i;j<=tot&&g[j]==g[i];j++)
if (g[i]&1) ans[++cnt]=1;
else ans[++cnt]=2;
}
i=tot+1;
for (j=g[i-1]-1;j>=g[i]+1;j--) ans[++cnt]=(j%2?3:4);
for (printf("%lld\n",cnt),i=1;i<=cnt;i++) printf("%lld\n",ans[i]);
return 0;
}