Counting dp
Integer division:
Given a number n, divide it into the form of a1, a2, a3,...,an. Where a1≥a2≥a3≥…≥an.
Method 1: Complete backpack
Using the combination of k i, remove the k i, and the total composition is j-k * i.
The state equation can be deduced as:
f[i][j] = f[i-1][j] + f[i-1][j-i] +… + f[i-1][j-k * i ];
f[i][j-i] = f[i-1][j-i] +… + f[i-1][j-k * i]; get
f[i][j] = f [i-1][j] + f[i][j-i];
Converted to one dimension: f[j] = f[j] + f[j-i];
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
const int mod = 1e9 + 7;
int f[N];
int n;
int main()
{
cin >> n;
f[0] = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
f[j] = (f[j] + f[j - i]) % mod;
cout << f[n] << endl;
return 0;
}
Method two: counting dp
Divide the problem into patterns where the minimum value is 1 and the minimum value is greater than 1. i represents the sum, j represents the division into several groups.
Push the transfer equation:
(1) If the minimum value is 1, remove the minimum value. It can be expressed as f[i-1][j-1];
(2) If the minimum value is greater than 1, the value of each group is reduced by 1. It can be expressed as f[i-j][j];
f[i][j] = f[i-1][j-1] + f[i-j][j];
ans = f[n, 1] + f[n, 2] + f[n, 3] +… + f[n, n];
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
const int mod = 1e9 + 7;
int f[N][N];
int n;
int main()
{
cin >> n;
f[0][0] = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= i; j ++)
f[i][j] = (f[i - 1][j - 1] + f[i - j][j]) % mod;
int res = 0;
for(int i = 1; i <= n; i ++)
res = (res + f[n][i]) % mod;
cout << res << endl;
return 0;
}
Digital statistics dp
Find the number of times each number appears in a certain interval, for example: 11, 12, 13, 14, 15. The number of occurrences of number 1 is 6 times.
count(n, x) represents the number of occurrences of x in 1~n.
If you want to express the number of occurrences of x in [a, b], just use count(b, x)-count(a-1, x); to
discuss the situation:
1~n, x.
n = abcdefg
, respectively The number on everyone.
When x> 0
- 000~abc-1, x, 000 ~ 999 have abc * 1000 cases;
- abc, x
1) When d <x, it is 0;
2) When d = x 000 ~ efg, there are efg cases;
3) When d> x 000 ~ 999, there are 1000 cases.
When x = 0 - 001~abc-1, x, 000 ~ 999, there are abc * 1000 cases;
- abc, x
1) When d <x, it is 0;
2) When d = x 000 ~ efg, there are efg cases;
3) When d> x 000 ~ 999, there are 1000 cases.
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 10;
const int mod = 1e9 + 7;
int get(vector<int> num, int l, int r)
{
int res = 0;
for(int i = l; i >= r; i --)
res = res * 10 + num[i];
return res;
}
int power10(int x)
{
int res = 1;
while(x --) res *= 10;
return res;
}
int count(int n, int x)
{
if(!n) return 0;
vector<int> num;
while(n)
{
num.push_back(n % 10);
n /= 10;
}
n = num.size();
int res = 0;
for(int i = n - 1 - !x; i >= 0; i --)
{
if(i < n - 1)
{
res += get(num, n - 1, i + 1) * power10(i);
if(!x) res -= power10(i);
}
if(num[i] == x) res += get(num, i - 1, 0) + 1;
else if(num[i] > x) res += power10(i);
}
return res;
}
int main()
{
int a, b;
while(cin >> a >> b, a || b)
{
if(a > b) swap(a, b);
for(int i = 0; i < 10; i ++)
cout << count(b, i) - count(a - 1, i) << ' ';
cout << endl;
}
return 0;
}