Fzu problem 1895 divisible 45 problem

Source: Internet
Author: User
Tags cas

This problem is a bit burning, but as long as you want to know the number divisible by 45, you can be divisible by 5 and 9, can be divisible by 9 of the number of people Plus is definitely 9 multiples, can be divisible by 5 The end is 0 or 5.

Then the DFS process is slightly less understood, and there are a few optimizations that must be noted. The process of Dfs is to choose which number we do not, and the less the number the better, so the number of deletions in the DFS process should be getting smaller, this step must have, otherwise time out.

The output of the time also need to pay attention to the situation of 0, can only output a 0, the following is the code

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intN =1005;Const intM = the;Const intdir[5]= {0,5};inttmp, Rec[m], cnt[m];intN, S[n], x, id;BOOLFlag;CharAns[n];BOOLcmpChar*a,Char*b) {     for(intI=0; A[i]; i++)    {        if(A[i]!=b[i])returnA[i] <B[i]; }    return false;}voidDfsintMintDintsum) {    if(d>=m) {if((sum-tmp)%9==0)        {            CharNow[n]; Memset (now,0,sizeof(now)); intLen =0;  for(intI=9; i>=0; i--)            {                 for(intj =0; J < Cnt[i]; J + +) Now[len+ +] = i +'0'; } Now[len+ +] = Dir[id] +'0'; if(M < x | | (CMP (ans, now) && m = =x)) {flag=true; X=m;            strcpy (ans, now); }        }        return; }     for(inti =1; I <=8; i++)    {        if(!cnt[i])Continue; Cnt[i]--; DFS (M, D+1, sum+i); Cnt[i]++; }}intMain () {intCAs; scanf ("%d", &CAs);  for(intI=1; i<=cas; i++)    {        CharNum[n]; scanf ("%s", num); X= strlen (num), TMP =0; Flag=false; memset (REC,0,sizeof(REC)); memset (ans,0,sizeof(ans));  for(intI=0; i<x; i++)        {            intcur = num[i]-'0'; TMP+=cur; Rec[cur]++; }         for(intI=0; i<2; i++) {memcpy (CNT, REC,sizeof(REC)); Cnt[dir[i]]--; if(Cnt[dir[i]) <0)Continue; ID=i;  for(intj=0; J<=tmp && j<=x; J + +) Dfs (J,0,0); }        if(!flag) {printf ("impossible\n"); Continue; }        intLenss = strlen (ans), sum1 =0;  for(inti =0; i < Lenss; i++) {sum1+ = Ans[i]-'0'; }        if(Sum1 = =0) {puts ("0"); Continue;    } puts (ans); }    return 0;}

Fzu problem 1895 divisible 45 problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.