Changing Digits(dfs)

Problem Description

Given two positive integers n and k, you are asked to generate a new integer, say m, by changing some (maybe none) digits of n, such that the following properties holds:

  1. m contains no leading zeros and has the same length as n (We consider zero itself a one-digit integer without leading zeros.)
  2. m is divisible by k
  3. among all numbers satisfying properties 1 and 2, m would be the one with least number of digits different from n
  4. among all numbers satisfying properties 1, 2 and 3, m would be the smallest one
 

Input

There are multiple test cases for the input. Each test case consists of two lines, which contains n(1≤n≤10100) and k(1≤k≤104kn) for each line. Both n and k will not contain leading zeros.

 

Output

Output one line for each test case containing the desired number m.

 

Sample Input
2 2 619103 3219
 

Sample Output
2 119103
***************************************************************************************************************************

[Title] Italy

Gives the n and k, find a number m, m is a number satisfying all of the following four conditions: (1) the same number of bits n (2) can be divisible by k (3) with the same bit number n is not the same minimum ( 4) the minimum value satisfying the above three conditions. Wherein 1 ≦ n- ≦ 10 100  , 1 ≦ K ≦ 10 . 4Kn-

***************************************************************************************************************************

 1 #inc;ude<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int mod[110][10],test[110],nu[110],f[110][12000];
 6 char str[110];
 7 int k,len;
 8 void init()
 9 {
10     int i,j;
11     for(i=0;i<10;i++)mod[0][i]=i%k;
12     for(i=1;i<len;i++)
13     {
14         for(j=0;j<10;j++)
15         {
16             mod[i][j]=(mod[i-1][j]*10)%k;
17         }
18     }
19     memset(f,0,sizeof(f));
20 }
21 bool DFS(int pos,int num,int m )
22 {
23 
24     int i,j;
25     if(m==0)
26     {
27         for(i=len-1;i>=0;i--)printf("%d",test[i]);
28         printf("\n");
29         return true;
30     }
31     if(pos<0||num<=f[pos][m]||num==0)return false;
32     for(i=pos;i>=0;i--)
33     {
 34 is          for (J = 0 ; J <NU [I]; J ++) // from the request before a small value like 
35          {
 36              IF (I == len . 1 && J == 0 ) Continue ;
 37 [              Test [I] = J;
 38 is              int A = (M- (MOD [I] [NU [I]] - MOD [I] [J]) + K)% K;
 39  
40              IF (the DFS (I- . 1 , num- . 1 , A )) return  to true ;
 41 is  
42 is          }
 43 is          Test [I] = NU [I];
 44 is      }
 45      for(I = 0 ; I <= POS; I ++) // find a small value to a high level from the tail 
46 is      {
 47          for (J = NU [I] + . 1 ; J < 10 ; J ++ )
 48          {
 49              IF (I == len - . 1 && J == 0 ) Continue ;
 50              Test [I] = J;
 51 is              int A = (m + (MOD [I] [J] -mod [I] [NU [I]]))% K;
 52 is              IF ( the DFS (I- . 1 , num- . 1 , A)) return  to true ;
 53 is  
54 is          }
 55          Test [I] =NU [I];
 56 is      }
 57 is      F [POS] [m] = NUM; // value change recording 
58      return  to false ;
 59  
60  }
 61 is  void Solve ()
 62 is  {
 63 is      int I;
 64      int m = 0 ;
 65      for (I = 0 ; I <len; I ++ )
 66      {
 67           Test [I] = NU [I] = STR [len-I- . 1 ] - ' 0 ' ;
 68           m + = MOD [I] [NU [I] ];
 69           m% =k;
70     }
71     for(i=1;i<=len;i++)
72     {
73         if(DFS(len-1,i,m))return;
74     }
75 }
76 int main()
77 {
78     while(scanf("%s",str)!=EOF)
79     {
80         scanf("%d",&k);
81          len=strlen(str);
82         init();
83         solve();
84     }
85 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3536504.html

Guess you like

Origin blog.csdn.net/weixin_33895604/article/details/93432928