Hdu6048 Puzzle(2017多校第2场)

来源:互联网 发布:linux 宕机日志 编辑:程序博客网 时间:2024/06/11 19:54

Puzzle

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 469    Accepted Submission(s): 252


Problem Description
A Jigsaw puzzle contains N*M-1 pieces of jigsaws in a N rows*M columns rectangular board.Each jigsaw has a distinct number from 1 to N*M-1.Li is a naughty boy,he wants to arrange the board in his unique way.At the beginning,he picks all N*M-1 jigsaws out and put them on the table and then he will put them back to the board respecting the following steps:
1.Sorting all the remaining jigsaws on the table in ascending order.
2.Picking out the 1st ,the P+1 th ,the 2*P+1 th,......the n*P+1 th jigsaws and put them back to the blank area in the board one by one from the top row to the bottom row,from the left column to the right column.
3.if there are jigsaws remained on the table,back to step 1.
After he arranging the board,it’s obvious that there’s only one blank area located at the bottom-right corner.
Your task is to make the numbers on jigsaws sorted with every row and every column in ascending order(From left to right,top to bottom),and the blank area should be located at the bottom-right corner in the end.Each step you can move the blank area’s neighboring jigsaws(which share a common side with the blank area) towards the blank area.It’s really a difficult question,so you need to write a program to judge whether it is possible to complete the task.



 

Input
The first line contains an integer T(T<=100),which represents the number of test cases.
Following T lines,each line contains three integers N,M,P(2<=N,M<=1000;1<=P<=N*M-2).
 

Output
For each test case,print “YES” in a separate line if it is possible to complete the task ,otherwise please print “NO”.
 

Sample Input
33 2 33 2 4999 999 1
 

Sample Output
YESNOYES
 

Source
2017 Multi-University Training Contest - Team 2

———————————————————————————————————

题意:给出一个n*m的方格,以及1到n*m-1的数字,一开始按照一定的规则(规则是每次选择剩余数中,第1个,第p+1个,第p*2+1个…,选完一轮后,去除选择的数,剩下的数继续按照这个规则来选)选择每个数,从上到下,从左到右填入方格中,最后留一个空格子。 方格中的空格子可以通过和相邻的数字交换位置来移动,问最后是否可以得到1到n*m-1这些数字按照从左到右从上到下的顺序放在方格中(右下角是空格)

解题思路:由题意得空格的横向移动:排列不发生改变,逆序对奇偶性不改变;空格的竖向移动:由于最后空格回到了右下角,即竖向交换的次数为偶数次,所以也必然不改变逆序对的奇偶性。又因为最后逆序数对必然为零,所以只需要看一开始的序列逆序数对是否为偶数即可。按题意生成原始序列可以发现每一轮后方比这个数小的数量是等差数列,公差为p-1,项数为(n-1)/p+1,所以不需要真正求出排列


#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <math.h>#include <time.h>#include <algorithm>#include <complex>#include <vector>#include <stack>#include <queue>#include <unordered_map>#include <set>using namespace std;#define LL long long#define mem(a,b) memset(a,b,sizeof a);const int inf=0x3f3f3f3f;const LL llinf=0x3f3f3f3f3f3f3f3f;const double pi=acos(-1.0);const double eps=1e-8;const LL mod=1e9+7;const int maxn=2010;int main(){    int T,m,n,p;    for(~scanf("%d",&T);T--;)    {        scanf("%d%d%d",&n,&m,&p);        int sum=m*n-1;        int cnt=0;        while(sum>=p)        {            int k=(sum-1)/p+1;            cnt+=(p-1)*(k-1)*k/2;            sum-=k;        }        if(cnt%2==0)            printf("YES\n");        else            printf("NO\n");    }    return 0;}



原创粉丝点击