Codeforces Round #300 Quasi Binary(DP)

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.

Examples
input
9
output
9
1 1 1 1 1 1 1 1 1
input
32
output
3
10 11 11
【题意】给出一种定义数,这种数每一位不是0就是1,给你一个n,问你最少可以由多少定义数组成,分别列出这些定义数。
【分析】简单DP,先预处理出所有可能的数,然后背包。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long LL;
const int N = 1e6+50;
int n;
int dp[N],vis[N],pre[N];
vector<int>vec,ans;
void init(){
    queue<int>q;
    q.push(1);
    vis[1]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vec.pb(u);
        int k = u*10+0;
        if(k<=1000000&&!vis[k]){
            q.push(k);
            vis[k]=1;
        }
        k = u*10+1;
        if(k<=1000000&&!vis[k]){
            q.push(k);
            vis[k]=1;
        }
    }
}
int main(){
    scanf("%d",&n);
    if(!n){
        printf("1\n0\n");
        return 0;
    }
    init();
    met(dp,inf);
    dp[0]=0;
    for(int i=1;i<=n;i++){
        for(int x : vec){
            if(i-x>=0&&dp[i-x]+1<dp[i]){
                dp[i]=dp[i-x]+1;
                pre[i]=i-x;
            }
        }
    }
    int k=n;
    while(k){
        int s=k-pre[k];
        k=pre[k];
        ans.pb(s);
    }
    printf("%d\n",dp[n]);
    for(auto x:ans){
        printf("%d ",x);
    }printf("\n");
    return 0;
}

 

转载于:https://www.cnblogs.com/jianrenfang/p/7226099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值