Luo Gu P2320 [HNOI2006] Guiguzi purse

Topic Portal

Problem-solving ideas:

For each number i, we can use i / 2 is represented, whereas for i / 2 we can be represented by i / 4 ...... (and so on)

For example, for 10, we can represent 5 to + 5, 3 and 5 can be represented by + 2, 2 + 1 can be represented by 1, so for 10, we only need to 5,3,2,1.

AC Code:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 int m,ans[2000000],k;
 7 
 8 int main()
 9 {
10     scanf("%d",&m);
11     while(m) {
12         ans[++k] = (m + 1) / 2;
13         m /= 2;
14     }
15     printf("%d\n",k);
16     k++;
17     while(--k) 
18         printf("%d ",ans[k]);
19     return 0;
20 }

 

Guess you like

Origin www.cnblogs.com/lipeiyi520/p/11306013.html