HDU 4814
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4814
题意:
给一个黄金分割点的值,问怎么样把一个数(<=1e9)表示成以黄金分割点为进制的数。而且最后表示出来的数要进行标准化处理,即所有数字非0即1,且不能有两个1相邻。
思路:
复现的时候并没有细想。
两个性质f(n) = f(n - 1) + f(n - 2)-> 1 = f(-1) +f(-2),2*f(n) = f(n+1) + f(n - 2)(f代表黄金分割,里面参数代表幂次)
然后就可以把数字根据第一个性质划归到f(-1)和f(-2),根据两个性质来回折腾就可以。
自己写的时候开了个SPFA,妥妥TLE,因为无用状态太多。实际上考虑每次都会因为第二个性质最高位向前推进一位,而算出来最多有五十位,所以复杂度100*100。
源码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>
#include <queue>
using namespace std;
const int MAXN = 100 + 5;
int d[MAXN * 2];
struct D
{
int u, v;
bool operator < (const D& rbs)const{
return v < rbs.v;
}
};
priority_queue<D>que;
bool valid(int &l, int &r)
{
int ok = 0;
for(int i = l + 1 ; i <= r ; i++){
// printf("i = %d\n", i);
if(d[i] == 1 && d[i - 1] == 1){
d[i] = 0, d[i - 1] = 0;
d[i + 1]++;
if(r < i + 1) r = i + 1;
if(i == l) l = i + 1;
ok = 1;
}
else if(d[i] > 1){
que.push(D{i, d[i]});
ok = 1;
}
}
return ok;
}
void check(int l, int r)
{
for(int i = l ; i <= r ; i++) printf("%d", d[i]);
printf("\n");
}
int main()
{
int u;
while(scanf("%d", &u) != EOF){
if(u == 1){
printf("1\n");
continue;
}
memset(d, 0, sizeof(d));
while(!que.empty()) que.pop();
que.push(D{MAXN - 1, u});
que.push(D{MAXN - 2, u});
d[MAXN - 1] = d[MAXN - 2] = u;
int head, rear;
head = MAXN - 2, rear = MAXN - 1;
int flag = 1;
while(flag){
flag = 0;
for(int i = head ; i <= rear ; i++){
if(d[i] > 1){
d[i + 1] += d[i] / 2;
d[i - 2] += d[i] / 2;
d[i] %= 2;
head = min(head, i - 2);
rear = max(rear, i + 1);
flag = 1;
}
}
for(int i = head ; i <= rear ; i++){
int temp = min(d[i], d[i - 1]);
if(temp > 0){
d[i + 1] += temp;
d[i] -= temp;
d[i - 1] -= temp;
flag = 1;
rear = max(rear, i + 1);
}
}
while(d[head] == 0) head++;
while(d[rear] == 0) rear--;
}
while(d[head] == 0) head++;
while(d[rear] == 0) rear--;
for(int i = rear ; i >= head ; i--){
printf("%d", d[i]);
if(i == MAXN) printf(".");
}
printf("\n");
// printf("head = %d, rear = %d\n", head, rear);
}
return 0;
}