## 雯舞

Love three things int he world -- Manchery

# [随机化 || 黄金分割] HihoCoder #1461 Challenge 26 Rikka with Number

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<ctime>
using namespace std;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

int tot=0;

inline int Gcd(int a,int b){
return !b?a:(tot+=a/b,Gcd(b,a%b));
}

int len=0;
int lst[1005];

int main(){
int T,t,n;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
while (T--){
for (int i=1;i<=1000 && minv>60;i++){
tot=0;
if (Gcd(n,t=(long long)rand()*rand()%(n-1)+1)==1)
if (tot<minv)
minv=tot,mink=t;
}
int a=n,b=mink;
len=0;
while (!(a+b==1))
if (a>b)
lst[++len]=0,a-=b;
else
lst[++len]=1,b-=a;
if (a==0 && b==1)
for (int i=len;i;i--)
printf("%d",lst[i]);
else
for (int i=len;i;i--)
printf("%d",1-lst[i]);
printf("\n");
}
return 0;
}


0.618就是黄金分割啊..最优情况下就是来回加这样步数最少，你算一下两个的比例就是0.618，所以就直接在这个附近找就可以了

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<ctime>
using namespace std;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

int tot=0;

inline int Gcd(int a,int b){
return !b?a:(tot+=a/b,Gcd(b,a%b));
}

const double ff=0.618;
int len=0;
int lst[1005];

int main(){
int T,t,n;
freopen("t.in","r",stdin);
freopen("t1.out","w",stdout);
while (T--){
if (n*ff<=1000){
for (int i=1;i<=n && minv>60;i++){
tot=0;
if (Gcd(n,i)==1)
if (tot<minv)
minv=tot,mink=i;
}
}else{
for (int i=1;i<=1000 && minv>60;i++){
tot=0;
if (Gcd(n,n*ff-500+i)==1)
if (tot<minv)
minv=tot,mink=n*ff-500+i;
}
}
int a=n,b=mink;
len=0;
while (!(a+b==1))
if (a>b)
lst[++len]=0,a-=b;
else
lst[++len]=1,b-=a;
if (a==0 && b==1)
for (int i=len;i;i--)
printf("%d",lst[i]);
else
for (int i=len;i;i--)
printf("%d",1-lst[i]);
printf("\n");
}
return 0;
}


#### Rikka with Sequence II

2017-03-24 10:16:23

#### hihoCoder1509 : 异或排序

2017-05-06 16:49:48

#### hihocoder Challenge 29 D. 不上升序列

2017-06-29 17:32:46

#### hiho-hihoCoder挑战赛29-D-不上升序列

2017-06-27 01:17:23

#### Codeforces 235E Number Challenge (神定理＋莫比乌斯反演)

2015-08-26 23:09:24

#### codeforces 235E. Number Challenge （反演）

2017-03-20 20:51:52

#### Codeforces 235E. Number Challenge DP

2015-07-04 11:08:05

#### [Codeforces235E]Number Challenge（莫比乌斯反演）

2017-03-20 22:56:14

#### 杜教筛学习小计

2017-04-12 21:06:50

#### Codeforces 235 E Number Challenge(莫比乌斯反演)

2016-06-06 16:00:29