#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head typedef pair<ll,ll> PLL; const ll M1=1e9,M2=1e5; int T; int vis[M2*2],dp[M2*2]; string gao(ll x,string t) { if (x==0) return ""; if (x==1) return t; if (x<=9) { if (SZ(t)==1) return to_string(x)+t; else return to_string(x)+"["+t+"]"; } else { function<int(ll)> compopt=[&](ll y) { int id=y<=M2?y:M2+x/y; if (vis[id]==T) return dp[id]; vis[id]=T; if (y==0) return dp[id]=0; if (y==1) return dp[id]=SZ(t); if (y<=9) return dp[id]=(SZ(t)==1)?2:3+SZ(t); dp[id]=1<<30; for (int i=9;i>=2;i--) { dp[id]=min(dp[id],3+compopt(y/i)+compopt(y%i)); if (y/i==1&&SZ(t)==1) dp[id]=min(dp[id],2+compopt(y%i)); } return dp[id]; }; function<string(ll)> constopt=[&](ll y) { int id=y<=M2?y:M2+x/y; if (y==0) return string(); if (y==1) return t; if (y<=9) return (SZ(t)==1)?to_string(y)+t:to_string(y)+"["+t+"]"; for (int i=9;i>=2;i--) { if (dp[id]==3+compopt(y/i)+compopt(y%i)) { return to_string(i)+"["+constopt(y/i)+"]"+constopt(y%i); } if (y/i==1&&SZ(t)==1&&dp[id]==2+compopt(y%i)) { return to_string(i)+t+constopt(y%i); } } assert(0); return string(); }; if (x<=M1) { ++T; compopt(x); return constopt(x); } for (int i=9;i>=4;i--) if (x%i==0) { return gao(i,gao(x/i,t)); } return gao(9,gao(x/9,t))+gao(x%9,t); } } string gopt(ll dx,ll dy) { assert(dx==0||dy==0||dx+dy==0); if (dx==0) { if (dy>0) return gao(dy,"F"); else return gao(-dy,"C"); } else if (dy==0) { if (dx>0) return gao(dx,"A"); else return gao(-dx,"D"); } else if (dx+dy==0) { if (dx>0) return gao(dx,"B"); else return gao(dy,"E"); } assert(0); return ""; } int cc=0; pair<string,vector<PLL>> build(ll n) { if (n==1) { return mp("A",vector<PLL>{mp(0,0),mp(0,1),mp(1,0)}); } else if (n==2) { return mp("AFDBA",vector<PLL>{mp(0,0),mp(0,2),mp(2,0)}); } else if (n%2==1) { auto [op2,pt2]=build(n-1); op2+="E"; op2+=gao(n-2,"FD"); op2+="F"; op2+=gao(n-1,"B"); op2+="C"; op2+="A"; pt2.pop_back(); pt2.push_back(mp(n-2,1)); pt2.pb(mp(n-1,1)); pt2.pb(mp(n,0)); return mp(op2,pt2); } else { auto [op2,pt2]=build(n/2); op2=gao(2,op2); per(i,1,SZ(pt2)) { op2+=gopt(pt2[i-1].fi-pt2[i].fi,pt2[i-1].se-pt2[i].se); } op2+="E"; op2+=gao(n/2,"AE"); op2+=gao(n/2-1,gao(n/2-1,"DB")+"D"+gao(n/2,"F")); cc+=SZ(gao(n/2-1,gao(n/2-1,"DB")+"D"+gao(n/2,"F"))); pt2.pop_back(); pt2.push_back(mp(n/2-1,1)); pt2.push_back(mp(n/2-1,n/2+1)); pt2.push_back(mp(0,n)); for (auto &[x,y]:pt2) swap(x,y); for (auto &c:op2) if (c>='A'&&c<='F') c='A'+'F'-c; return mp(op2,pt2); } } int main() { ll n; scanf("%lld",&n); auto [op2,pt2]=build(n); per(i,1,SZ(pt2)) { op2+=gopt(pt2[i-1].fi-pt2[i].fi,pt2[i-1].se-pt2[i].se); } printf("%s\n",op2.c_str()); //fprintf(stderr,"tot %d cc %d\n",SZ(op2),cc); }
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head typedef pair<ll,ll> PLL; const ll M1=1e9,M2=1e5; int T; int vis[M2*2],dp[M2*2]; string gao(ll x,string t) { if (x==0) return ""; if (x==1) return t; if (x<=9) { if (SZ(t)==1) return to_string(x)+t; else return to_string(x)+"["+t+"]"; } else { function<int(ll)> compopt=[&](ll y) { int id=y<=M2?y:M2+x/y; if (vis[id]==T) return dp[id]; vis[id]=T; if (y==0) return dp[id]=0; if (y==1) return dp[id]=SZ(t); if (y<=9) return dp[id]=(SZ(t)==1)?2:3+SZ(t); dp[id]=1<<30; for (int i=9;i>=2;i--) { dp[id]=min(dp[id],3+compopt(y/i)+compopt(y%i)); if (y/i==1&&SZ(t)==1) dp[id]=min(dp[id],2+compopt(y%i)); } return dp[id]; }; function<string(ll)> constopt=[&](ll y) { int id=y<=M2?y:M2+x/y; if (y==0) return string(); if (y==1) return t; if (y<=9) return (SZ(t)==1)?to_string(y)+t:to_string(y)+"["+t+"]"; for (int i=9;i>=2;i--) { if (dp[id]==3+compopt(y/i)+compopt(y%i)) { return to_string(i)+"["+constopt(y/i)+"]"+constopt(y%i); } if (y/i==1&&SZ(t)==1&&dp[id]==2+compopt(y%i)) { return to_string(i)+t+constopt(y%i); } } assert(0); return string(); }; if (x<=M1) { ++T; compopt(x); return constopt(x); } for (int i=9;i>=4;i--) if (x%i==0) { return gao(i,gao(x/i,t)); } return gao(9,gao(x/9,t))+gao(x%9,t); } } string gopt(ll dx,ll dy) { assert(dx==0||dy==0||dx+dy==0); if (dx==0) { if (dy>0) return gao(dy,"F"); else return gao(-dy,"C"); } else if (dy==0) { if (dx>0) return gao(dx,"A"); else return gao(-dx,"D"); } else if (dx+dy==0) { if (dx>0) return gao(dx,"B"); else return gao(dy,"E"); } assert(0); return ""; } int cc=0; pair<string,vector<PLL>> build(ll n) { if (n==1) { return mp("A",vector<PLL>{mp(0,0),mp(0,1),mp(1,0)}); } else if (n==2) { return mp("AFDBA",vector<PLL>{mp(0,0),mp(0,2),mp(2,0)}); } else if (n%2==1) { auto [op2,pt2]=build(n-1); op2+="E"; op2+=gao(n-2,"FD"); op2+="F"; op2+=gao(n-1,"B"); op2+="C"; op2+="A"; pt2.pop_back(); pt2.push_back(mp(n-2,1)); pt2.pb(mp(n-1,1)); pt2.pb(mp(n,0)); return mp(op2,pt2); } else { auto [op2,pt2]=build(n/2); op2=gao(2,op2); per(i,1,SZ(pt2)) { op2+=gopt(pt2[i-1].fi-pt2[i].fi,pt2[i-1].se-pt2[i].se); } op2+="E"; op2+=gao(n/2,"AE"); op2+=gao(n/2-1,gao(n/2-1,"DB")+"D"+gao(n/2,"F")); cc+=SZ(gao(n/2-1,gao(n/2-1,"DB")+"D"+gao(n/2,"F"))); pt2.pop_back(); pt2.push_back(mp(n/2-1,1)); pt2.push_back(mp(n/2-1,n/2+1)); pt2.push_back(mp(0,n)); for (auto &[x,y]:pt2) swap(x,y); for (auto &c:op2) if (c>='A'&&c<='F') c='A'+'F'-c; return mp(op2,pt2); } } int main() { ll n; scanf("%lld",&n); auto [op2,pt2]=build(n); per(i,1,SZ(pt2)) { op2+=gopt(pt2[i-1].fi-pt2[i].fi,pt2[i-1].se-pt2[i].se); } printf("%s\n",op2.c_str()); //fprintf(stderr,"tot %d cc %d\n",SZ(op2),cc); } |