"Daily DP" gym-101889e Enigma digital DP Memory Search

Source: Internet
Author: User

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

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.