传送门
Board
然后这是我们
A题TLE到死,然后发现status里面有好多人都是2秒多3秒过的……我们1秒就T了,于是问了一下KADR,得到的回答是:
Hi,Thanks for pointing out this problem! I’ve asked MikeMirzayanov about it and he said there was a bug in the system which was fixed about a month ago. Apparently time limit has been set to 5 seconds for a few months and some people got AC with nonoptimal solutions. I think it is not fair to rejudge their solutions now, but taking into account the fact that this is only a training in the gym, from now on the time limit will be set to 1 second.Problem statement says that the time limit is 2 sec for this problem. However, Codeforces judging servers are faster than servers used on the original contest in Petrozavodsk training camp. That’s why some time limits on Codeforces differ from the ones in statement. You can find the correct time limits in “Problems” tab on the contest page. Sorry for inconvenience.
-.-反正是练习嘛,就这样吧。
记一下A题
先给定一个素数P(P<=10^8),再给定Q(Q<=10^4)个询问,每个询问给定$$a_i, b_i$$,问$$a_i^x = b_i (mod P)$$的最小解x是多少。
如果啥都不看显然是经典的离散对数问题。
Baby-step Giant-step是其中一个经典解法,但是这个复杂度是$$O(\sqrt{P})$$的。乘起来到10^8还是会T。
用原根$$g^s, g^t$$去分别替掉a和b的话,就变成了$$(g^s)^x = g^t (mod P)$$,也就变成解这个同余方程$$s*x = t (mod (P – 1))$$,这个可以扩展gcd然后算,就不说了。
然后变成怎么求a, b对原根g的指标(也就是求s和t)。这就又回到了离散对数问题,虽然想呵呵,但是仔细想想还是有点不一样的。那就是变成了询问2Q次$$g^x = a (mod P)$$的解,这个底数被固定了。
最开始我们是想打张指标表,但是单纯的O(P)还是会T。
如果TL是5s的话,这里已经足够通过了。
于是我们还是得用Baby-step Giant-step,但是得注意到这个分块的精髓在于让两边的计算量接近。
那么假设我们每个块是S这么大,BSGS的时候就要先打一张S这么大的表,都塞进hash table里面。
然后对于每一个求指标的询问,我们需要扫P/S次,每次都要到hash table里面找一找。
于是打表的复杂度是S,处理询问的复杂度是Q*P/S,为了使总复杂度最小,两边取等号,S = Q*P/S,于是$$S=\sqrt{Q*P}$$,算起来就是S=10^6。
总计算量就是10^6级别了。
于是速度刷到了第一……
#include
#include
#include
#include
#include
#include
#include