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;
}



 

乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值