ZOJ 3513 Human or Pig
扫描二维码
随时随地手机看文章
题面:
D - Human or Pig Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu SubmitStatusPracticeZOJ 3513
Description
A man is lost in a strange world. In this world, he is a human in the daytime, and he will become a pig at night. The strange world is rectangle which is seperated into many 1 * 1 grids, from (0,0) to (X,Y). Every grid has a coordinate (x , y ). If he is in the grid ( x , y ), he can jump to grid (x - k * y , y ) or ( x , y - k * x ). k is a positive integer. For example, if he is in the grid (4,9), he can only jump to the grid (4,1) or (4,5). At night, he jumps to another grid at 1:00 AM as pig. In the daytime, he jumps to another grid at 1:00 PM as a human. So he will jump exactly twice everyday.
As the figure show, the grids ( x , 0 ), ( 0 , y ) (0 <= x <= X, 0 <=y <= Y ) are the river. When he Jump into the river, he will change from pig to human or change from human to pig immediately. And his property will never change from then on. It means that if he jump into the river in the daytime, he will be a pig forever. He want to jump into the river at night, so that he change from pig to human immediately, and can be a human forever, never become a pig again.
When he become a pig at night, he will jump to a grid whose coordinate satisfies (x - k * y , y ) or ( x , y - k * x ) arbitrarily. He will not jump out of the strange world either in the daytime or at night. At the beginning, he is at the grid (x0 , y0 ). To ensure that he can jump into the river as a pig at last, at the beginning, he can choose to start as a pig at night or as a human in the daytime. You need to determine what time (day or night) to start in every grid of the strange world excecpt the river. Use a matrix to display it.
Input
There are multiple cases (no more than 100).
Each case contain two integers X and Y (1 <= X * Y <= 40000) indicating the size of the strange world.
Output
For each test case i, print case number in the form "Case #i" in one single line. And there is aX*Y matrix. The j th charater of the i th line indicating what time (day or night) to start in the grid (i , j ). 'H' means that to ensure that he can jump into the river as a human, he needs to start as a human in the daytime. 'P' means that to ensure that he can jump into the river as a human, he needs to start as a pig at night.
Sample Input
1 2 2 3
Sample Output
Case #1: PH Case #2: PHH HPP
题目大意:
给定一个初始坐标,(x,y)可以移动到(x-k*y,y)或者(x,y-k*x),k为正整数。初始给定的点在一张地图上,地图上的(x,0),(0,y)是一条神奇的河,跳进去就会发生身份的转变,即猪变人,人变猪,且不会再发生改变。很重要的一点是,状态为猪是笨的,他的选择是随意的,而人的状态是明智的,他是奔着最后可以变为人的目标去的。其实,当x,y相对大小不变关系时,只能移动x,或者y。故比如x>y时,可以移动x/y次。那么就可以视这些可移动的为一个序列。进而可以转换为取石子的模型,每次可移动的步数,可以视为一堆石子。与普通取石子模型不同的是,这道题需要按固定顺序取。因为,要保证最后那一跳是猪变人,所以如果某个位置是填猪,也就是无论他怎么跳,最后都会跳到猪变人的状态,若某个位置填人,那么就是该位置,不能任由猪乱跳,需要人掌控。那么,人是如何做到掌控的呢,像一堆石子只有1个,人和猪是没有选择余地的,只能乖乖跳。但只要数量大于1,那么人就可以选择,比如,是留一个给猪跳,还是全都自己跳。因此,只要数取石子序列从最开始开始的连续1的个数,然后确保第一个出现大于1的位置是留给人的就ok啦。
代码:
#include#include#include#include#includeusing namespace std; int cal(int x,int y) { int tc=0,tx=0; if(x<y) { x^=y; y^=x; x^=y; } while(1) { tx=x/y; if(tx==1) tc++; else break; x%=y; if(x==0)break; x^=y; y^=x; x^=y; } return tc; } int main() { int n,m,cnt=1,res; while(~scanf("%d%d",&n,&m)) { printf("Case #%d:n",cnt++); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { res=cal(i,j); if(res%2) printf("P"); else printf("H"); } printf("n"); } } return 0; }