Hit summer training Contest 11/b another number game

Source: Internet
Author: User
Directory

Topic


First, after the problem has a little new understanding, the biggest harvest is to awaken my previous memories.

This problem is DFS, but pure DFS will time out. How to do it.

Dfs is a deep search of the solution tree, in theory the answer tree each node represents a different state, but often we need the part is constant, that is, the intersection. Since it has not changed, it is possible to record the results of the first search and then search this state (where the state is focused on the unchanging part) and return the results directly. Said so much, is not to remind a little memory. Yes, it's called a memory search .

Memory Search when the Rujia of the petition, said that the DP (Dynamic programming) state transfer equation generally have two solutions, one is staged recursive, one is the memory of the search.

Search and DP, here to see the two have a magical connection, in fact, the memory of the search solution DP is the search for state space . In fact, I have come to such a conclusion, just too long did not touch DP, forget almost.


Second, again this competition, see this hiter problem solving, only to understand, this time all about "number" of the topic is actually called "digital Statistics problem" , is the content of the bitwise DP . (So 、、、)

The previous step number of a, with this question can be said to be very similar (the code will only need minor changes), but you can find that my approach to a problem is pure Dfs search tree, due to the problem of the solution tree constraints (the number of digits low is always greater than the number of high digits), All the nodes that lead to the solution tree are very small, and this is done quickly. Of course, the memory of the search can be further optimized (will remove the duplicate status of the search).

But this problem, without a special kind of constraint, the answer tree is similar to 10 tree (only in the upper bound will be less branches), so pure DFS will be tle (well, I just started to do this (⊙_⊙) ... )

This can be seen in the order of a B is still a bit of design significance, very good practice competition ~


Third, the Code

① Pure Dfs,tle

#include <cstdio> #include <cstring> #include <iostream> using namespace std;
#define LONG long int64 int sum;

int mod;
	void Solve (int islim, int num[], int pre, int cur, int n)//pre represents the previous few numbers and%mod {int curlim = Islim? Num[cur]: 9;
		if (cur = = n-1) {int res=0;
	for (int i=0; i<=curlim; i++) if ((pre+i)%mod = = 0) sum++; } else {for (int i=0; i<=curlim; i++) {Solve ((islim&& (i==num[cur)))? 1:0, Num, (pre+i)%mod, cur+1, N)
		;
	}}} int getnum (int x, int num[])//x with int64 {int buf[20];
	int i;		for (i=0; x; i++) {buf[i] = x%10;
	456 654 x/=10;
	} for (int j=0; j<i; J + +) {Num[j] = buf[i-j-1];
} return i;
	} int main () {int A, B;
		while (scanf ("%d%d%d", &a, &b, &mod)! = EOF) {int num[20];

		int n;
		if (a<0) {a = (1<<31)-1; n = getnum (a==1? a:a-1, num); num[9]++;}
		else {n = getnum (a==1? a:a-1, num);}
		Solve (1, NUM, 0, 0, N);
		
		int ans = sum;
		sum = 0; if (b<0) {b = (1<< 31)-1; n = getnum (b, NUM); num[9]++;
		} else {n = getnum (b, num);}
		Solve (1, NUM, 0, 0, N);
		ans = Sum-ans; if (a = = 1) ans+= 1%mod = = 0?
		1:0;
		printf ("%d\n", ans);
	sum=0; }
}

② changed to memory search, ac~

#include <cstdio> #include <cstring> #include <iostream> using namespace std;
#define LONG long int64 int mod;

#define MAXN #define MAXM 102 #define ISLIM 2 int dp[maxn][maxm][islim]; int solve (int islim, int num[], int pre, int cur, int n)//pre represents the previous few numbers and%mod {if (dp[cur][pre][islim]!=-1) return DP[CU

	R][pre][islim]; int curlim = Islim?
	Num[cur]: 9;
		if (cur = = n-1) {int res=0;
		for (int i=0; i<=curlim; i++) if ((pre+i)%mod = = 0) res++;
	return Dp[cur][pre][islim] = res;
		} else {int res=0;
		for (int i=0; i<=curlim; i++) {res + = Solve ((islim&& (i==num[cur)))? 1:0, Num, (pre+i)%mod, cur+1, N);
	} return Dp[cur][pre][islim] = res;
	}} int Getnum (int x, int num[])//x with int64 {int buf[20];
	int i;		for (i=0; x; i++) {buf[i] = x%10;
	456 654 x/=10;
	} for (int j=0; j<i; J + +) {Num[j] = buf[i-j-1];
} return i;
	} int main () {int A, B; while (scanf ("%d%d%d", &a, &b, &mod)! = EOF) {memset (DP, 1, sizeof (DP));
		int num[20];

		int n;
		if (a<0) {a = (1<<31)-1; n = getnum (a==1? a:a-1, num); num[9]++;}
		else {n = getnum (a==1? a:a-1, num);}
		
		int ans = solve (1, NUM, 0, 0, N);
		if (b<0) {b = (1<<31)-1; n = getnum (b, num); num[9]++;}
		else {n = getnum (b, num);}
		Memset (DP,-1, sizeof (DP));
		ans = solve (1, NUM, 0, 0, N)-ans; if (a = = 1) ans+= 1%mod = = 0?
		1:0;
	printf ("%d\n", ans); }
}


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.