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

Hint


Author: WENG, Caizhi
Source: The 2018 ACM-ICPC Asia Qingdao Regional Contest

Submit    Status

#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;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值