T1 Lyrith -迷宮リリス-
解题思路
众所周知,
8
×
125
=
1000
8\times125=1000
8×125=1000 。
所以只需要考虑后三位是
8
8
8 的倍数即可。
于是枚举
1
1
1~
1000
1000
1000 中
8
8
8 的倍数,判断每一位数字有没有出现过相应的次数以上即可。
code
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string s;
int n;
int a[10];
int main()
{
cin>>s;
n=s.size();
for(int i=0;i<n;i++)
a[s[i]-'0']++;
for(int i=1;i*8<1000;i++)
{
int t=i*8,b[4]={},tot=0;
while(t) b[++tot]=t%10,t/=10;
for(int j=1;j<=3;j++)
a[b[j]]--;
int flg=1;
for(int j=1;j<=3;j++)
if(a[b[j]]<0)
flg=0;
if(flg)
{
printf("YES\n");
int g=0,s=0;
for(int i=1;i<=10;i++)
while(a[i])
printf("%d",i),a[i]--,g=1;
for(int i=3;i;s+=b[i--])
if(!(!g&&!b[i]&&!s))
printf("%d",b[i]);
return 0;
}
for(int j=1;j<=3;j++) a[b[j]]++;
}
printf("NO\n");
}