USACO 2020~2021 February Contest GOLD 题解(3)

USACO 2020~2021 二月黄金组 题解(3)

3. Count The Cows

As is typical, Farmer John’s cows have spread themselves out along his largest pasture, which can be regarded as a large 2D grid of square “cells” (picture a huge chessboard).
The pattern of cows across the pasture is quite fascinating. For every cell (x,y) with x≥0 and y≥0, there exists a cow at (x,y) if for all integers k≥0, the remainders when ⌊x3k⌋ and ⌊y3k⌋ are divided by three have the same parity. In other words, both of these remainders are odd (equal to 1), or both of them are even (equal to 0 or 2).
FJ is curious how many cows are present in certain regions of his pasture. He asks Q queries, each consisting of three integers xi,yi,di. For each query, FJ wants to know how many cows lie in the cells along the diagonal range from (xi,yi) to (xi+di,yi+di) (endpoints inclusive).

题目大意:农夫John的牛在一个二维方阵里吃草。对于方阵中的某个格子(x,y)(x,y为自然数),该方格中有牛当且仅当对于所有非负整数k,都有 ⌊ x / 3 k ⌋ \lfloor x/3^k \rfloor x/3k ⌊ y / 3 k ⌋ \lfloor y/3^k \rfloor y/3k除以3的余数同奇偶。现在John一共会作出Q次询问(1 ≤ \leq Q ≤ \leq 104 ),每个询问包含三个整数x,y,d(0 ≤ \leq x,y,d ≤ \leq 1018),询问在以(x,y)上顶点,(x+d,y+d)为下顶点的对角线上牛的总头数。

思路分析: 这道题乍一看是一道描述不算简单的数论题,我们先打一下表(其实通过题目中给出的样例也可以看出一些规律):

101000101
010000010
101000101
000101000
000010000
000101000
101000101
010000010
101000101

我们有如下猜想:整个方格是一个形状为“X”的分形图。下面提供对这一猜想的粗略证明:考虑背靠原点3*3个边长为3k的方格,将每个分割为9个边长为3k-1的小方格。可以发现,每个大方格的横纵坐标在除以3取余数之后,都会被映射至左上方大方格内的相对位置一致的小方格:如果大方格横纵坐标除以三之余数奇偶性不同,那么这个大方格不满足条件,因此对应小方格也不满足条件。运用数学归纳法的思想,可以推出对于任意的正整数k,在背靠原点的3 * 3个边长为3k的方格,横纵坐标奇偶性不同的方格没有牛,而横纵坐标奇偶性相同的方格与左上方小方格构图相同。因此,整个方格是一个形状为“X”的分形图。

有了这个重要的观察,我们可以运用递归的思想,将边长为3k的正方形对角线在O(1)的时间内转化为对边长为3k-1的正方形对角线的求解。具体来说,定义f(k,dif)为在以原点为起点,边长为3^k的正方形中所有满足x−y=dif且有牛的方格(x,y)的个数(也就是该正方形左下角边长为dif的正方形的对角线上有牛的方格的个数)。那么:
f ( k , d i f ) = { 3 f (

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值