Function and Function
Time Limit: 1 Second Memory Limit: 65536 KB
If we define , do you know what function means?
Actually, calculates the total number of enclosed areas produced by each digit in . The following table shows the number of enclosed areas produced by each digit:
Enclosed Area | Digit | Enclosed Area | Digit |
---|---|---|---|
0 | 1 | 5 | 0 |
1 | 0 | 6 | 1 |
2 | 0 | 7 | 0 |
3 | 0 | 8 | 2 |
4 | 1 | 9 | 1 |
For example, , and .
We now define a recursive function by the following equations:
For example, , and .
Given two integers and , please calculate the value of .
Input
There are multiple test cases. The first line of the input contains an integer (about ), indicating the number of test cases. For each test case:
The first and only line contains two integers and (). Positive integers are given without leading zeros, and zero is given with exactly one '0'.
Output
For each test case output one line containing one integer, indicating the value of .
Sample Input
6
123456789 1
888888888 1
888888888 2
888888888 999999999
98640 12345
1000000000 0
Sample Output
5
18
2
0
0
1000000000
解题思路:考虑到数字不超过10的9次方总共的0的个数不超过18个直接枚举。。。
坑点是当前数字为0的时候要返回1;
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
#include<cmath>
#include<stdlib.h>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x)
#define pb(x) push_back(x)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define LL long long
#define N 500005
#define MAXN 2000005
#define inf 0x3f3f3f3f
int a[15]={1,0,0,0,1,0,1,0,2,1};
int b[18]={17,16,15,13,12,11,7,5,3,2,1};
int c[18]={14,10,9,6,4,0};
int cal(int n)
{
int ans=0;
if(n==0)return 1;
while(n)
{
ans+=a[n%10];
n/=10;
}
return ans;
}
void solve(int n,int k)
{
int now=cal(n);
if(k==0)printf("%d\n",n);
else if(k==1)printf("%d\n",now);
else
{
int type;
k--;
for(int i=0;i<11;i++)
{
if(now==b[i])
type=1;
}
for(int i=0;i<6;i++)
{
if(now==c[i])
type=2;
}
if(now==8||now==18)type=3;
if(type==1)
{
printf("%d\n",k%2==0?1:0);
}
else if(type==2)printf("%d\n",k%2==0?0:1);
else
{
if(k==1)printf("2\n");
else printf("%d\n",k%2==0?0:1);
}
}
}
int main()
{
int t;
sca(t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
solve(n,k);
}
}
/*
f17=0 f0=1
f18=2
f16=0 f0=1 f1=0
f15 f0 1
f14 f1 f0 f1
f13 f0 f1
f12 f0 f1
f11 f0 f1
f10 f1 f0
f9 f1 f0
f8=2 f2=0 f0=1 f1
f7 f0 f1
f6=1 f1=0 f0
f5 f0 f1
f4 f1 f0
f3 f0 f1
f2 f0 f1
f1 f0 f1
f0 f1 f0
*/