Test instructions: give you a string of length 1000 and a number n let you put in the string '? ' Fill in the number so that the string is a multiple of N and the smallest (no preamble 0)
The Dp[len][mod] is the state of the MoD (dp=0 or 1) for the presence of the first Len bit
Implemented with a mnemonic search, DFS returns 1 or 0.
If the last one is searched and the remainder is 0, return 1.
If you search for a DP state that has been updated, return 0 directly.
Add mod as a global variable and update mod constantly. For every bit of '? ' Violent enumeration 0~9
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<algorithm>#include<iostream>#include<math.h>#include<ctime>#include<vector>#include<queue>#include<stack>#include<list>#include<string>using namespacestd;#defineRep (i,j,k) for (int i = (int) j;i <= (int) K;i + +)#definePer (i,j,k) for (int i = (int) j;i >= (int) k;i--)#defineDebug (x) cerr<< #x << "=" << (x) <<endl#defineMmm (A, b) memset (A,b,sizeof (a))#definePB Push_backtypedefDoubleDb;typedefLong Longll;Const intMAXN = ++5;Const intMAXN = (int) 3e5 +7;Const intN = (int) 3e5 +7;Const intINF = (int)0x3f3f3f3f;//const LL mod = 1e9 + 7;strings;intN;intMoD =0;intDP[MAXN][MAXN];intANS[MAXN];BOOLDfsintx) {if(x = = S.length ())returnmod==0; if(Dp[x][mod] = =1)return 0; if(S[x] = ='?') { inti =0; if(x = =0) i =1; for(; I <=9; i++) { intMD =MoD; MoD*=Ten, mod + = i, mod%= n,ans[x]=i; if(Dfs (x +1))return 1; MoD=MD; } Dp[x][mod]=1; } Else { intMD =MoD; MoD*=Ten, mod + = s[x]-'0', MoD%= n,ans[x]=s[x]-'0'; returnDFS (x +1); Dp[x][mod]=1; MoD=MD; } return 0;}intMain () {CIN>> s >>N; if(Dfs (0)) { intLen =s.length (); Rep (I,0, Len-1) cout <<Ans[i]; } ElsePuts"*"); //Cin >> S;}/**/
"Daily DP" gym-101889e Enigma digital DP Memory Search