CF 354E DFS
竟然搜过去了。。
显然从后往前搜索。每次判断最后若干位是否和目标一致。
对于每一位,可以填i个0 j个4 k个7 其中i+j+k==6
先预处理出这个。然后按位搜索。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[100][4]; int all; typedef unsigned long long ll; ll M; int d[100],np; ll te[100]; void prework(){ all=0; for (int i=0;i<=6;i++) for (int j=0;j<=6-i;j++){ int k = 6-i-j; a[++all][0] = j*4+k*7; a[all][1] = i; a[all][2] = i+j; a[all][3] = i+j+k; } te[0] =1; for (int i=1;i<=18;i++) te[i]=te[i-1]*10; } bool check(ll a,ll b,int p){ int aa = a/te[p-1]%10; int bb = b/te[p-1]%10; return aa==bb; } bool work(ll mul,ll now,int p){ if (mul==now) return true; for (int i=1;i<=all;i++){ if (mul-now<a[i][0]*te[p]) continue; ll ne = now+a[i][0]*te[p]; if (!check(mul,ne,p+1)) continue; d[p]=i; np = p; if (work(mul,ne,p+1)) return true; } return false; } int dig[6][20]; void output(){ for (int i=0;i<=np;i++){ for (int j=0;j<a[d[i]][1];j++) dig[j][i]=0; for (int j=a[d[i]][1];j<a[d[i]][2];j++) dig[j][i]=4; for (int j=a[d[i]][2];j<a[d[i]][3];j++) dig[j][i]=7; } for (int i=0;i<6;i++){ ll now =0; for (int j=0;j<=np;j++) now+= te[j]*dig[i][j]; cout << now << " "; } cout << endl; } int main(){ prework(); int T; cin >> T; ll N; while(T--){ memset(dig,0,sizeof(dig)); cin >> N; if (work(N,0,0)) output(); else cout << -1 << endl; } }