码迷,mamicode.com
首页 > 其他好文 > 详细

uva 01350

时间:2015-01-15 23:43:44      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:

1350 - Pinary

`Pinary" number is a positive number using only two digits ``0" and ``1" with usual rule that it must not begin with a 0, and the additional rule that two successive digits must not be both ``1". This means that the factor ``11" is forbidden in the string. So the allowed Pinary writings are 1, 10, 100, 101, 1000, 1001,..., 100010101010100010001 . For example, ``100101000" is a Pinary number, but neither ``0010101" nor ``10110001" are Pinary numbers.

Each Pinary number represents a positive integer in the order they appear (using length order, lexicographic order), that is, 1 is mapped to 1, 10 is mapped to 2. And 100, 101 and 1000 are mapped to 3, 4 and 5, respectively. You are to write a program to generate Pinary number representations for integers given.

Input 

Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing a postive integer 2 < K < 90, 000, 000 .

Output 

Your program is to write to standard output. Print exactly one line for each test case. For each test case, print the Pinary number representation for input integer. The following shows sample input and output for three test cases.

Sample Input 

3 
7 
2000 
22

Sample Output 

1010 
1001000001001000 
1000001

 


大意:

  给出 pinary 数的定义, 就是二进制位中没有前导零且没有连续的 1.

  请你求出第 K 个 pinary 数的二进制位表示.

  

思路:

  这种数位计数问题有两个思路, 数位 DP 和 乱搞...

  "通过观察"(我是没发现), 可以发现位数为 T 的 pinary 的个数是 Fibbonacci 序列的第 T 项.

  然后就能够确定位数...确定了位数之后,我们就能够用查找的一般方法来寻找了.(少了不减, 多了减掉).

技术分享
 1 #include<cstdlib>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 const int maxn = 10000;
 7 string str;
 8 long long n,T;
 9 long long dig[maxn],prefix[maxn];
10 void init(){
11     dig[1] = dig[2] = 1;
12     prefix[1] = 1; prefix[2] = 2;
13     for(int i = 3, sigma = 2; sigma <= (int)9e8+10; sigma += dig[i], ++i)
14         dig[i] = dig[i-1] + dig[i-2], prefix[i] = prefix[i-1] + dig[i];
15 }
16 void process(int n){
17     int i,lim;
18     for(lim = 1; prefix[lim] < n; ++lim);
19     for(i = lim; i >= 1; --i){
20         if(n >= prefix[i-1] + 1) str += 1, n -= prefix[i-1] + 1;
21         else str += 0;
22     }
23 }
24 int main()
25 {
26     freopen("pin.in","r",stdin);
27     freopen("pin.out","w",stdout);
28     init();
29     cin >> T;
30     while(T--){
31         cin >> n;
32         str = "";
33         process(n);
34         cout << str << endl;
35     }
36     return 0;
37 }
View Code

 

 

uva 01350

标签:

原文地址:http://www.cnblogs.com/Mr-ren/p/4227463.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!