2017多校2 1004 Puzzle

来源:互联网 发布:淘宝增值服务是什么 编辑:程序博客网 时间:2024/06/08 06:18

Puzzle

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


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
 



给一个n,m表示棋盘规格,再给一个p,将n*m-1个棋子从1到n*m-1编号放入棋盘,按照第1,1+p,1+2p,。。。,1+np的顺序来放,若超出范围则将剩下的棋子重新排序再按之前规则放。让右下角留一个空格,问能否通过类似华容道的方式,让空格进行一些相邻换位操作之后回到右下角,令棋盘里的数字按升序排列。因为空格换位完之后又要回来,所以操作的次数一定是偶数,那么就可以去寻找什么属性是每次操作都会在2种状态之中变化的,看了官方题解才知道是数列的逆序对数的奇偶性,再去打表看一下逆序对数的求和规律就可以发现如何去求逆序对数了。逆序对数是奇数则不行,偶数才可以。

官方题解:



#include<iostream>using namespace std;int main(){    int t;    cin>>t;    while(t--)    {        int n,m,p;        cin>>n>>m>>p;        int nu=n*m-1;        int ans=0;        while(nu>p)        {            int n1=(nu-1)/p+1;            ans+=n1*(n1-1)/2*(p-1);            nu=nu-n1;        }        puts(ans&1?"NO":"YES");    }    return 0;}     


原创粉丝点击