FOJ 1643 Oaiei's cube II

来源:互联网 发布:中国海关数据库公开 编辑:程序博客网 时间:2024/05/21 16:57
Oaiei's cube II

Time Limit:1sMemory limit:32M
Accepted Submit:16Total Submit:31

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

15

Sample Output

1110

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的情况类似,具体见代码不累述了.
总结:暴力生成数据观察规律,模拟解答


 

  1. #include <iostream>
  2. using namespace std;
  3. int a[2100]={0,2,2},sum[2100]={0,2};
  4. int main()
  5. {
  6.     int i,n,bit,k,bit1;
  7.     for(i=3;i<=2100;i++)
  8.         a[i]=a[i-1]+i-1;
  9.     for(i=2;;i++)
  10.     {sum[i]+=sum[i-1]+a[i];
  11.         if(sum[i]>=1500000000) break;
  12.     }
  13.     while(scanf("%d",&n)!=EOF)
  14.         {
  15.             for(i=1;i<=2088;i++)
  16.                 if(n-sum[i]<=0)
  17.                     break;
  18.             bit=i;
  19.             if(bit==1)
  20.                 {
  21.                     cout<<n-1<<endl;
  22.                     continue;
  23.                 }
  24.             if(n==sum[bit-1]+1)
  25.                 {
  26.                     printf("1");
  27.                     for(i=1;i<=bit-1;i++)
  28.                         printf("0");
  29.                     printf("/n");
  30.                     continue;
  31.                 }
  32.             if(n==sum[bit-1]+2)
  33.                 {
  34.                     printf("1");
  35.                     for(i=1;i<=bit-2;i++)
  36.                         printf("0");
  37.                     printf("1/n");
  38.                     continue;
  39.                 }
  40.             printf("1");
  41.             n-=sum[bit-1];
  42.             n-=2;
  43.             for(i=1;;i++)
  44.                 if(n>i+1)
  45.                     n-=i+1;
  46.                 else
  47.                     {
  48.                         bit1=i+1;
  49.                         break;
  50.                     }
  51.             bit--;
  52.             for(i=1;i<=bit-bit1;i++)
  53.                 printf("0");
  54.             printf("1");
  55.             bit=bit1-1;
  56.             if(n<=2)
  57.                 {
  58.                     
  59.                     bit--;
  60.                     for(i=1;i<=bit;i++)
  61.                         printf("0");
  62.                     printf("%d",n-1);
  63.                     printf("/n");
  64.                     continue;
  65.                 }
  66.             n-=2;
  67.             for(i=1;i<=bit-n-1;i++)
  68.                 printf("0");
  69.             printf("1");
  70.             for(i=1;i<=n;i++)
  71.                 printf("0");
  72.             putchar('/n');
  73.         }
  74.    return 0;
  75. }

 

 
原创粉丝点击