[COJ0528] Bjoi Lucky Number
Question Description
Input
See " Question description "
Output
See " Question description "
Input example
See " Question description "
Output example
See " Question description "
Data size and conventions
See " Question description "
Exercises
First think of a more violent digital DP method: Set F[I][J][S1][S2] represents the first I bit, the highest bit is J, where the number of odd digits and the number of S1, even digits and for the S2, the number of numbers satisfying this condition.
However, each query has to enumerate S1 and S2 each time it is processed, and the data of T = 1000 will be unbearable.
Paste this code first:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype > #include <algorithm>using namespace std; #define LL Long Longll Read () {ll x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 20#define maxs 90#define maxk 110LL g[maxn][10][maxs][maxs];int gcd[maxs][maxs];int Gcd (int x, int y) {retur N!y? X:GCD (y, x% y); }int K, NUM[MAXN]; ll sum (ll x) {int cnt = 0; LL tx = X;while (x) num[++cnt] = x%, x/= 10; LL ans = 0;int S1 = 0, s2 = 0;for (int i = cnt; i; i--) {for (int j = 0; J < Num[i]; j + +) for (int t1 = max (S1, 1); T1 < MAXS; t1++) for (int t2 = max (s2, 1); T2 < maxs; t2++) if (Gcd[t1][t2] <= K && g[i][j][t1-s1][t2-s2]) {ans + = g[i][j][ t1-s1][t2-s2];//printf ("%d%d%d:%d\n", I, J, T1-s1, T2-s2, G[i][j][t1-s1][t2-s2]);} if (I & 1) S1 + = Num[i]; else s2 + = Num[i];} if (gcd[s1][s2] <= K && s1 && S2) ans++;//printf ("%lld:%lld\n", TX, ans); return ans; int main () {g[0][0][0][0] = 1;for (int i = 0; i < maxn-1; i++) for (int j = 0; J <= 9; j + +) for (int s1 = 0; S1 < MA Xs s1++) for (int s2 = 0; s2 < maxs; s2++) if (G[i][j][s1][s2]) {for (int k = 0; k <= 9; k++) {int S1 = s1, S2 = s2;if (i + 1 & 1) S1 + = k; else S2 + = K;g[i+1][k][s1][s2] + = G[i][j][s1][s2];} printf ("%d%d%d:%d\n", I, J, S1, S2, g[i][j][s1][s2]);} for (int i = 0; i < maxs; i++) for (int j = 0; J < Maxs; J + +) Gcd[i][j] = GCD (i, j); int T = read (); while (t--) {K = read ( ); LL L = Read (), r = Read ();p rintf ("%lld\n", SUM (r)-sum (L-1));} return 0;}
Remember to preprocess Gcd[i][j], otherwise it will be slower ...
Now our main goal is to optimize the cost of each inquiry, and the core is to not enumerate S1 and S2 every time.
Let's consider the process of finding the answer in [1, X], we are a bit-determined number, so the current state can be represented graphically as a form:
xxxx____
where "X" means that the number is filled, "_" means that the number is not filled, we found that the reason why we have just enumerated S1 and S2 is that the previous number has been filled, the parity bit has a certain number and, so here we might as well set it as state, so it becomes:
F[I][K][S1][S2] Indicates that there is an i-bit is not filled, and the number previously filled in, the odd digits on the number and S1, even digits on the number and for the S2, and now we have to fill the back of that I space, and after the completion of the entire number of odd digits and the sum of the even digits of greatest common divisor not more than K, So how many kinds of filling are there to satisfy the above conditions?
Obviously initial state f[0][1~maxk][1~maxs][1~maxs] = 1, where maxk represents the maximum value of the input K, MAXS represents the maximum number that may occur;
Transfer:
1. F[i][k][s1][s2], F[i+1][k][s1+j][s2] (0≤j <), i+1 odd
2. F[i][k][s1][s2], F[i+1][k][s1][s2+j] (0≤j <), i+1 to even
The burden of questioning is much reduced.
This problem also has Kaka constant.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype > #include <algorithm>using namespace std; #define LL Long Longll Read () {ll x = 0, f = 1; char c = GetChar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = GetChar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = GetChar ();} return x * f;} #define MAXN 19#define maxs 90#define maxk 101LL f[maxn][maxk][maxs][maxs];int gcd[maxs][maxs];int Gcd (int x, int y) {ret Urn!y? X:GCD (y, x% y); }int NUM[MAXN]; ll sum (ll x, int k) {int cnt = 0;while (x) num[++cnt] = x%, x/= 10;int s1 = 0, s2 = 0; LL ans = 0;for (int i = cnt; i; i--) {for (int j = 0; J < Num[i]; J + +) {ans + = f[i-1][k][s1][s2];if (i & 1) s1++; else s2++;}} Ans + = F[0][k][s1][s2];return ans;} int main () {for (int i = 0; i < maxs; i++) for (int j = 0; J < Maxs; J + +) Gcd[i][j] = GCD (i, j); for (int k = 1; k < m Axk k++) for (int i = 1, i < MAXS; i++) for (int j = 1; j < Maxs; J + +) if (Gcd[i][j] <= k) f[0][k][i][j]++;for (int i = 0; i < maxn-1; i++) for (int k = 1; k < maxk; k++) {int mxs = (maxn-i >> 1) * 9;for (int s1 = 0; s1 <= Mxs; s1++) for (int s2 = 0; s2 <= mxs, s2++) if (F[i][k][s1][s2]) for (int j = 0; J <= 9; j + +) {int T1 = S1, t2 = s2;if (i + 1 & 1) T1-= j; else T2-= j;if (T1 < 0 | | T2 < 0) Break;f[i+1][k][t1][t2] + = F[i][k][s1][s2];}} int T = read (); while (t--) {int k = read (); LL L = Read (), r = Read ();p rintf ("%lld\n", Sum (r, K)-sum (L-1, k));} return 0;}
[COJ0528] Bjoi Lucky Number