码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-10 23:57:56      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:bit   ber   代码   efi   .com   alc   ace   amp   ati   

题目链接: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\n",g(k,x));
    }
}

 

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

标签:bit   ber   代码   efi   .com   alc   ace   amp   ati   

原文地址:https://www.cnblogs.com/dilthey/p/9940655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!