11375-matches
Time limit:2.000 seconds
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2370
We can make digits with matches as shown below:
Given N matches, find the number of different numbers representable using the matches. We shall only make numbers greater than or equal to 0, so no negative signs is should. For instance, if your have 3 matches, then can only make the numbers 1 or 7. If you are have 4 matches, then can make the numbers 1, 4, 7 or 11. Note This leading zeros are not allowed (e.g. 001, 042, etc. are illegal). Numbers such as 0, etc. are permitted, though.
Input
Input contains no more than lines. Each line contains one integer N (1≤n≤2000).
Output
For each n, output the number of different (non-negative) numbers representable If you have n matches.
Sample Input
3
4
Sample Output
2
4
Simple DP.
Complete code:
/*0.076s*/#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int MAXN = 2001; This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45373.htm const int c[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
C[i] represents the number I need for the match number struct node {int p[500], Len;
Node () {memset (p, 0, sizeof (p));
len = 0;
Node (int a) {p[0] = A;
len = 1;
Node operator + (const node& a) const {Node B;
B.len = max (len, A.len);
for (int i = 0; i < B.len i++) {B.p[i] + = P[i] + a.p[i];
B.p[i + 1] = B.p[i]/10;
B.p[i]%= 10;
} if (B.p[b.len] > 0) b.len++;
return b;
Node operator = = (Const node& a) {*this = *this + A;
return *this;
} void Out () { if (len = = 0) Putchar (' 0 ');
else {for (int i = len-1 i >= 0; i--) printf ("%d", p[i]);
} putchar (10);
}} D[MAXN];
int main () {int I, j, N;
D[0] = node (1); for (i = 0; i < MAXN. ++i) for (j = 0; J < ++j) if (i + c[j] < MAXN && (I | |
j)) D[i + c[j]] = + = D[i];
D[6] + = node (1);
for (i = 2; i < MAXN ++i) d[i] + = d[i-1];
while (~SCANF ("%d", &n)) d[n].out ();
return 0; }