zoj 4070 Function and Function (找规律)

这道题目定义了一个函数f(x),它计算数字x中每个数字产生的封闭区域总数。例如,f(1234) = 1,f(5678) = 3。接着引入递归函数g^k(x),其中g^0(x) = x,g^k(x) = f(g^(k-1)(x))。给定x和k,需要计算g^k(x)的值。输入包含多个测试用例,每个用例给出两个整数x和k,输出g^k(x)的结果。
摘要由CSDN通过智能技术生成

题目地址

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:

DigitEnclosed AreaDigitEnclosed Area
0150
1061
2070
3082
4191

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

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值