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.
The first line contains a single integer n (1 ≤ n ≤ 106).
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.
9
9 1 1 1 1 1 1 1 1 1
32
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; }