Flea CodeForces - 32C (思维)

xiaoxiao2021-02-28  71

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

转载请注明原文地址: https://www.6miu.com/read-2602602.html

最新回复(0)