HDU 4389 x mod f (x) [Digital statistics DP]

Source: Internet
Author: User
Tags modulus

I used to be called "bitwise DP", which seems to be the same. Used to be a memory search to do, transfer up don't think much. Now I have learned this Daniel's writing, and I feel it is good to use iteration.

To summarize:

is to get an upper bound bound. Then logically the bound is divided into three parts, one is counted, one is the current statistic, the last is the non-statistic.

Bound from high to low (a[n~1]),

statistics I , a[n~i+1] are counted, all as a[i] (that one on the largest possible digital). Then A[i] is the current statistic bit, enumerating 0~a[i]-1 of these possible figures. and A[i-1~1] is the non-statistical bit, each time the non-statistic bit DP. (ie, under a[n~i], the number of non-statistical digits may be).


Then this problem, the ideas are in the code.

Code:

#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <string
> #include <vector> #include <map> #include <algorithm> using namespace std;  inline int Rint () {int x; scanf ("%d", &x), return x,} inline int max (int x, int y) {return (x>y), x:y;} inline int min (int x, int y) {return (x<y) x:y;} #define for (I, A, b) for (int i= (a); i<= (b); i++) #define FORD (I,A,B)
for (int i= (a); i>= (b), i--) #define REP (x) for (int i=0; i< (x); i++) typedef long long Int64;
#define INF (1<<30) const double EPS = 1e-8;
#define BUG (s) cout<< #s << "=" <<s<< ""//[A, b] the number of X%sigma (xi) =0 within. 
Idea://First interval maximum can be 10^9, must be DP, sub-problem.
Then the upper-digit statistic DP.
There must be two dimensions, the number of digits with the sum of each of you, and then consider the transfer, plus two-dimensional modulus with the remainder.
The number of satisfied numbers is indicated by the d[number of bits [members and] [modulus] [remainder].
d[len+1][sum+x][mod][(res*10+x)%mod] = Sigma (D[len][sum][mod][res]). Then statistics, from left to right bit-wise statistics, such as 321,//First bit for 0,1,2, that is, 0xx, 1xx, 2xx.
(xx denotes any two digits, and the value of DP is used to determine the number of matches.) The second place is0,1, i.e. 30x, 31x.

The third bit is 0, or 320 (a bit special, it is d[0][0][mod][0], and a number 321 is not counted,//So we put the last one alone.	#define MAXN 9 #define MAXSUM (maxn*9)//81 int tens[maxn+2];

Tens[i] = 10^i.	int d[maxn+2][maxsum+2][maxsum+2][maxsum+2];

	d[number of digits [members and] [modulus] [remainder] void dp () {memset (d, 0, sizeof (d));
	Initializes the boundary, len=1 for (sum, 0, 9) for (mod, 1, maxsum) d[1][sum][mod][sum%mod]++;
					DP for (LEN, 1, MAXN-1)//loop starting from 1, actually counting i+1 for (sum, 0, maxsum) for (mod, 1, maxsum) for (res, 0, MAXSUM-1)
		for (x, 0, 9)//enumeration Increment {if (sum+x>maxsum) break;
	d[len+1][sum+x][mod][(res*10+x)%mod] + + d[len][sum][mod][res];		}} int cal (int x) {if (x = = 0) return 0;//////A[MAXN+3];
	1-th int n=0;	int s = 0;
		Everyone and for (int t=x; t; t/=10) {a[++n] = t%10;
	S+=a[n];
	}//statistic int cnt = 0; For (mod, 1, 9*n)//enumeration possible modulus, that is, end of everyone and {if (mod>maxsum) break;//x=10^9 may be super if (mod>x) break;//pruning int pre = 0	;	The preceding and int sum = 0; At present everyone and FORD (i, N, 2)//from high to low enumeration {INT len = i-1;
				Number of remaining digits for (j, 0, A[i]-1)//enumeration of the current bit of the digital {if (mod-sum-j<0) break; For (res, 0, mod-1)//Enumerate remainder possible remainder {if ((pre+j*tens[len]+res)%mod = = 0) {//bug (len); bug (mod-sum-j); bug
						(mod); bug (res); bug (D[len][mod-sum-j][mod][res]) <<endl;
					CNT + = D[len][mod-sum-j][mod][res];
			}}} sum + = A[i];
			if (sum>mod) break;
		Pre + = A[i]*tens[len];
	}}//bug (CNT) <<endl;
		for (int t=x; t--, s--)//Handle the minimum bit {if (t%s = = 0) separately cnt++;	if (t%10 = = 0) break;
	To borrow.//bug (CNT) <<endl;
return CNT;
	} void Init () {tens[0] = 1;
	For (I, 1, maxn) tens[i] = tens[i-1]*10;
DP ();
	} int main () {init ();
	int T = Rint ();
		For (CA, 1, T) {int A = Rint ();
		int B = Rint ();
	printf ("Case%d:%d\n", CA, Cal (B)-cal (A-1)); }
}


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.