Flea CodeForces - 32C (思维)

本文解析了一道CodeForces上的经典跳蚤移动问题。该问题涉及在一个n*m的格子板上,跳蚤从任意格子出发,仅能沿水平或垂直方向跳动特定距离,目标是找到能让跳蚤覆盖最多不同格子的起始位置数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flea

CodeForces - 32C

It is known that fleas in Berland can jump only vertically and horizontally, and the length of the jump is always equal to s centimeters. A flea has found herself at the center of some cell of the checked board of the size n × m centimeters (each cell is 1 × 1 centimeters). She can jump as she wishes for an arbitrary number of times, she can even visit a cell more than once. The only restriction is that she cannot jump out of the board.

The flea can count the amount of cells that she can reach from the starting position (x, y). Let's denote this amount by dx, y. Your task is to find the number of such starting positions (x, y), which have the maximum possible value of dx, y.

Input

The first line contains three integers n, m, s (1 ≤ n, m, s ≤ 106) — length of the board, width of the board and length of the flea's jump.

Output

Output the only integer — the number of the required starting positions of the flea.

Example
Input
2 3 1000000
Output
6
Input
3 3 2
Output
4
题意:
有n*m的格子块,可以任选格子作为起点,每次只能横着或者竖着跳k个格子。定义d(x,y)为以(x,y)为起点能到达的格子数。
求使得d(x,y)最大的起点共有多少个。

思路:令x = n%s   y = m%s,也就是说行和列用步数整除后分别还剩下几个格子

           令z=n/s    t = m/s   ,也就求得用步数平分可以把行列分别分成几段

这样我们只需要任意选取范围内的行列,其交点便是满足的起点,种类数也就是全部乘起来就可以即x*y*z*t

举个例子:加入x = 2,y = 2,z = 3,t = 3,说明行分成了三段还多出两个格子,列也是同样,但是我们如果想要使到达的的格子数最多,我们就不能浪费多出的两个格子,我们发现如果我们我们正向跳跃,0->n,0->m,我们每次取每段的前两个作为起点是不是就成了四段了,而不是三段了,也就是加一,因此我们发现如果余数不为零,我们完全可以让分成的段数加一,只不过每次取的格子是余数的个数,而不是每段格子的个数,这样就可以达到最多。然后相乘也就是,从行选一段从列选一段,然后行列再选格子。

而如果余数是0呢即如果x=0,y=0,z= 3,t=3,那我们就只能从行列每段挑一个,然后在从每段中选一个格子,可选的格子便是每段所包含的格子数,所以我们令x=步数s,y=s,在四个相乘就可以了

code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;

int main(){
    ll n,m,s;
    while(~scanf("%lld%lld%lld",&n,&m,&s)){
        ll x = n % s;
        ll y = m % s;
        ll z = n / s;
        ll t = m / s;
        if(x == 0) x = s;
        else z++;
        if(y == 0) y = s;
        else t++;
        ll ans = x * y * z * t;
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值