Topic description
Someone just learned digital DP, and he suddenly thought about the following questions one day:
Given n, ask how many pairs <x, y> satisfy:
x, y∈[1, n], x < y
The numbers of [0, 9] appearing in x, y are the same
enter
an integer n (n <= 107)
output
output a number as the answer
sample input
30
Sample output
3
hint
<1, 11> <2, 22> <12, 21>
source
The 10th ACM Programming Competition of Zhengzhou Institute of Light Industry
This problem is that the idea is that x, y are within 1~n, and the numbers appearing in x and y are the same, so we can violently store the number of all digital states from 1 to n. Then ask for a combination.
For example: 12 121 121212 1212121 212121 The numbers of these numbers are all 12, then the number with the number 12 is 5, and any two of these five numbers can form a kind of x, y.
Specifically look at the code:
1 #include<iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <string> 7 typedef long long ll; 8 using namespace std; 9 const int maxn = 1e6; 10 const int inf = 0x3f3f3f3f; 11 int nu[maxn]; 12 int dis[maxn]; 13 int vis[11]; 14 using namespace std; 15 int main() 16 { 17 int n; 18 scanf("%d", &n); 19 for(int i = 1; i <= n; ++i) { 20 memset(vis, 0 , sizeof(vis)); 21 int x = i; 22 while(x) { 23 vis[x % 10] = 1; 24 x /= 10; 25 } 26 int num = 0; 27 for(int j = 0; j <= 9; ++j) { 28 if(vis[j]) { 29 num += 1 << j; 30 } 31 } 32 nu[num]++; 33 } 34 ll ans = 0; 35 for(int i = 0; i < (1 << 10); ++i) { 36 if(nu[i]) 37 ans += nu[i] * (nu[i] - 1) / 2; 38 } 39 printf("%lld\n", ans); 40 return 0; 41 }