One day, oaiei plays with his cubes again. He writes “0” or “1” in his N cubes. Then he arranges these cubes to form a series of binary number. But, he is bored with this simple game, so he restricts the game as follows:
-
”0” is the first number in the sequence, and it is index is 1.
-
there are not more than three “1”s in the number .
-
all numbers in the sequence is increased and the index of each number is from 1
So the first 16 number is : 0,1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,10000.
Now, your task is to find the binary number according the index.
Input
There are multiple test cases. For each test case, there is an integer N, denoting the index (1<=N<=1.5 * 10 ^ 9) .
Output
For each test case, you should output the binary number according the index.
Sample Input
Sample Output
Original: FZU 2008 Summer Training II--Combinatorics
1.先生成小数据.(用暴力) 下面是1~32的数据: 1:0 2:1 3:10 4:11 5:100 6:101 7:110 8:111 9:1000 10:1001 11:1010 12:1011 13:1100 14:1101 15:1110 16:10000 17:10001 18:10010 19:10011 20:10100 21:10101 22:10110 23:11000 24:11001 25:11010 26:11100 27:100000 28:100001 29:100010 30:100011 31:100100 32:100101 通过位数分类,f[i]表示i位所有的种数 f[1]=2;f[2]=2;f[3]=4;f[4]=7;f[5]=11...f[n]=f[n-1]+n-1; 然后把位对应的种数加起来.使得给出一个数字可以迅速在o(n)内发现它的位数bit,这对于解题来说是一
大步 2.根据题目的条件知道表达式中最多只有3个1,那么对于任何一个数,第一个数字是1应该没什么疑问了(
从左边开始数).然后实际上后面的bit-1位中最多只能出现2个1 3.先考虑一个1的情况,很显然是bit-1种 例如 5:100 6:101 7:110 8:111 显然101,110是满足后面只有一个一的条件的,是3-1=2个. 然后很明显如果所给的数确定了位数以后减去sum[bit],如果是介于1..bit-1,那很明显就可以马上得到答
案. 4.考虑2个1的情况,和考虑1的情况类似,具体见代码不累述了. 总结:暴力生成数据观察规律,模拟解答
- #include <iostream>
- using namespace std;
- int a[2100]={0,2,2},sum[2100]={0,2};
- int main()
- {
- int i,n,bit,k,bit1;
- for(i=3;i<=2100;i++)
- a[i]=a[i-1]+i-1;
- for(i=2;;i++)
- {sum[i]+=sum[i-1]+a[i];
- if(sum[i]>=1500000000) break;
- }
- while(scanf("%d",&n)!=EOF)
- {
- for(i=1;i<=2088;i++)
- if(n-sum[i]<=0)
- break;
- bit=i;
- if(bit==1)
- {
- cout<<n-1<<endl;
- continue;
- }
- if(n==sum[bit-1]+1)
- {
- printf("1");
- for(i=1;i<=bit-1;i++)
- printf("0");
- printf("/n");
- continue;
- }
- if(n==sum[bit-1]+2)
- {
- printf("1");
- for(i=1;i<=bit-2;i++)
- printf("0");
- printf("1/n");
- continue;
- }
- printf("1");
- n-=sum[bit-1];
- n-=2;
- for(i=1;;i++)
- if(n>i+1)
- n-=i+1;
- else
- {
- bit1=i+1;
- break;
- }
- bit--;
- for(i=1;i<=bit-bit1;i++)
- printf("0");
- printf("1");
- bit=bit1-1;
- if(n<=2)
- {
-
- bit--;
- for(i=1;i<=bit;i++)
- printf("0");
- printf("%d",n-1);
- printf("/n");
- continue;
- }
- n-=2;
- for(i=1;i<=bit-n-1;i++)
- printf("0");
- printf("1");
- for(i=1;i<=n;i++)
- printf("0");
- putchar('/n');
- }
- return 0;
- }
|