过时间了没有提交,不知道正不正确,就是想纪念一下,还是太年轻了。。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
unsigned char a[5];
char b[5][10];
char str[10][2]={"0","1","2","3","4","5","6","7","8","9"};
void saveToA(char num, int index)//按位保存到数组低位
{
switch(num)
{
case '1':a[index] |= 0x1;break;
case '2':a[index] |= 0x2;break;
case '3':a[index] |= 0x4;break;
case '4':a[index] |= 0x8;break;
case '5':a[index] |= 0x10;break;
case '6':a[index] |= 0x20;break;
case '7':a[index] |= 0x40;break;
default :a[index] |= 0x0;break;
}
}
void saveToB(unsigned char a[],int k)
{
int i,j;
for(i=5-k;i<5;i++)
{
int visited[10];
for(j=0;j<10;++j)
visited[j]=1;
if(a[i] & 0x1)
visited[1]=visited[4]=0;
if(a[i] & 0x2)
visited[1]=visited[2]=visited[3]=visited[7]=0;
if(a[i] & 0x4)
visited[5]=visited[6]=0;
if(a[i] & 0x8)
visited[0]=visited[1]=visited[7]=0;
if(a[i] & 0x10)
visited[1]=visited[3]=visited[4]=visited[5]=visited[7]=visited[9]=0;
if(a[i] & 0x20)
visited[2]=0;
if(a[i] & 0x40)
visited[1]=visited[4]=visited[7]=0;
for(j=9;j>=0;--j)
if(visited[j]!=0)
{
strcat(b[i],str[j]);
}
}
}
int ans(int k,int n)
{
if(k<1)
return 0;
int count = 0;
//int value = 0;求和没有用到
int i,j;
int row = -1;
int column = -1;
int flag = 0;
for(i=5-k; i<5; ++i)
{
if(flag == 0)
{
int len = strlen(b[i]);//至少为1
for(j=0; j<len; ++j)
{
//if(b[i][j] == '#')
// continue;
if((b[i][j] - '0') * pow(10, 4-i) < n)
{
if(b[i][j] != '0')
{
row = i;
column = j;
flag = 1;
}
else
{
if(i == 4)
count += 1;
else
count += ans(5-i-1, n);
flag = 2;
break;
}
}
if(flag == 1)
{
int x = 1;
int p;
for(p = row+1; p<5; ++p)
x *= strlen(b[p]);
count += x;
if(j != len-1)
flag = 0;
else
{
flag = 2;
break;
}
}
else if(flag == 0 && j == len-1)
{
flag = 2;
break;
}
}
}
else
break;
}
return count;
}
int main()
{
int s;
int k, n;
int i;
cin>>s;
while(s--)
{
cin>>k>>n;
getchar();
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for(i=0; i<k; ++i)
{
char num;
num = getchar();
while(num != '\n')
{
if(num != ' ')
saveToA(num, 5-k+i);
num = getchar();
}
}
if(n == 0)
cout<<0<<endl;
else
{
saveToB(a, k);
cout<<ans(k, n)<<endl;
}
}
return 0;
}