Function and Function(青島icpc)
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
Hint
Author: WENG, Caizhi
Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[10]= {1,0,0,0,1,0,1,0,2,1};
int ff(long long n)
{
int sum=0;
if(n==0)
return 0;
while(n>0)
{
sum+=a[n%10];
n/=10;
}
return sum;
}
long long f(long long n,long long k,long long m)
{
int a;
if(k==0)
return n;
a=ff(n);
if(n==0)
{
if(k%2)
return 1;
else return 0;
}
if(n==1)
{
if(k%2)
return 0;
else return 1;
}
return f(a,k-1,m);
}
int main()
{
long long t,k,n;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&k);
printf("%lld\n",f(n,k,k));
}
return 0;
}