Quasi Binary(Codeforces Round #300)

B. Quasi Binary
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A number is called quasibinary if its decimal representation contains only digits 0 or 1. For example, numbers 0, 1, 101, 110011 — are quasibinary and numbers 2, 12, 900 are not.

You are given a positive integer n. Represent it as a sum of minimum number of quasibinary numbers.

Input

The first line contains a single integer n (1 ≤ n ≤ 106).

Output

In the first line print a single integer k — the minimum number of numbers in the representation of number n as a sum of quasibinary numbers.

In the second line print k numbers — the elements of the sum. All these numbers should be quasibinary according to the definition above, their sum should equal n. Do not have to print the leading zeroes in the numbers. The order of numbers doesn't matter. If there are multiple possible representations, you are allowed to print any of them.

Sample test(s)
input
9
output
9
1 1 1 1 1 1 1 1 1 
input
32
output
3
10 11 11 
 
        

      题意:给出一个数n,输出m个数相加等于n,并且使得m最小,而且所有的数
只能由0,1组成
      思路,由只有0,1两个数字可以联想到二进制,0->0, 01->1, 10->2, 11-
>3......,所以把十进制转换成二进制进行搜索即可。
 
        
 
        
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>

using namespace std;

int minn;
int n;
int p[100100],b[100010];
int v[1000100];

void DFS(int pn,int cnt)
{
    //printf("pn = %d   minn = %d   cnt = %d\n",pn,minn,cnt);
    if(cnt>=minn)
    {
        return ;
    }
    if(pn == 0)
    {
        if(minn>cnt)
        {
            minn = cnt;
            for(int i=0;i<cnt;i++)
            {
                b[i] = p[i];
            }
        }
        return ;
    }
    for(int i=65; i>=1; i--)
    {
        int pi = i;
        int r = 1;
        int sum = 0;
        while(pi)
        {
            sum += (pi%2)*r;
            r = r*10;
            pi = pi/2;
        }
        if(sum<=pn)
        {
            p[cnt] = sum;
            if(cnt+1<v[pn-sum])
            {
                DFS(pn-sum,cnt+1);
                v[pn-sum] = cnt + 1;
            }
        }
    }
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<=1000001;i++)
        {
            v[i] = 9999999;
        }
        minn = 999999;
        DFS(n,0);
        printf("%d\n",minn);
        for(int i=minn-1;i>=0;i--)
        {
            if(i == 0)
            {
                printf("%d\n",b[i]);
                break;
            }
            printf("%d ",b[i]);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值