If we define f(0)=1,f(1)=0,f(4)=1,f(8)=2,f(16)=1,……, do you know what function means?
Actually, f(x) 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:
Digit | Enclosed Area | Digit | Enclosed Area |
---|---|---|---|
0 | 1 | 5 | 0 |
1 | 0 | 6 | 1 |
2 | 0 | 7 | 0 |
3 | 0 | 8 | 2 |
4 | 1 | 9 | 1 |
For example,f(1234)=0+0+0+1=1 , and f(5678)=0+1+0+2=3 .
We now define a recursive function by the following equations:
g^0(x)=x;
g^k(x)=f(g^k-1(x)) if k>=1
For example,g^2(1234)=f(f(1234))=f(1)=0, and f(5678)=0+1+0+2=3.
Given two integers x and k, please calculate the value of g^k(x).
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
找规律,到一定数量会010101循环。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <deque>
#include <queue>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int M=1e5+10;
ll f[10]={1,0,0,0,1,0,1,0,2,1};
ll OJ(ll p)
{
ll ans=0;
if(p==0)
return 1;
while(p)
{
if(p==0)
break;
ans+=f[p%10];
p/=10;
}
return ans;
}
int main()
{
int T,i,j,n,k;
scanf("%lld",&T);
while(T--)
{
scanf("%lld%lld",&n,&k);
if(k==0)
{
printf("%lld\n",n);
continue;
}
ll ans=n;
while(k)
{
k--;
ans=OJ(ans);
if(ans==0)
break;
}
if(k==0)
printf("%lld\n",ans);
else
if(k%2==0)
printf("0\n");
else
printf("1\n");
}
return 0;
}