hdu3029 2010.3.6

来源:互联网 发布:黑龙江网络广播 编辑:程序博客网 时间:2024/05/13 09:58

hdu3029 2010.3.6

将题目转化成了三进制,如果数学学得不好的话,那就多写几组数据找规律,总结出来处理方法

 

Scales 3029

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 95    Accepted Submission(s): 45

 

 

Problem Description

Give you a scale, a goods weigts mkilograms. And then give you some stones weighting 1, 3, 9, 27, ..., 3^k. Ofcourse, the number of different weights' is only ONE.

 

Now put the goods on the left of scale. Andthen you should put some stones on two sides of scale to make the scalebalanced.

 

 

Input

An integer m, stands for the weights of thegoods (0 ≤ m ≤ 100 000 000)

 

 

Output

You should output two lines.

 

In the first line, the first integer N1 isthe number of stones putting on the left of scale, and then N1 integersfollow(in the ascend order), indicating the weight of stones putting on theleft, seperated by one space. In the second line, please use the same way asthe first line's to output the N2, which is the number of stones putting on theright side. Then following N2 integers, which are in ascending order, indicatethe stones' weight putting on the right side.

 

 

Sample Input

42

30

 

 

Sample Output

3 3 9 27

1 81

0

2 3 27

 

 

Source

2009 Multi-University Training Contest 6 -Host by WHU

 

 

Recommend

gaojie


#include <stdio.h>#include <string.h>int num[50],mid[50],m,lnum,left[50],right[50];int ans1[50],ans2[50],lans1,lans2;__int64 ans[20]={0,1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489};void change(){    int mm=m,i;    while (mm)    {        lnum++;        num[lnum]=mm%3;        mm/=3;    }    memcpy(mid,num,sizeof(num));}void main(){    int i;    while(scanf("%d",&m)!=EOF)    {        lnum=0;        memset(left,0,sizeof(left));        memset(right,0,sizeof(right));        memset(num,0,sizeof(num));        change();        for(i=1;i<=lnum;i++)            if (num[i]==2)            {                left[i]=1;                num[i+1]++;            }            else                if (num[i]>2)                    num[i+1]++;        lnum++;        for(i=1;i<=lnum;i++)        {            right[i]+=left[i]+mid[i];            right[i+1]+=right[i]/3;            right[i]%=3;        }        lnum++;        lans1=0;        lans2=0;        for(i=1;i<=lnum;i++)            if (left[i]==1)            {                lans1++;                ans1[lans1]=i;            }        for(i=1;i<=lnum;i++)            if(right[i]==1)            {                lans2++;                ans2[lans2]=i;            }        printf("%d",lans1);        for(i=1;i<=lans1;i++)            printf(" %d",ans[ans1[i]]);        printf("\n");        printf("%d",lans2);        for(i=1;i<=lans2;i++)            printf(" %d",ans[ans2[i]]);        printf("\n");                    }}


0 0