SGU 375题解

H - Amplifiers
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description



Scientist Shurik needs voltage that is   N  times more that the standard voltage in the wall outlet for power supply for his time machine. The standard voltage is equal to one Bervolt. Shurik decided to use voltage amplifiers. In the nearby shop he found the amplifiers of two types, the first type creates voltage 2X-1 Bervolt from   X  Bervolt, the second one creates voltage 2X+1 Bervolt from   X  Bervolt. The number of amplifiers in the shop is unlimited. Shurik wants to build a sequence of amplifiers from the outlet to the time machine. Of course he wants to minimize the number of amplifiers. Help him.

Input

A single integer 1 ≤   N≤ 2· 10   9.

Output

If it is possible to make such scheme, output in the first line the minimal possible number of amplifiers. The second line in this case is to contain the sequence of amplifiers from the outlet to the time machine. Use number 1 for the first-type amplifiers and number 2 for second-type amplifiers.  

If there is no solution, output "  
No solution
" (without quotes).

Sample Input

sample input
sample output
5
2
2 1

本题的题意是:输入一个数N,通过2*x-1或者通过2*x+1(其中x的起始值1,然后结果作为x的值)变换成N的最小步数,并且输出每一步(若是2*x-1则输出1,肉味2*x+1则输出2)

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>

char a[1010];
char b[1010];
char c[1010];

void deal(char *a,char *b){
    int lena=strlen(a);
    int lenb=strlen(b);
    int len;
    memset(c,'0',sizeof(c));
    int i,j,t;
    for(i=0;i<lena;i++){
        int arr=0;
        for(j=0;j<lenb;j++){
            t=(c[i+j]-'0')+(a[i]-'0')*(b[j]-'0')+arr;
            c[i+j]=t%10+'0';
            arr=t/10;
        }
        if(arr!=0){
            c[i+j]=arr+'0';
        }
    }
    for(i=1009;i>=0;i--){
        if(c[i]!='0') break;
    }
    memset(b,0,sizeof(b));
    len=i+1;
    for(i=0;i<len;i++){
        b[i]=c[i];
    }
    b[len]=0;
    memset(c,0,sizeof(c));
}

void make(char b1[],char b2[]){
    int i,len1,len2,j;
    len1=strlen(b1);
    len2=strlen(b2);
    if(len1<len2){
        int tp=len2;
        char cc[1010];
        strcpy(cc,b2);
        memset(b2,0,sizeof(b2));
        len2=len1;
        strcpy(b2,b1);
        memset(b1,0,sizeof(b1));
        len1=tp;
        strcpy(b1,cc);
    }
    for(i=len2;i<len1;i++){
        b2[i]='0';
    }
    int arr=0;
    for(i=0;i<len1;i++){
       int t=(b1[i]-'0')+(b2[i]-'0')+arr;
       b1[i]=t%10+'0';
       arr=t/10;

    }
    if(arr!=0){
        b1[len1]=arr+'0';
        b1[len1+1]=0;
    }else{
        b1[len1]=0;
    }
}

int main(){
    int n,m,i;
    while(~scanf("%d %d",&n,&m)){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        a[0]='2';
        b[0]='2';
        for(i=1;i<m;i++){
            deal(a,b);
        }
        char b1[1010];
        memset(b1,0,sizeof(b1));
        strcpy(b1,b);
        b1[0]=b1[0]-2;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        a[0]='2';
        b[0]='2';
        for(i=1;i<n;i++){
            deal(a,b);
        }
        char b2[1010];
        memset(b2,0,sizeof(b2));
        strcpy(b2,b);
        make(b1,b2);
        int len=strlen(b1);
        for(i=len-1;i>=0;i--){
            printf("%c",b1[i]);
        }
        puts("");
    }
return 0;
}



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值