当前位置: 代码迷 >> 综合 >> 紫书 习题 10-22 UVa 10479 (找规律)
  详细解决方案

紫书 习题 10-22 UVa 10479 (找规律)

热度:108   发布时间:2023-09-20 20:26:35.0

自己一直在纠结这个串的构造方法

而没有观察串本身的规律……

 

2的63次方用 unsigned long long

然后可以发现串是递归构造的。

将串分成1,1,2,4,8,16, 然后会发现s串里面1个s-2串,2个s-3串,3个s-4串最后加上s

如第三个串1003

是由1个1串(1), 2个0串(0)最后加上自己3构成的

因此根据这个性质递归求就好了

 

#include<cstdio>
#include<vector>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;typedef unsigned long long ull;
ull n;ull dfs(int k)
{int p = 1;for(int i = k - 2; i >= 0; i--){ull t = i ? (1uLL << (i - 1)) : 1;REP(j, 0, p){if(n > t) n -= t;else return dfs(i);}p++;}return k;
}int main()
{while(~scanf("%llu", &n) && n){if(n == 1) { puts("0"); continue; }n--;for(int i = 1; ; i++){ull t = 1uLL << (i - 1);if(n > t) n -= t;else{printf("%llu\n", dfs(i));break;}}}return 0;
}