//递推,n根火柴可以摆出多少个数字组合
采用递推
f[0]=1;
for(int i=0;i<=2000;i++)
for(int j=0;j<10;j++)
if(!(i==0&&j==0)&&i+c[j]<=2000)
f[i+c[j]]+=f[i];
注意第一个数字不能使0(除了只有一个数字0的情况)
Time Limit: 2000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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 should be used. For instance, if you have 3 matches, then you can only make the numbers 1 or 7. If you have 4 matches, then you can make the numbers 1, 4, 7 or 11. Note that leading zeros are not allowed (e.g. 001, 042, etc. are illegal). Numbers such as 0, 20, 101 etc. are permitted, though.
Input
Input contains no more than 100 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
Problemsetter: Mak Yan Kei
//递推,n根火柴可以摆出多少个数字组合
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 720
struct HP
{
int len,s[MAXN];
HP()
{
memset(s,0,sizeof(s));
len=1;
}
HP operator =(const char *num)
{
len=strlen(num);
for(int i=0;i<len;i++) s[i]=num[len-i-1]-'0';
}
HP operator =(int num)
{
char s[MAXN];
sprintf(s,"%d",num);
*this=s;
return *this;
}
HP(int num) { *this=num;}
HP(const char*num) {*this=num;}
string str()const
{
string res="";
for(int i=0;i<len;i++) res=(char)(s[i]+'0')+res;
if(res=="") res="0";
return res;
}
HP operator +(const HP& b) const
{
HP c;
c.len=0;
for(int i=0,g=0;g||i<max(len,b.len);i++)
{
int x=g;
if(i<len) x+=s[i];
if(i<b.len) x+=b.s[i];
c.s[c.len++]=x%10;
g=x/10;
}
return c;
}
void clean()
{
while(len > 1 && !s[len-1]) len--;
}
HP operator += (const HP& b)
{
*this = *this + b;
return *this;
}
};
istream& operator >>(istream &in, HP& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator <<(ostream &out, const HP& x)
{
out << x.str();
return out;
}
int c[]={6,2,5,5,4,5,6,3,7,6};
HP f[2010];
HP s[2010];
int main()
{
//注意第一个数字不能是0
for(int i=0;i<=2000;i++)
f[i]=0;
f[0]=1;
for(int i=0;i<=2000;i++)
for(int j=0;j<10;j++)
if(!(i==0&&j==0)&&i+c[j]<=2000)
f[i+c[j]]+=f[i];
s[0]=0;
for(int i=1;i<=2000;i++)
s[i]=s[i-1]+f[i];
int n;
while(~scanf("%d",&n))
{
//注意之前的递推都是以第一个数不为0为前提
//当n>=6时,可以只有一个数0
if(n>=6)
cout<<s[n]+1<<endl;
else
cout<<s[n]<<endl;
}
return 0;
}