ZOJ 4070 - Function and Function - [签到题][2018 ACM-ICPC Asia Qingdao Regional Problem M]

Posted dilthey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 4070 - Function and Function - [签到题][2018 ACM-ICPC Asia Qingdao Regional Problem M]相关的知识,希望对你有一定的参考价值。

题目链接:http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5840

Time Limit: 1 Second  Memory Limit: 65536 KB

If we define $f(0)=1,f(1)=0,f(4)=1,f(8)=2,f(16)=1 cdots$, do you know what function $f$ means?

Actually, $f(x)$ calculates the total number of enclosed areas produced by each digit in $x$. The following table shows the number of enclosed areas produced by each digit:

Enclosed AreaDigitEnclosed AreaDigit
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:

 技术分享图片

For example, $g^2(1234)=f(f(1234))=f(1)=0$, and $g^2(5678)=f(f(5678))=f(3)=0$.

Given two integers $x$ and $k$, please calculate the value of $g^k(x)$.

技术分享图片

 

 

题解:

(浙大出题就是良心,又稳又好。)

 求 $k$ 层嵌套的 $f(x)$,因为几层 $f(x)$ 下去 $x$ 很快就变成 $0$ 或者 $1$ 了,这个时候,可以根据 $x$ 外面还剩下多少层 $f$ 直接返回 $0$ 或者 $1$。

 

AC代码:

#include<bits/stdc++.h>
using namespace std;
int fx[10]={1,0,0,0,1,0,1,0,2,1};
int x,k;
int f(int x)
{
    int res=0;
    do{
        res+=fx[x%10];
        x/=10;
    }while(x);
    return res;
}
int g(int k,int x)
{
    while(k--)
    {
        x=f(x);
        if(x==0) return k%2;
        if(x==1) return 1-k%2;
    }
    return x;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&x,&k);
        printf("%d
",g(k,x));
    }
}

 

以上是关于ZOJ 4070 - Function and Function - [签到题][2018 ACM-ICPC Asia Qingdao Regional Problem M]的主要内容,如果未能解决你的问题,请参考以下文章

ZOJ 4070Function and Function

zoj——1202 Divide and Count

ZOJ Monthly, January 2019 I Little Sub and Isomorphism Sequences(set 妙用) ZOJ4089

ZOJ - 4089 :Little Sub and Isomorphism Sequences (同构 set)

ZOJ-1107-FatMouse and Cheese-dfs+记忆化搜索

HDU1078 ZOJ1107 FatMouse and Cheese记忆化搜索