Divisibility by Thirty-six(字符串维护+整除问题+贪心)

Description

  There is a string containing only decimal digit characters. The length of the string is between 1 and 1,000. Using characters of the string, you have to construct the maximum number which divides by thirty-six without remainder. Each character of the string may not be used more than once.

Input

  The first line of the input contains an integer T (T <= 1000), indicating the number of cases. Each case begins with a line containing a single line representing the source string.

Output

  For each test case, print a line containing the test case number (beginning with 1) and the decimal representation of the maximum number (leading zeroes should be omitted). If no number can be constructed, please output “impossible” instead. 

Sample Input

2
03061
345

Sample Output

Case 1: 6300
Case 2: impossible

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 1005
#define M 15
int dir[30][2]={{0, 0},{4, 0},{8, 0},{1, 2},{1, 6},{2, 0},{2, 4},{2, 8},{3, 2},{3, 6},{4, 4},{8, 4},{5, 2},{5, 6},{6, 0},{6, 4},{6, 8},{7, 2},{7, 6},{8, 8},{9, 2},{9, 6}};
int s[N],temp,rec[M],cnt[M],flag,id,len;
char ans[N];
void put(int n[],int k,char *str)
{
    int l=0,i,j;
    for(i=9;i>=0;i--)
    {
        for(j=0;j<n[i];j++)
        {
            str[l]=i+'0';
            l+=1;
        }
    }
    for(i=0;i<2;i++)
    {
        str[l]=dir[k][i]+'0';
        l+=1;
    }
    str[l]='\0';
}
int compare(char *str,char *s)
{
    int i,j;
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]!=s[i]) return str[i]<s[i];
    }
    return 0;
}
void judge(int n)
{
    char now[N];
    put(cnt,id,now);
    if(n<len||(compare(ans,now)&&n==len))
    {
        flag=1;
        len=n;
        strcpy(ans,now);
    }
}
void dfs(int n,int d,int sum)
{
    if(d>=n)
    {
        if((sum-temp)%9==0) judge(n);
            return ;
    }
    for(int i=1;i<=8;i++)
    {
        if(!cnt[i]) continue;
        cnt[i]-=1;
        dfs(n,d+1,sum+i);
        cnt[i]+=1;
    }
}
void Search(int d)
{
    for(int i=0;i<2;i++)
    {
        cnt[dir[d][i]]-=1;
        if(cnt[dir[d][i]]<0) return ;
    }
    id=d;
    for(int i=0;i<=temp&&i<=len;i++)
    {
        dfs(i,0,0);
    }
}
int isok(char *str)
{
    int sum=0,l=strlen(str);
    if(l==0&&rec[0]==0) return 0;
    for(int i=0;i<l;i++)
    {
        sum+=str[i]-'0';
    }
    return !sum;
}
void solve()
{
    int i;
    for(i=0;i<22;i++)
    {
        memcpy(cnt,rec,sizeof(rec));
        Search(i);
    }
}
int main()
{
   int T,kase=1;
   scanf("%d",&T);
   while(T--)
   {
      char num[N];
      scanf("%s",num);
      len=strlen(num);
      temp=flag=0;
      memset(rec,0,sizeof(rec));
      memset(ans,0,sizeof(ans));
      for(int i=0;i<len;i++)
      {
          int item=num[i]-'0';
          temp+=item;
          rec[item]+=1;
      }
      solve();
      if(isok(ans))
         printf("Case %d: 0\n",kase);
      else if(flag)
         printf("Case %d: %s\n",kase,ans);
      else
         printf("Case %d: impossible\n",kase);
      kase+=1;
   }
   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值