The 2018 ACM-ICPC Asia Qingdao Regional Contest

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 AreaDigitEnclosed AreaDigit
0150
1061
2070
3082
4191

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
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值