#P5820. 【L&K R-03】射击场决战

【L&K R-03】射击场决战

Description

[If you do not want to read the story, please read the part below the separator.]

Xiao L and Xiao K are the leaders of two gangs, and they are sworn enemies. One year, a large-scale conflict broke out between the two gangs. Xiao K, being weaker, was surrounded by Xiao L’s followers and was finally forced to retreat to a large shooting range. Xiao K knew the situation was hopeless. Just as he was about to fight Xiao L to the death, news came that Xiao L wanted to invite him to play a game. Xiao K knew there must be a trap, but he had no other choice, so he went alone to the meeting point Xiao L specified.

The meeting point was beside the shooting range. Xiao L stood in the field and waved to the approaching Xiao K with a smile. “Ah, Xiao K, long time no see. I remember the last time we met, it was in that small tavern. Back then, I chatted with you happily and talked about the world. Who would have thought we would end up like this today!” Xiao L paused and continued, “You used to be my brother. I do not want to end things between us with violence. Luckily, I have a good idea: how about we settle this with a game? What do you say, Xiao K?” Xiao K knew he had no room to refuse Xiao L, so he nodded. Seeing that, Xiao L smiled again and began to explain the rules of the game.

“As you can see, our game will be played on this shooting range. The range has a total of nn rows and mm columns, i.e. n×mn\times m targets. For convenience, let the row direction be left to right, and the column direction be front to back. This shooting range has a special feature: each target has a counter. If you hit a target, the displayed value of its counter increases by one. However, each counter value has a range: it can only be an integer not less than 00 and not greater than kk. If you hit a target when its counter value is already kk before the hit, then upon being hit, its counter overflows, resets to 00, and emits an overflow error signal that starts to travel along wires. Due to the special wiring of the range, the signal only travels to the right. During the transmission, it affects some other counters in the same row. If an affected counter has value kk, it will also overflow, reset to 00, and emit an overflow error signal, which stacks with the previous signal (but the +1+1 effect does not stack). Otherwise, its value increases by one, and it sends a correction message that cuts off the signal propagation, meaning targets to its right will no longer be affected. Of course, if the signal keeps traveling without being cut off, it will eventually reach the information management terminal. Because signals keep stacking during transmission, and because the terminal has to process huge amounts of information, its ability to correct error signals is weak; this may cause the terminal to crash or even explode, which is against the rules.

“You and I will take turns choosing one target to shoot once. The shooter decides which target to shoot. If it is someone’s turn to shoot but they cannot make a shot without breaking the rules, then they lose. Since I designed this game, I will go first. However, I will also give you some choices. The initial value of each target’s counter is not necessarily 00; I can set it. I happen to have several setting plans, but I do not know which one to choose. Could you help me pick one?”

Xiao L took a few slips of paper out of his pocket. Xiao K looked and found that each slip did not list the initial values of all counters; it only had three numbers a,b,ca,b,c. What Xiao L did not know was that Xiao K had amazing observation skills. While Xiao L was speaking, Xiao K had already analyzed how the displayed values changed and how the circuit was wired, and figured out the rule used to generate the initial counter values. The initial values of the counters are generated one by one in order. The order is row-major: from left to right, from top to bottom. Specifically, they are generated in the order of row 11 column 1,2,,m1,2,\ldots,m, then row 22 column 1,2,,m1,2,\ldots,m, ..., up to row nn column 1,2,,m1,2,\ldots,m. Generating one counter value uses a,b,ca,b,c as parameters, and after each generation, a,b,ca,b,c all change. Concretely, for each generated counter value, the following function is called once:

typedef unsigned long long ull; 
inline ull generate(ull&a,ull&b,ull&c,ull&k)
{ 
	a<<=19;a+=b+c;
	a<<=26;a^=c+=a+81;b--;
	a<<=7;a>>=(b^c^1145)&14;
	c*=a;a|=b+=c;a^=b&c;
	return a%(k+1);
}

The return value of the function is the generated counter value. It is easy to see that all initial values are integers between 00 and kk, so they do not violate the rules.

Xiao K knows Xiao L is extremely smart and will surely aim to win. Of course, since Xiao K also knows a lot about this game, his skill will not be worse than Xiao L’s. Xiao K cannot break the rules made by Xiao L, or he might anger Xiao L and cause the conflict to break out again. However, Xiao K can compute which of Xiao L’s plans guarantee a win for Xiao L, and which guarantee a win for himself, and then choose a plan where he is guaranteed to win.

Unfortunately, time does not allow Xiao K to compute for too long. Luckily, you can program and help Xiao K get the result in a short time. Please help Xiao K determine, among all the plans Xiao L provides, which ones make Xiao L sure to win and which ones make Xiao L sure to lose.


Since what Xiao L said may be too long to understand, Xiao K decides to describe the problem more concisely. Xiao L provides a number matrix with nn rows and mm columns. Xiao L and Xiao K take turns operating. Each time, one number in the matrix can be increased by 11. If after adding 11 this number becomes greater than kk, then it is reset to zero, and the +1+1 operation is passed to the number on its right. If a number needs to pass the operation but there is no number to its right, then the current player’s initial operation is not allowed. The player who cannot make a move loses. Xiao L moves first, and both players are extremely smart. If Xiao L will win, output YES; otherwise output NO.

The initial numbers in the matrix are provided by Xiao L. Xiao L provides several plans to fill the matrix; in each plan, the numbers in the matrix are generated by the function above, in order from left to right and from top to bottom. The difference between plans lies in different parameters a,b,ca,b,c. It is guaranteed that the generation method is irrelevant to the correct solution of this problem. Note that for each plan, Xiao L actually provides six parameters k,n,m,a,b,ck,n,m,a,b,c, meaning k,n,mk,n,m may also differ between plans.

Input Format

The first line contains the total number of plans provided by Xiao L.

For each plan, six parameters are given in order: k,n,m,a,b,ck,n,m,a,b,c, with meanings as described in the statement.

Output Format

For each plan, if Xiao L will win, output YES; otherwise output NO.

2
1 1 1 1 1 1
1 1 1 2 2 2
YES
NO

Hint

[Sample Explanation]

In both plans, there is only one target in the shooting range.

For plan 1, the initial value on this target’s counter is 00. Xiao L, as the first player, shoots this target and makes its counter increase by 11. When it is Xiao K’s turn, the counter value is 11, and there is no legal shot, so Xiao K loses and Xiao L wins.

For plan 2, the initial value on this target’s counter is 11, and there is no legal shot, so Xiao L loses.

[Constraints]

At most 2020 plans.

Test ID Range of nn Range of mm Range of kk Special Property
11 n=1n=1 1m51\le m\le5 1k51\le k\le 5 None
22 1m201\le m\le20
33 1m1000001\le m\le100000 1k10181\le k\le 10^{18}
44 1n21\le n \le2
55 1n1000001\le n \le100000 m=1m=1
66 1n10001\le n \le1000 1m10001\le m\le1000 kk is even
77 1n500001\le n \le50000 1m201\le m\le20
88 1n101\le n \le10 1m1000001\le m\le100000
9119\sim 11 1n10001\le n \le1000 1m10001\le m\le1000 None
121412\sim 14 1n500001\le n \le50000 1m201\le m\le20
151715\sim 17 1n101\le n \le10 1m1000001\le m\le100000

For all testdata, k,n,m,a,b,ck,n,m,a,b,c are positive integers, and 1a,b,c10181\le a,b,c\le10^{18}. The score distribution is as follows: test ID 11 is worth 77 points; test IDs 99, 1212, and 1515 are worth 55 points; all others are worth 66 points.

Translated by ChatGPT 5