本喵今天累了,所以水一篇。
题目链接:link,请点击这里喵~
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <string.h>
#include <time.h>
#define lnt long long
#define inf 0x7fffffff
using namespace std;
int xx;char ff,chh;inline int read(){
xx=ff=0;while(!isdigit(chh)){if(chh=='-'){ff=1;}chh=getchar();}
while(isdigit(chh)){xx=(xx<<1)+(xx<<3)+chh-'0';chh=getchar();}return ff? -xx: xx;
}
const int N=50,END=0.1*CLOCKS_PER_SEC;
char date[N];int ans,n,to;
void dfs(int,int,int,int);
int main(){
scanf("%s",date);
n=strlen(date);to=read();
for(int i=0;i<n;++i){date[i]-='0';}
for(ans=1;ans<n;++ans){//你总不可能搜出来 n 个加号吧
dfs(0,0,0,ans);
}
puts("-1");
return 0;
}
void dfs(int sum,int now,int dep,int cnt){
if(clock()>END){printf("%d",-1);exit(0);}
if(dep==n){
if(sum==to){
printf("%d",ans);
exit(0);
}
return;
}
if(sum>to){return;}
if(cnt>0){dfs(sum+date[dep],date[dep],dep+1,cnt-1);}
dfs(sum+now*9+date[dep],now*10+date[dep],dep+1,cnt);
}