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.
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.
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.
For each test case i, print case number in the form "Case #i" in one single line. And there is a X*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.
1 2 2 3
Case #1: PH Case #2: PHH HPP
#include <fstream> #include <stdio.h> #include <iostream> #include <string.h> #include <string> #include <limits.h> #include <algorithm> #include <math.h> #include <numeric> #include <functional> #include <ctype.h> using namespace std; const int kMAX=1<<16; char grid_[kMAX]; int n,m; int grid(int x,int y) { return x*m+y; } char Get(int x,int y) { int ret = grid(x,y); char c='P'; for(int k=1;x>k*y;++k) { int yy=grid(x-k*y,y); if(grid_[yy] == 'P') { c='H'; break; } } for(int k=1;y>k*x;++k) { int yy=grid(x,y-k*x); if(grid_[yy] == 'P') { c='H'; break; } } return grid_[ret]=c; } int main() { int cases=1; while(~scanf("%d%d",&n,&m) ) { printf("Case #%d:\n",cases++); memset(grid_,'?',sizeof(grid_)); for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) putchar(Get(i,j)); puts(""); } } return 0; }
#include<iostream> #include<cstdlib> #include<stdio.h> #include<memory.h> using namespace std; int n,m; int mat[100000]; int get(int x,int y) { return x*m+y; } void solve(int x,int y) { int id=get(x,y); //if(mat[id]!=-1) return ; bool flag=true; for(int i=1;;i++) { int px=x-i*y; if(px<=0) break; int iid=get(px,y); if(mat[iid]==0) { flag=false; break; } } if(flag) { for(int i=1;;i++) { int py=y-i*x; if(py<=0) break;//悲催,写成<0wrong了 int iid=get(x,py); if(mat[iid]==0) { flag=false; break; } } } if(flag) mat[id]=0; else mat[id]=1; return ; } int main() { int count=1; while(scanf("%d%d",&n,&m)!=EOF) { memset(mat,-1,sizeof(mat)); printf("Case #%d:\n",count++); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) solve(i,j); int id=1*m+1; int c=0; for(int i=id;i<=n*m+m;i++) { if(mat[i]==0) cout<<'P'; else cout<<'H'; c++; if(c==m) { c=0; cout<<endl; } } } }