C. Pull Your Luck
C. Pull Your Luck
While James is gone on business, Vesper takes her time and explores what the legendary Casino Royale has to offer to people who are fond of competitive programming.
Her attention was grabbed by the very new "Pull Your Luck" roulette which functions in a pretty peculiar way. The roulette's wheel consists of sectors number from to . There is no ball and the winning sector is determined by a static arrow pointing to one of the sectors. Sectors' indexes go in the natural order and the wheel always spins in the direction of indexes increment. That means that sector goes right after sector for all from to , and sector goes right after sector .
After a bet is made, the player is allowed to pull the triggering handle herself and cause the wheel to spin. If the player's initial pull is made with the force equal to positive integer , the wheel will spin for seconds. During the first second it will advance sectors, the next second it will advance sectors, then sectors, and so on until it comes to a complete stop. After the wheel comes to a complete stop, the sector which the arrow is pointing to is the winning one.
The roulette's arrow currently points at sector . Vesper knows that she can pull the handle with any integer force from to inclusive. Note that it is not allowed to pull the handle with force , i. e. not pull it all. The biggest prize is awarded if the winning sector is . Now Vesper wonders if she can make sector win by pulling the triggering handle exactly once?
Input
The first line of the input contains a single integer () — the number of test cases. Then follow lines containing one test description each.
Each test description consists of three integers , and (, , ). They are the number of sectors on the wheel, the current sector the arrow points at, and the maximum force that Vesper can pull the handle with, respectively.
It is guaranteed that the sum of over all test cases doesn't exceed .
Output
Print lines, the -th line should contain the answer for the -th test case. If it is possible to pull the handle with the integer force from to in order to make sector win, print "Yes". Otherwise, print "No".
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Example
input
7 5 2 1 5 2 2 10 0 100 11 7 100 3 1 1000 31 0 10 100 49 7
output
No
Yes
Yes
Yes
No
No
No
Note
In the first example, the only possible way to pull the handle is with force . That is not enough to make the arrow point at sector , at least force is required to do so.
In the second example, Vesper can pull the handle with the force so the wheel will spin sectors ahead and the arrow will point at sector .
In the third example, Vesper can pull the handle with the force so the wheel will spin sectors and will point at sector again.
In the fourth example, Vesper can pull the handle with the force so the wheel will spin sectors. That will make the wheel make one full turn plus more sectors.
In the fifth example, whatever force Vesper chooses to pull the handle with, she can only make sectors and win.
解题思路
题目看半天,又长又臭.jpg。
说白了就是问是否存在一个,使得,也即。
首先我们知道如果,那么就会有。因此比赛的时候就想着两边同时乘上,然后会得到,比赛的时候一直想着用扩欧来求,肯定求不出来,因为变量是二次的。
然后赛后看了眼题解想到,对哦,直接枚举来看看哪个数满足这个同余方程不就行了吗。对于同余方程,当时得到是之前重复的结果,这是因为
因此从枚举到就可以判断是否存在满足上述同余方程的解。但实际上这种做法是错误的。
这是因为可以推出。但不一定可以推出,举个简单的反例还可以推出,而
那么加个判断条件加个不就可以了吗。还是不行,这就涉及到余数循环周期这个问题了。
我们先考虑原来的式子,即,如果代入,那么就会有
这说明对于任意的,当那么得到的结果会是之前已有的情况,或者说得到的余数是一个循环,即存在一个周期。如果定义,那么就会有。
所以这就解释了为什么仅加个判断条件还是不行,因为至少要枚举到。注意,我们应该关注的是原来的同余方程。
所以应该从枚举到,然后判断是否满足同余方程即可。
AC代码如下,时间复杂度为:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 void solve() { 5 int n, x, p; 6 scanf("%d %d %d", &n, &x, &p); 7 p = min(p, 2 * n); 8 for (int i = 1; i <= p; i++) { 9 if ((x + i * (i + 1ll) / 2) % n == 0) { 10 printf("YES\n"); 11 return; 12 } 13 } 14 printf("NO\n"); 15 } 16 17 int main() { 18 int t; 19 scanf("%d", &t); 20 while (t--) { 21 solve(); 22 } 23 24 return 0; 25 }
一个值得注意的问题是,这个周期是怎么得到的?其实我觉得首先你要意识到具有周期性这个性质,然后就是代入,等等去试,看能不能得到。昨天寻找周期性这个问题困扰了我一天,然后还写了这篇博文:关于多项式方程所在剩余系的余数循环周期的猜想与推导。算是学到了很多东西吧。
参考资料
Nebius Welcome Round Editorial:https://codeforces.com/blog/entry/113830
Nebius Welcome Round (Div. 1 + Div. 2) C:https://zhuanlan.zhihu.com/p/613470409
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17216164.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-03-14 修改数组
2022-03-14 垒骰子
2021-03-14 一元多项式的乘法与加法运算